From 297af63ac79ea6bfbceaf30b5779dbc904f0735b Mon Sep 17 00:00:00 2001 From: Namju Date: Thu, 30 May 2024 15:27:29 +0900 Subject: [PATCH] =?UTF-8?q?Fix:=20=EC=A3=BC=EC=84=9D=EC=B2=98=EB=A6=AC?= =?UTF-8?q?=ED=95=9C=20=EC=9D=B4=EB=B2=A4=ED=8A=B8=20=EC=83=9D=EC=84=B1/?= =?UTF-8?q?=EC=88=98=EC=A0=95=20API=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../deu/connect/luck/event/EventService.java | 39 +++++-------------- .../event/controller/EventController.java | 7 ++-- .../event/controller/EventRestController.java | 38 ++++++++---------- .../connect/luck/event/dto/EventRequestV2.kt | 7 +++- 4 files changed, 35 insertions(+), 56 deletions(-) diff --git a/src/main/java/ac/kr/deu/connect/luck/event/EventService.java b/src/main/java/ac/kr/deu/connect/luck/event/EventService.java index edb2fa3..03ea581 100644 --- a/src/main/java/ac/kr/deu/connect/luck/event/EventService.java +++ b/src/main/java/ac/kr/deu/connect/luck/event/EventService.java @@ -99,11 +99,10 @@ public Event createEvent(EventRequestV2 eventRequest, String managerEmail) { * * @param id 이벤트 UID * @param eventRequest 이벤트 생성 요청 폼 - * @param multipartFile 이벤트 대표 이미지 * @param managerEmail 이벤트 매니저 이메일 * @return 수정된 이벤트 */ - public Event updateEvent(Long id, EventRequest eventRequest, MultipartFile multipartFile, String managerEmail) { + public Event updateEvent(Long id, EventRequestV2 eventRequest, String managerEmail) { Event findEvent = eventRepository.findById(id).orElseThrow( () -> new IllegalArgumentException("해당 이벤트가 존재하지 않습니다.") ); @@ -112,37 +111,19 @@ public Event updateEvent(Long id, EventRequest eventRequest, MultipartFile multi throw new IllegalArgumentException("해당 이벤트의 매니저가 아닙니다."); } - if (multipartFile != null) { - String image = imageUploader.uploadImage(multipartFile).getData().getUrl(); + if (eventRequest.getImage() != null) { + String image = imageUploader.uploadImage(eventRequest.getImage()).getData().getUrl(); findEvent.setImageUrl(image); } - if (eventRequest.title() != null) { - findEvent.setTitle(eventRequest.title()); - } - - if (eventRequest.content() != null) { - findEvent.setContent(eventRequest.content()); - } - - if (eventRequest.zipCode() != null) { - findEvent.setZipCode(eventRequest.zipCode()); - } - if (eventRequest.streetAddress() != null) { - findEvent.setStreetAddress(eventRequest.streetAddress()); - } - - if (eventRequest.detailAddress() != null) { - findEvent.setDetailAddress(eventRequest.detailAddress()); - } + findEvent.setTitle(eventRequest.getTitle()); + findEvent.setContent(eventRequest.getContent()); + findEvent.setZipCode(eventRequest.getZipCode()); + findEvent.setStreetAddress(eventRequest.getStreetAddress()); + findEvent.setDetailAddress(eventRequest.getDetailAddress()); + findEvent.setStartAt(eventRequest.getStartAt()); + findEvent.setEndAt(eventRequest.getEndAt()); - if (eventRequest.startAt() != null) { - findEvent.setStartAt(eventRequest.startAt()); - } - - if (eventRequest.endAt() != null) { - findEvent.setEndAt(eventRequest.endAt()); - } return eventRepository.save(findEvent); } diff --git a/src/main/java/ac/kr/deu/connect/luck/event/controller/EventController.java b/src/main/java/ac/kr/deu/connect/luck/event/controller/EventController.java index 68ce686..94156f3 100644 --- a/src/main/java/ac/kr/deu/connect/luck/event/controller/EventController.java +++ b/src/main/java/ac/kr/deu/connect/luck/event/controller/EventController.java @@ -6,6 +6,7 @@ import ac.kr.deu.connect.luck.event.dto.EventRequestV2; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; @@ -68,13 +69,13 @@ public String getEventUpdate(@PathVariable("id") Long id, Model model) { } @PostMapping("/update") + @PreAuthorize("hasRole('ROLE_EVENT_MANAGER')") public String updateEventPost( @RequestParam("eventId") Long id, Principal principal, - EventRequest eventRequest, - @RequestParam("multipartFile") MultipartFile multipartFile + EventRequestV2 eventRequest ) { - eventService.updateEvent(id, eventRequest, multipartFile, principal.getName()); + eventService.updateEvent(id, eventRequest, principal.getName()); return "redirect:/event/my"; } diff --git a/src/main/java/ac/kr/deu/connect/luck/event/controller/EventRestController.java b/src/main/java/ac/kr/deu/connect/luck/event/controller/EventRestController.java index fbacc75..2866f78 100644 --- a/src/main/java/ac/kr/deu/connect/luck/event/controller/EventRestController.java +++ b/src/main/java/ac/kr/deu/connect/luck/event/controller/EventRestController.java @@ -4,6 +4,7 @@ import ac.kr.deu.connect.luck.event.EventService; import ac.kr.deu.connect.luck.event.EventStatus; import ac.kr.deu.connect.luck.event.dto.EventDetailResponse; +import ac.kr.deu.connect.luck.event.dto.EventRequestV2; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.tags.Tag; @@ -12,7 +13,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; -import org.springframework.web.multipart.MultipartFile; import java.security.Principal; import java.util.List; @@ -39,40 +39,34 @@ public ResponseEntity getEvent(@PathVariable Long id) { return ResponseEntity.ok(eventService.getEvent(id)); } - //서비스 로직 수정함 레스트도 수정되어야할듯 - /*@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + /** + * 이벤트 생성 Rest API + * + * @param eventRequest 이벤트 생성 요청 정보 + * @param principal 로그인 한 사용자 정보 + * @return 생성된 이벤트 정보 + */ + @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PreAuthorize("hasRole('ROLE_EVENT_MANAGER')") @Operation(summary = "이벤트 생성", description = "이벤트 생성\n이벤트 주소입력 시 카카오 우편번호 서비스를 사용해서 주소를 입력받아야함.") public ResponseEntity createEvent( - @Parameter(description = "행사 이름") @RequestPart(value = "title") String title, - @Parameter(description = "이벤트 내용") @RequestPart(value = "content") String content, - @Parameter(description = "우편번호") @RequestPart(value = "zipCode") String zipCode, - @Parameter(description = "도로명주소") @RequestPart(value = "streetAddress") String streetAddress, - @Parameter(description = "상세주소") @RequestPart(value = "detailAddress") String detailAddress, - @Parameter(description = "시작일자 예시)2021-08-01T00:00:00") @RequestPart(value = "startAt") String startAt, - @Parameter(description = "종료일자 예시)2021-08-01T00:00:00") @RequestPart(value = "endAt") String endAt, - @Parameter(description = "이벤트 대표 이미지") @RequestPart(value = "image", required = false) MultipartFile multipartFile, + @ModelAttribute EventRequestV2 eventRequest, Principal principal ) { - return ResponseEntity.ok(eventService.createEvent(title, content, zipCode, streetAddress, detailAddress, startAt, endAt, multipartFile, principal.getName())); + return ResponseEntity.ok(eventService.createEvent(eventRequest, principal.getName())); } + + @PatchMapping(value = "/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PreAuthorize("hasRole('ROLE_EVENT_MANAGER')") @Operation(summary = "이벤트 수정", description = "이벤트 수정") public ResponseEntity updateEvent( @Parameter(description = "이벤트 UID") @PathVariable(value = "id", required = false) Long id, - @Parameter(description = "행사 이름") @RequestPart(value = "title", required = false) String title, - @Parameter(description = "이벤트 내용") @RequestPart(value = "content", required = false) String content, - @Parameter(description = "우편번호") @RequestPart(value = "zipCode", required = false) String zipCode, - @Parameter(description = "도로명주소") @RequestPart(value = "streetAddress", required = false) String streetAddress, - @Parameter(description = "상세주소") @RequestPart(value = "detailAddress", required = false) String detailAddress, - @Parameter(description = "시작일자 예시)2021-08-01T00:00:00") @RequestPart(value = "startAt", required = false) String startAt, - @Parameter(description = "종료일자 예시)2021-08-01T00:00:00") @RequestPart(value = "endAt", required = false) String endAt, - @Parameter(description = "이벤트 대표 이미지") @RequestPart(value = "image", required = false) MultipartFile multipartFile, + @ModelAttribute EventRequestV2 eventRequest, Principal principal) { - return ResponseEntity.ok(eventService.updateEvent(id, title, content, zipCode, streetAddress, detailAddress, startAt, endAt, multipartFile, principal.getName())); - }*/ + return ResponseEntity.ok(eventService.updateEvent(id, eventRequest, principal.getName())); + } @DeleteMapping("/{id}") diff --git a/src/main/java/ac/kr/deu/connect/luck/event/dto/EventRequestV2.kt b/src/main/java/ac/kr/deu/connect/luck/event/dto/EventRequestV2.kt index 2e14f93..2e0240a 100644 --- a/src/main/java/ac/kr/deu/connect/luck/event/dto/EventRequestV2.kt +++ b/src/main/java/ac/kr/deu/connect/luck/event/dto/EventRequestV2.kt @@ -1,6 +1,7 @@ package ac.kr.deu.connect.luck.event.dto import io.swagger.v3.oas.annotations.media.Schema +import org.springframework.format.annotation.DateTimeFormat import org.springframework.web.multipart.MultipartFile import java.time.LocalDateTime @@ -16,9 +17,11 @@ data class EventRequestV2( val streetAddress: String, @field:Schema(description = "상세 주소", example = "역삼동 123-456") val detailAddress: String, - @field:Schema(description = "이벤트 시작일", example = "2021-01-01T00:00:00") + @field:Schema(description = "이벤트 시작일", example = "2021-01-01T00:00:00Z") + @field:DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) val startAt: LocalDateTime, - @field:Schema(description = "이벤트 종료일", example = "2021-01-01T23:59:59") + @field:Schema(description = "이벤트 종료일", example = "2021-01-01T23:59:59Z") + @field:DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) val endAt: LocalDateTime, @field:Schema(description = "이미지", format = "binary") var image: MultipartFile?