Skip to content

Commit

Permalink
merge : #151 행사 같이 가고 싶어요 등록 해제 api 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
amaran-th authored Aug 1, 2023
2 parents 0d27706 + 29ce1cc commit 8b18c76
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 276 deletions.
8 changes: 8 additions & 0 deletions backend/emm-sale/src/docs/asciidoc/index.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ include::{snippets}/participate-event/http-request.adoc[]
.HTTP response
include::{snippets}/participate-event/http-response.adoc[]

=== `DELETE` : 컨퍼런스에 함께가기 요청 취소(컨퍼런스에 함께 가는 멤버에서 삭제)

.HTTP request
include::{snippets}/participate-event-cancel/http-request.adoc[]

.HTTP response
include::{snippets}/participate-event-cancel/http-response.adoc[]

=== `GET`: 행사 목록 조회

.HTTP request
Expand Down
17 changes: 12 additions & 5 deletions backend/emm-sale/src/main/java/com/emmsale/event/api/EventApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,24 @@ public ResponseEntity<String> participateEvent(
@RequestBody final EventParticipateRequest request,
final Member member
) {
final Long participantId = eventService.participate(
eventId,
request.getMemberId(),
member
);
final Long participantId = eventService.participate(eventId, request.getMemberId(), member);

return ResponseEntity
.created(create(format("/events/%s/participants/%s", eventId, participantId)))
.build();
}

@DeleteMapping("/{eventId}/participants")
public ResponseEntity<String> cancelParticipateEvent(
@PathVariable final Long eventId,
@RequestBody final EventParticipateRequest request,
final Member member
) {
eventService.cancelParticipate(eventId, request.getMemberId(), member);

return ResponseEntity.noContent().build();
}

@GetMapping("/{id}/participants")
public ResponseEntity<List<ParticipantResponse>> findParticipants(@PathVariable final Long id) {
final List<ParticipantResponse> responses = eventService.findParticipants(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,21 @@ public Long participate(final Long eventId, final Long memberId, final Member me
return participant.getId();
}

public void cancelParticipate(final Long eventId, final Long memberId, final Member member) {
validateMemberNotAllowed(memberId, member);
if (!eventRepository.existsById(eventId)) {
throw new EventException(NOT_FOUND_EVENT);
}

participantRepository
.findByMemberIdAndEventId(memberId, eventId)
.ifPresentOrElse(
participant -> participantRepository.deleteById(participant.getId()),
() -> {
throw new EventException(EventExceptionType.NOT_FOUND_PARTICIPANT);
});
}

@Transactional(readOnly = true)
public List<EventResponse> findEvents(final EventType categoryName, final LocalDate nowDate,
final Integer year, final Integer month,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.emmsale.event.domain.repository;

import com.emmsale.event.domain.Participant;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ParticipantRepository extends JpaRepository<Participant, Long> {

Optional<Participant> findByMemberIdAndEventId(final Long memberId, final Long eventId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum EventExceptionType implements BaseExceptionType {

NOT_FOUND_EVENT(HttpStatus.NOT_FOUND, "해당하는 행사를 찾을 수 없습니다."),
FORBIDDEN_PARTICIPATE_EVENT(HttpStatus.FORBIDDEN, "참가하려는 사용자와 로그인된 사용자가 다릅니다."),
NOT_FOUND_PARTICIPANT(HttpStatus.NOT_FOUND, "해당 행사에 등록되지 않은 사용자입니다."),
ALREADY_PARTICIPATED(HttpStatus.BAD_REQUEST, "이미 참가신청한 멤버입니다."),
INVALID_STATUS(
HttpStatus.BAD_REQUEST,
Expand Down
9 changes: 9 additions & 0 deletions backend/emm-sale/src/main/resources/http/event.http
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,14 @@ Authorization: bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNjkwMTk2OTY5L
"memberId": 1
}

### Event 참여자 목록에서 멤버 삭제
DELETE http://localhost:8080/events/1/participants
Content-Type: application/json
Authorization: bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxIiwiaWF0IjoxNjkwMTk2OTY5LCJleHAiOjE2OTM3OTY5Njl9.yahaEBvKBA7xelNuykx8TROhemnzJAsu1Sv5rrSfCM0

{
"memberId": 1
}

### 행사 참여자 목록 조회
GET http://localhost:8080/events/1/participants
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ void participateEvent() throws Exception {
.andDo(document("participate-event", requestFields));
}

@Test
@DisplayName("Event에 사용자를 참여자 목록에서 제거할 수 있다.")
void cancelParticipateEvent() throws Exception {
//given
final Long eventId = 1L;
final Long memberId = 2L;
final EventParticipateRequest request = new EventParticipateRequest(memberId);
final String fakeAccessToken = "Bearer accessToken";

final RequestFieldsSnippet requestFields = requestFields(
fieldWithPath("memberId").type(JsonFieldType.NUMBER).description("멤버 식별자")
);

//when
mockMvc.perform(delete(format("/events/%s/participants", eventId))
.header("Authorization", fakeAccessToken)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isNoContent())
.andDo(document("participate-event-cancel", requestFields));
}

@Test
@DisplayName("특정 카테고리의 행사 목록을 조회할 수 있으면 200 OK를 반환한다.")
void findEvents() throws Exception {
Expand Down
Loading

0 comments on commit 8b18c76

Please sign in to comment.