Skip to content

Commit

Permalink
Merge pull request #120 from onetime-with-members/feature/#119/mod-ev…
Browse files Browse the repository at this point in the history
…ent-title

[feat] : 유저는 생성한 이벤트 제목을 수정할 수 있다
  • Loading branch information
bbbang105 authored Nov 13, 2024
2 parents 48514b7 + f080ed9 commit a5465f2
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 3 deletions.
21 changes: 21 additions & 0 deletions src/main/java/side/onetime/controller/EventController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import side.onetime.dto.event.request.CreateEventRequest;
import side.onetime.dto.event.request.ModifyUserCreatedEventTitleRequest;
import side.onetime.dto.event.response.*;
import side.onetime.global.common.ApiResponse;
import side.onetime.global.common.status.SuccessStatus;
Expand Down Expand Up @@ -128,4 +129,24 @@ public ResponseEntity<ApiResponse<SuccessStatus>> removeUserCreatedEvent(
eventService.removeUserCreatedEvent(authorizationHeader, eventId);
return ApiResponse.onSuccess(SuccessStatus._REMOVE_USER_CREATED_EVENT);
}

/**
* 유저가 생성한 이벤트 제목 수정 API
*
* 이 API는 인증된 유저가 생성한 특정 이벤트의 제목을 수정합니다.
*
* @param authorizationHeader 인증된 유저의 토큰
* @param eventId 제목을 수정할 이벤트의 ID
* @param modifyUserCreatedEventTitleRequest 새로운 제목 정보가 담긴 요청 데이터
* @return 수정 성공 여부
*/
@PutMapping("/{event_id}")
public ResponseEntity<ApiResponse<SuccessStatus>> modifyUserCreatedEventTitle(
@RequestHeader("Authorization") String authorizationHeader,
@PathVariable("event_id") String eventId,
@Valid @RequestBody ModifyUserCreatedEventTitleRequest modifyUserCreatedEventTitleRequest) {

eventService.modifyUserCreatedEventTitle(authorizationHeader, eventId, modifyUserCreatedEventTitleRequest);
return ApiResponse.onSuccess(SuccessStatus._MODIFY_USER_CREATED_EVENT_TITLE);
}
}
4 changes: 4 additions & 0 deletions src/main/java/side/onetime/domain/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ public Event(UUID eventId, String title, String startTime, String endTime, Categ
this.endTime = endTime;
this.category = category;
}

public void updateTitle(String title) {
this.title = title;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package side.onetime.dto.event.request;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import jakarta.validation.constraints.NotBlank;

@JsonNaming(value = PropertyNamingStrategies.SnakeCaseStrategy.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
public record ModifyUserCreatedEventTitleRequest(
@NotBlank(message = "변경할 제목은 필수 값입니다.") String title
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public enum SuccessStatus implements BaseCode {
_GET_MOST_POSSIBLE_TIME(HttpStatus.OK, "200", "가장 많이 되는 시간 조회에 성공했습니다."),
_GET_USER_PARTICIPATED_EVENTS(HttpStatus.OK, "200", "유저 참여 이벤트 목록 조회에 성공했습니다."),
_REMOVE_USER_CREATED_EVENT(HttpStatus.OK, "200", "유저가 생성한 이벤트 삭제에 성공했습니다."),
_MODIFY_USER_CREATED_EVENT_TITLE(HttpStatus.OK, "200", "유저가 생성한 이벤트 제목 수정에 성공했습니다."),
// Member
_REGISTER_MEMBER(HttpStatus.CREATED, "201", "멤버 등록에 성공했습니다."),
_LOGIN_MEMBER(HttpStatus.OK, "200", "멤버 로그인에 성공했습니다."),
Expand Down
17 changes: 15 additions & 2 deletions src/main/java/side/onetime/service/EventService.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import side.onetime.domain.enums.Category;
import side.onetime.domain.enums.EventStatus;
import side.onetime.dto.event.request.CreateEventRequest;
import side.onetime.dto.event.request.ModifyUserCreatedEventTitleRequest;
import side.onetime.dto.event.response.*;
import side.onetime.exception.CustomException;
import side.onetime.exception.status.EventErrorStatus;
Expand Down Expand Up @@ -305,6 +306,19 @@ public List<GetUserParticipatedEventsResponse> getUserParticipatedEvents(String
// 유저가 생성한 이벤트 삭제 메서드
@Transactional
public void removeUserCreatedEvent(String authorizationHeader, String eventId) {
EventParticipation eventParticipation = verifyUserIsEventCreator(authorizationHeader, eventId);
eventRepository.deleteEvent(eventParticipation.getEvent());
}

// 유저가 생성한 이벤트 제목 수정 메서드
@Transactional
public void modifyUserCreatedEventTitle(String authorizationHeader, String eventId, ModifyUserCreatedEventTitleRequest modifyUserCreatedEventTitleRequest) {
EventParticipation eventParticipation = verifyUserIsEventCreator(authorizationHeader, eventId);
eventParticipation.getEvent().updateTitle(modifyUserCreatedEventTitleRequest.title());
}

// 유저가 이벤트의 생성자인지 검증하는 메서드
private EventParticipation verifyUserIsEventCreator(String authorizationHeader, String eventId) {
User user = jwtUtil.getUserFromHeader(authorizationHeader);
Event event = eventRepository.findByEventId(UUID.fromString(eventId))
.orElseThrow(() -> new CustomException(EventErrorStatus._NOT_FOUND_EVENT));
Expand All @@ -314,10 +328,9 @@ public void removeUserCreatedEvent(String authorizationHeader, String eventId) {
throw new CustomException(EventParticipationErrorStatus._NOT_FOUND_EVENT_PARTICIPATION);
}
if (!EventStatus.CREATOR.equals(eventParticipation.getEventStatus())) {
// 해당 이벤트의 생성자가 아닌 경우
throw new CustomException(EventParticipationErrorStatus._IS_NOT_USERS_CREATED_EVENT_PARTICIPATION);
}

eventRepository.deleteEvent(event);
return eventParticipation;
}
}
53 changes: 52 additions & 1 deletion src/test/java/side/onetime/event/EventControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import side.onetime.domain.enums.Category;
import side.onetime.domain.enums.EventStatus;
import side.onetime.dto.event.request.CreateEventRequest;
import side.onetime.dto.event.request.ModifyUserCreatedEventTitleRequest;
import side.onetime.dto.event.response.*;
import side.onetime.service.EventService;
import side.onetime.util.JwtUtil;
Expand Down Expand Up @@ -385,4 +386,54 @@ public void removeUserCreatedEvent() throws Exception {
)
));
}
}

@Test
@DisplayName("유저가 생성한 이벤트 제목을 수정한다.")
public void modifyUserCreatedEventTitle() throws Exception {
// given
String eventId = UUID.randomUUID().toString();
ModifyUserCreatedEventTitleRequest request = new ModifyUserCreatedEventTitleRequest("수정할 이벤트 제목");

String requestContent = new ObjectMapper().writeValueAsString(request);

Mockito.doNothing().when(eventService).modifyUserCreatedEventTitle(anyString(), anyString(), any(ModifyUserCreatedEventTitleRequest.class));

// when
ResultActions resultActions = this.mockMvc.perform(RestDocumentationRequestBuilders.put("/api/v1/events/{event_id}", eventId)
.header(HttpHeaders.AUTHORIZATION, "Bearer sampleToken")
.contentType(MediaType.APPLICATION_JSON)
.content(requestContent)
.accept(MediaType.APPLICATION_JSON));

// then
resultActions
.andExpect(status().isOk())
.andExpect(jsonPath("$.is_success").value(true))
.andExpect(jsonPath("$.code").value("200"))
.andExpect(jsonPath("$.message").value("유저가 생성한 이벤트 제목 수정에 성공했습니다."))

// docs
.andDo(MockMvcRestDocumentationWrapper.document("event/modify-user-created-event-title",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
resource(
ResourceSnippetParameters.builder()
.tag("Event API")
.description("유저가 생성한 이벤트 제목을 수정한다.")
.pathParameters(
parameterWithName("event_id").description("수정할 이벤트의 ID [예시 : dd099816-2b09-4625-bf95-319672c25659]")
)
.requestFields(
fieldWithPath("title").type(JsonFieldType.STRING).description("새로운 이벤트 제목")
)
.responseFields(
fieldWithPath("is_success").type(JsonFieldType.BOOLEAN).description("성공 여부"),
fieldWithPath("code").type(JsonFieldType.STRING).description("응답 코드"),
fieldWithPath("message").type(JsonFieldType.STRING).description("응답 메시지")
)
.responseSchema(Schema.schema("ModifyUserCreatedEventTitleResponseSchema"))
.build()
)
));
}
}

0 comments on commit a5465f2

Please sign in to comment.