Skip to content

Commit

Permalink
Feat: 설문 기반 이벤트 수정 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yxhwxn committed Aug 6, 2024
1 parent e34ae0a commit dc8d973
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public ResponseEntity<ApiResponse<List<EventResponseDTO.EventInfoDTO>>> getAllEv

@PostMapping("/new/comment/crawling")
@Operation(summary = "댓글 이벤트 생성 API",
description = "request : type(ENUM 타입으로, 'COMMENT와 SURVEY' 둘 중 하나를 입력해주시면 됩니다), " +
description = "Request : type(ENUM 타입으로, 'COMMENT와 SURVEY' 둘 중 하나를 입력해주시면 됩니다), " +
"title, description, url, startDate(yyyy-MM-dd), endDate(yyyy-MM-dd), announcementDate(yyyy-MM-dd)")
public ResponseEntity<ApiResponse<Void>> createCommentEvent(@RequestBody @Valid EventRequestDTO.CommentEventCreateDTO request, @CurrentAccount Account account) {
eventService.createCommentEvent(request, account.userId());
Expand All @@ -46,10 +46,17 @@ public ResponseEntity<ApiResponse<Void>> createCommentEvent(@RequestBody @Valid

@PostMapping("/new/survey")
@Operation(summary = "설문조사 이벤트 생성 API",
description = "request : type(ENUM 타입으로, 'COMMENT와 SURVEY' 둘 중 하나를 입력해주시면 됩니다), " +
description = "Request : type(ENUM 타입으로, 'COMMENT와 SURVEY' 둘 중 하나를 입력해주시면 됩니다), " +
"title, description, startDate(yyyy-MM-dd), endDate(yyyy-MM-dd), announcementDate(yyyy-MM-dd)")
public ResponseEntity<ApiResponse<Void>> createSurveyEvent(@RequestBody @Valid EventRequestDTO.SurveyEventCreateDTO request, @CurrentAccount Account account) {
eventService.createSurveyEvent(request, account.userId());
return ResponseEntity.ok(ApiResponse.of(ResponseCode.SUCCESS));
}

@PutMapping("/{eventId}/update")
@Operation(summary = "이벤트 수정 API", description = "PathVariable: eventId, Request : title, description, url, startDate, endDate, announcementDate")
public ResponseEntity<ApiResponse<Void>> updateEvent(@PathVariable Long eventId, @RequestBody @Valid EventRequestDTO.EventUpdateDTO request, @CurrentAccount Account account) {
eventService.updateEvent(eventId, request, account.userId());
return ResponseEntity.ok(ApiResponse.of(ResponseCode.SUCCESS));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,16 @@ public class EventRequestDTO {
public static class CommentEventCreateDTO {
@NotNull
private EventType type;

@NotEmpty
private String title;

@NotEmpty
private String description;

@NotEmpty
private String url;

@NotEmpty
private String startDate;

@NotEmpty
private String endDate;

@NotEmpty
private String announcementDate;
}
Expand All @@ -44,20 +38,30 @@ public static class CommentEventCreateDTO {
public static class SurveyEventCreateDTO {
@NotNull
private EventType type;

@NotEmpty
private String title;

@NotEmpty
private String description;

@NotEmpty
private String startDate;

@NotEmpty
private String endDate;

@NotEmpty
private String announcementDate;
}

@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public static class EventUpdateDTO {
@NotNull
private EventType type;
private String title;
private String description;
private String url;
private String startDate;
private String endDate;
private String announcementDate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.cmc.suppin.event.events.controller.dto.EventRequestDTO;
import com.cmc.suppin.event.events.controller.dto.EventResponseDTO;
import com.cmc.suppin.event.events.domain.Event;
import com.cmc.suppin.global.enums.EventType;
import com.cmc.suppin.member.domain.Member;

import java.time.LocalDate;
Expand Down Expand Up @@ -51,7 +52,6 @@ public static EventResponseDTO.CommentEventDetailDTO toEventDetailDTO(Event even

public static EventResponseDTO.EventInfoDTO toEventInfoDTO(Event event) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

return EventResponseDTO.EventInfoDTO.builder()
.type(event.getType())
.title(event.getTitle())
Expand All @@ -65,5 +65,23 @@ public static EventResponseDTO.EventInfoDTO toEventInfoDTO(Event event) {
.build();
}

public static Event toUpdatedEventEntity(EventRequestDTO.EventUpdateDTO request, Member member) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
Event.EventBuilder eventBuilder = Event.builder()
.title(request.getTitle())
.description(request.getDescription())
.startDate(LocalDate.parse(request.getStartDate(), formatter).atStartOfDay())
.endDate(LocalDate.parse(request.getEndDate(), formatter).atStartOfDay())
.announcementDate(LocalDate.parse(request.getAnnouncementDate(), formatter).atStartOfDay())
.member(member);

// Only set URL if the event type is COMMENT
if (request.getType() == EventType.COMMENT) {
eventBuilder.url(request.getUrl());
}

return eventBuilder.build();
}


}
4 changes: 4 additions & 0 deletions src/main/java/com/cmc/suppin/event/events/domain/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,8 @@ public void setMember(Member member) {
public void setStatus(EventStatus status) {
this.status = status;
}

public void setId(Long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface EventRepository extends JpaRepository<Event, Long> {
List<Event> findByMemberId(Long memberId);

Optional<Event> findByIdAndMemberId(Long id, Long memberId);

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,16 @@ public void createSurveyEvent(EventRequestDTO.SurveyEventCreateDTO request, Stri
event.setStatus(EventStatus.PROCESSING);
eventRepository.save(event);
}

public void updateEvent(Long eventId, EventRequestDTO.EventUpdateDTO request, String userId) {
Member member = memberRepository.findByUserIdAndStatusNot(userId, UserStatus.DELETED)
.orElseThrow(() -> new IllegalArgumentException("Member not found"));

Event event = eventRepository.findByIdAndMemberId(eventId, member.getId())
.orElseThrow(() -> new IllegalArgumentException("Event not found"));

Event updatedEvent = EventConverter.toUpdatedEventEntity(request, member);
updatedEvent.setId(event.getId()); // 유지하려는 ID 설정
eventRepository.save(updatedEvent);
}
}

0 comments on commit dc8d973

Please sign in to comment.