From e077b9a4bc305abfd5aa9d7262448847da364467 Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 22 Dec 2024 12:33:55 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[refactor]=20=ED=8E=B8=EC=A7=80=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=20=EC=97=90=EB=9F=AC=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20?= =?UTF-8?q?=EC=B2=98=EB=A6=AC=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20Swagger?= =?UTF-8?q?=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blism/controller/LetterController.java | 267 ++++++++++++++---- .../example/blism/service/LetterService.java | 14 +- 2 files changed, 214 insertions(+), 67 deletions(-) diff --git a/src/main/java/com/example/blism/controller/LetterController.java b/src/main/java/com/example/blism/controller/LetterController.java index 4443e09..f01a865 100644 --- a/src/main/java/com/example/blism/controller/LetterController.java +++ b/src/main/java/com/example/blism/controller/LetterController.java @@ -30,29 +30,77 @@ public class LetterController { private final S3Service s3Service; @PostMapping(path = "", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) - @Operation(summary = "편지 생성", description = "이미지와 내용을 포함하여 새로운 편지를 생성합니다.", - parameters = { - @io.swagger.v3.oas.annotations.Parameter(name = "image", description = "이미지 파일", required = true, content = @Content(mediaType = MediaType.MULTIPART_FORM_DATA_VALUE)), - @io.swagger.v3.oas.annotations.Parameter(name = "createLetterRequestDTO", description = "편지 생성 요청 데이터", required = true, - content = @Content(mediaType = "application/json", - schema = @Schema(implementation = CreateLetterRequestDTO.class), - examples = @ExampleObject(name = "createLetterRequestExample", - value = "{\"senderId\":1,\"receiverId\":2,\"mailboxId\":3,\"doorDesign\":1,\"colorDesign\":2,\"decorationDesign\":3,\"content\":\"Hello World!\",\"font\":1,\"visibility\":1}"))) - }) - @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "편지 생성 성공", - content = @Content(schema = @Schema(implementation = ApiResponse.class), - examples = @ExampleObject(name = "successExample", value = "{\"status\":\"success\",\"data\":null}"))) - @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "편지 생성 실패", - content = @Content(schema = @Schema(implementation = ApiResponse.class), - examples = @ExampleObject(name = "failureExample", value = "{\"status\":\"failure\",\"message\":\"로직 수행 간 문제 발생\"}"))) + @Operation(summary = "편지 생성", description = "편지를 생성합니다.") + @io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "200", + description = "편지 생성 성공", + content = @Content( + schema = @Schema(implementation = ApiResponse.class), + examples = @ExampleObject( + name = "successExample", + value = "{\n" + + " \"isSuccess\": true,\n" + + " \"code\": 200,\n" + + " \"message\": \"성공입니다.\",\n" + + " \"result\": [\n" + + " ]\n" + + "}" + ) + ) + ) + @io.swagger.v3.oas.annotations.responses.ApiResponses({ + @io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "401", + description = "편지 생성 실패", + content = @Content( + schema = @Schema(implementation = ApiResponse.class), + examples = { + @ExampleObject( + name = "failureSenderExample", + value = "{\n" + + " \"isSuccess\": false,\n" + + " \"code\": 401,\n" + + " \"message\": \"보내는 사람이 없습니다.\",\n" + + " \"result\": null\n" + + "}" + ), + @ExampleObject( + name = "failureReceiverExample", + value = "{\n" + + " \"isSuccess\": false,\n" + + " \"code\": 401,\n" + + " \"message\": \"받는 사람이 없습니다.\",\n" + + " \"result\": null\n" + + "}" + ), + @ExampleObject( + name = "failureMailboxExample", + value = "{\n" + + " \"isSuccess\": false,\n" + + " \"code\": 401,\n" + + " \"message\": \"우체통이 존재하지 않습니다.\",\n" + + " \"result\": null\n" + + "}" + ) + } + ) + ) + }) + public ResponseEntity createLetter(@RequestPart("image") MultipartFile image, @RequestPart CreateLetterRequestDTO createLetterRequestDTO) { - boolean logicStatus = letterService.createLetter(image, createLetterRequestDTO); - if (logicStatus) { - return ResponseEntity.ok().body(ApiResponse.onSuccess(null)); - } else { - return ResponseEntity.ok().body(ApiResponse.onFailure(401, "로직 수행 간 문제 발생", null)); + String logicStatus = letterService.createLetter(image, createLetterRequestDTO); + if (logicStatus.equals("보내는 사람이 없습니다.")) { + return ResponseEntity.ok().body(ApiResponse.onFailure(401, "보내는 사람이 없습니다.", null)); } + else if (logicStatus.equals("받는 사람이 없습니다.")){ + return ResponseEntity.ok().body(ApiResponse.onFailure(401, "받는 사람이 없습니다.", null)); + } + else if (logicStatus.equals("우체통이 존재하지 않습니다.")){ + return ResponseEntity.ok().body(ApiResponse.onFailure(401, "우체통이 존재하지 않습니다.", null)); + } + + return ResponseEntity.ok().body(ApiResponse.onSuccess(null)); } @GetMapping("/{letterId}") @@ -64,24 +112,40 @@ public ResponseEntity createLetter(@RequestPart("image") MultipartF schema = @Schema(implementation = LetterResponseDTO.class), examples = @ExampleObject( name = "successExample", - value = """ - { - "letterId": 1, - "senderId": 1, - "receiverId": 2, - "content": "Hello!", - "photoUrl": "https://example.com/photo.jpg", - "font": 1, - "visibility": 1, - "createdAt": "2024-12-22T04:35:08.367236" - } - """ + value = "{\n" + + " \"isSuccess\": true,\n" + + " \"code\": 200,\n" + + " \"message\": \"편지 조회 성공\",\n" + + " \"result\": {\n" + + " \"letterId\": 1,\n" + + " \"senderId\": 1,\n" + + " \"receiverId\": 2,\n" + + " \"content\": \"Hello!\",\n" + + " \"photoUrl\": \"https://example.com/photo.jpg\",\n" + + " \"font\": 1,\n" + + " \"visibility\": 1,\n" + + " \"createdAt\": \"2024-12-22T04:35:08.367236\"\n" + + " }\n" + + "}" + ) + ) + ) + @io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "401", + description = "편지 조회 실패", + content = @Content( + schema = @Schema(implementation = ApiResponse.class), + examples = @ExampleObject( + name = "failureExample", + value = "{\n" + + " \"isSuccess\": false,\n" + + " \"code\": 401,\n" + + " \"message\": \"편지가 존재하지 않습니다.\",\n" + + " \"result\": null\n" + + "}" ) ) ) - @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "401", description = "편지 조회 실패", - content = @Content(schema = @Schema(implementation = ApiResponse.class), - examples = @ExampleObject(name = "failureExample", value = "{\"status\":\"failure\",\"message\":\"편지가 존재하지 않습니다.\"}"))) public ResponseEntity getLetters(@PathVariable Long letterId) { Letter letter = letterService.getLetter(letterId); @@ -107,19 +171,59 @@ public ResponseEntity getLetters(@PathVariable Long letterId) { @GetMapping("/{userId}/sent") @Operation(summary = "보낸 편지 목록 조회", description = "특정 사용자가 보낸 모든 편지를 조회합니다.") - @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "보낸 편지 조회 성공", - content = @Content(schema = @Schema(implementation = List.class), - examples = @ExampleObject(name = "successExample", - value = "[{" + - "\"letterId\":1," + - "\"senderId\":1," + - "\"receiverId\":2," + - "\"content\":\"Hello!\"," + - "\"photoUrl\":\"https://example.com/photo.jpg\"," + - "\"font\":1," + - "\"visibility\":1}," + - "\"createdAt\": \"2024-12-22T04:35:08.367236\"]" - ))) + @io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "200", + description = "보낸 편지 조회 성공", + content = @Content( + schema = @Schema(implementation = ApiResponse.class), + examples = @ExampleObject( + name = "successExample", + value = "{\n" + + " \"isSuccess\": true,\n" + + " \"code\": 200,\n" + + " \"message\": \"보낸 편지 조회 성공\",\n" + + " \"result\": [\n" + + " {\n" + + " \"letterId\": 1,\n" + + " \"senderId\": 1,\n" + + " \"receiverId\": 2,\n" + + " \"content\": \"Hello!\",\n" + + " \"photoUrl\": \"https://example.com/photo.jpg\",\n" + + " \"font\": 1,\n" + + " \"visibility\": 1,\n" + + " \"createdAt\": \"2024-12-22T04:35:08.367236\"\n" + + " },\n" + + " {\n" + + " \"letterId\": 2,\n" + + " \"senderId\": 1,\n" + + " \"receiverId\": 3,\n" + + " \"content\": \"How are you?\",\n" + + " \"photoUrl\": \"https://example.com/photo2.jpg\",\n" + + " \"font\": 2,\n" + + " \"visibility\": 1,\n" + + " \"createdAt\": \"2024-12-22T05:10:15.123456\"\n" + + " }\n" + + " ]\n" + + "}" + ) + ) + ) + @io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "401", + description = "보낸 편지 조회 실패", + content = @Content( + schema = @Schema(implementation = ApiResponse.class), + examples = @ExampleObject( + name = "failureExample", + value = "{\n" + + " \"isSuccess\": false,\n" + + " \"code\": 401,\n" + + " \"message\": \"사용자를 찾을 수 없습니다.\",\n" + + " \"result\": null\n" + + "}" + ) + ) + ) public ResponseEntity getSentLetters(@PathVariable Long userId) { List letters = letterService.getSentLetters(userId); @@ -128,19 +232,59 @@ public ResponseEntity getSentLetters(@PathVariable Long userId) { @GetMapping("/{userId}/received") @Operation(summary = "받은 편지 목록 조회", description = "특정 사용자가 받은 모든 편지를 조회합니다.") - @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "받은 편지 조회 성공", - content = @Content(schema = @Schema(implementation = List.class), - examples = @ExampleObject(name = "successExample", - value = "[{" + - "\"letterId\":1," + - "\"senderId\":1," + - "\"receiverId\":2," + - "\"content\":\"Hello!\"," + - "\"photoUrl\":\"https://example.com/photo.jpg\"," + - "\"font\":1," + - "\"visibility\":1}," + - "\"createdAt\": \"2024-12-22T04:35:08.367236\"]" - ))) + @io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "200", + description = "받은 편지 조회 성공", + content = @Content( + schema = @Schema(implementation = ApiResponse.class), + examples = @ExampleObject( + name = "successExample", + value = "{\n" + + " \"isSuccess\": true,\n" + + " \"code\": 200,\n" + + " \"message\": \"보낸 편지 조회 성공\",\n" + + " \"result\": [\n" + + " {\n" + + " \"letterId\": 1,\n" + + " \"senderId\": 1,\n" + + " \"receiverId\": 2,\n" + + " \"content\": \"Hello!\",\n" + + " \"photoUrl\": \"https://example.com/photo.jpg\",\n" + + " \"font\": 1,\n" + + " \"visibility\": 1,\n" + + " \"createdAt\": \"2024-12-22T04:35:08.367236\"\n" + + " },\n" + + " {\n" + + " \"letterId\": 2,\n" + + " \"senderId\": 1,\n" + + " \"receiverId\": 3,\n" + + " \"content\": \"How are you?\",\n" + + " \"photoUrl\": \"https://example.com/photo2.jpg\",\n" + + " \"font\": 2,\n" + + " \"visibility\": 1,\n" + + " \"createdAt\": \"2024-12-22T05:10:15.123456\"\n" + + " }\n" + + " ]\n" + + "}" + ) + ) + ) + @io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "401", + description = "받은 편지 조회 실패", + content = @Content( + schema = @Schema(implementation = ApiResponse.class), + examples = @ExampleObject( + name = "failureExample", + value = "{\n" + + " \"isSuccess\": false,\n" + + " \"code\": 401,\n" + + " \"message\": \"사용자를 찾을 수 없습니다.\",\n" + + " \"result\": null\n" + + "}" + ) + ) + ) public ResponseEntity getReceivedLetters(@PathVariable Long userId) { List letters = letterService.getReceivedLetters(userId); @@ -148,6 +292,7 @@ public ResponseEntity getReceivedLetters(@PathVariable Long userId) } @PutMapping(value = "/{letterId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + @Operation(summary = "편지 수정", description = "편지를 수정합니다.") @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "편지 수정 성공", content = @Content(schema = @Schema(implementation = ApiResponse.class), examples = @ExampleObject(name = "successExample", value = "{\"status\":\"success\",\"data\":null}"))) diff --git a/src/main/java/com/example/blism/service/LetterService.java b/src/main/java/com/example/blism/service/LetterService.java index 3bbb06d..892decd 100644 --- a/src/main/java/com/example/blism/service/LetterService.java +++ b/src/main/java/com/example/blism/service/LetterService.java @@ -1,5 +1,6 @@ package com.example.blism.service; +import com.example.blism.apiPayload.code.BaseErrorCode; import com.example.blism.domain.Letter; import com.example.blism.domain.Mailbox; import com.example.blism.domain.Member; @@ -25,26 +26,27 @@ public class LetterService { private final MailboxRepository mailboxRepository; private final S3Service s3Service; - public boolean createLetter(MultipartFile image, CreateLetterRequestDTO dto) { + public String createLetter(MultipartFile image, CreateLetterRequestDTO dto) { - String photoUrl = s3Service.upload(image); Optional sender = memberRepository.findById(dto.getSenderId()); Optional receiver = memberRepository.findById(dto.getReceiverId()); Optional mailbox = mailboxRepository.findById(dto.getMailboxId()); if (sender.isEmpty()) { - return false; + return "보내는 사람이 없습니다."; } if (receiver.isEmpty()) { - return false; + return "받는 사람이 없습니다."; } if(mailbox.isEmpty()){ - return false; + return "우체통이 존재하지 않습니다."; } + String photoUrl = s3Service.upload(image); + Letter letter = Letter.builder() .sender(sender.get()) .receiver(receiver.get()) @@ -60,7 +62,7 @@ public boolean createLetter(MultipartFile image, CreateLetterRequestDTO dto) { letterRepository.save(letter); - return true; + return "편지 생성 성공"; } public Letter getLetter(Long letterId) { From 21a88e08669eb779d74c2192db3b8d78fd3c4e7d Mon Sep 17 00:00:00 2001 From: unknown Date: Sun, 22 Dec 2024 12:43:30 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[refactor]=20=ED=8E=B8=EC=A7=80=20=EC=A1=B0?= =?UTF-8?q?=ED=9A=8C,=20=EC=88=98=EC=A0=95=20=EC=97=90=EB=9F=AC=20?= =?UTF-8?q?=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20?= =?UTF-8?q?Swagger=20=EC=BC=80=EC=9D=B4=EC=8A=A4=20=EC=9E=91=EC=84=B1=20?= =?UTF-8?q?=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=ED=99=95=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../blism/controller/LetterController.java | 86 +++++++++++++++---- 1 file changed, 69 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/example/blism/controller/LetterController.java b/src/main/java/com/example/blism/controller/LetterController.java index f01a865..f4e7410 100644 --- a/src/main/java/com/example/blism/controller/LetterController.java +++ b/src/main/java/com/example/blism/controller/LetterController.java @@ -4,6 +4,8 @@ import com.example.blism.domain.Letter; import com.example.blism.dto.request.CreateLetterRequestDTO; import com.example.blism.dto.response.LetterResponseDTO; +import com.example.blism.repository.LetterRepository; +import com.example.blism.repository.MemberRepository; import com.example.blism.service.LetterService; import com.example.blism.service.S3Service; import io.swagger.v3.oas.annotations.Operation; @@ -28,6 +30,8 @@ public class LetterController { private final LetterService letterService; private final S3Service s3Service; + private final MemberRepository memberRepository; + private final LetterRepository letterRepository; @PostMapping(path = "", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE}) @Operation(summary = "편지 생성", description = "편지를 생성합니다.") @@ -115,7 +119,7 @@ else if (logicStatus.equals("우체통이 존재하지 않습니다.")){ value = "{\n" + " \"isSuccess\": true,\n" + " \"code\": 200,\n" + - " \"message\": \"편지 조회 성공\",\n" + + " \"message\": \"성공입니다.\",\n" + " \"result\": {\n" + " \"letterId\": 1,\n" + " \"senderId\": 1,\n" + @@ -181,7 +185,7 @@ public ResponseEntity getLetters(@PathVariable Long letterId) { value = "{\n" + " \"isSuccess\": true,\n" + " \"code\": 200,\n" + - " \"message\": \"보낸 편지 조회 성공\",\n" + + " \"message\": \"성공입니다.\",\n" + " \"result\": [\n" + " {\n" + " \"letterId\": 1,\n" + @@ -225,9 +229,15 @@ public ResponseEntity getLetters(@PathVariable Long letterId) { ) ) public ResponseEntity getSentLetters(@PathVariable Long userId) { - List letters = letterService.getSentLetters(userId); - return ResponseEntity.ok().body(ApiResponse.onSuccess(letters)); + boolean memberExist = memberRepository.existsById(userId); + + if (memberExist) { + List letters = letterService.getSentLetters(userId); + return ResponseEntity.ok().body(ApiResponse.onSuccess(letters)); + } + + return ResponseEntity.ok().body(ApiResponse.onFailure(401, "사용자를 찾을 수 없습니다.", null)); } @GetMapping("/{userId}/received") @@ -242,7 +252,7 @@ public ResponseEntity getSentLetters(@PathVariable Long userId) { value = "{\n" + " \"isSuccess\": true,\n" + " \"code\": 200,\n" + - " \"message\": \"보낸 편지 조회 성공\",\n" + + " \"message\": \"성공입니다.\",\n" + " \"result\": [\n" + " {\n" + " \"letterId\": 1,\n" + @@ -286,31 +296,73 @@ public ResponseEntity getSentLetters(@PathVariable Long userId) { ) ) public ResponseEntity getReceivedLetters(@PathVariable Long userId) { - List letters = letterService.getReceivedLetters(userId); - return ResponseEntity.ok().body(ApiResponse.onSuccess(letters)); + boolean memberExist = memberRepository.existsById(userId); + + if (memberExist) { + List letters = letterService.getReceivedLetters(userId); + return ResponseEntity.ok().body(ApiResponse.onSuccess(letters)); + } + + return ResponseEntity.ok().body(ApiResponse.onFailure(401, "사용자를 찾을 수 없습니다.", null)); } @PutMapping(value = "/{letterId}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @Operation(summary = "편지 수정", description = "편지를 수정합니다.") - @io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "200", description = "편지 수정 성공", - content = @Content(schema = @Schema(implementation = ApiResponse.class), - examples = @ExampleObject(name = "successExample", value = "{\"status\":\"success\",\"data\":null}"))) + @io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "200", + description = "편지 수정 성공", + content = @Content( + schema = @Schema(implementation = ApiResponse.class), + examples = @ExampleObject( + name = "successExample", + value = "{\n" + + " \"isSuccess\": true,\n" + + " \"code\": 200,\n" + + " \"message\": \"성공입니다.\",\n" + + " \"result\": [\n" + + " ]\n" + + "}" + ) + ) + ) + @io.swagger.v3.oas.annotations.responses.ApiResponse( + responseCode = "401", + description = " 편지 수정 실패", + content = @Content( + schema = @Schema(implementation = ApiResponse.class), + examples = @ExampleObject( + name = "failureExample", + value = "{\n" + + " \"isSuccess\": false,\n" + + " \"code\": 401,\n" + + " \"message\": \"편지를 찾을 수 없습니다.\",\n" + + " \"result\": null\n" + + "}" + ) + ) + ) public ResponseEntity updateLetter(@RequestPart("image") MultipartFile image, @PathVariable Long letterId, @RequestPart CreateLetterRequestDTO createLetterRequestDTO) { String photoUrl = null; - Letter letter = letterService.getLetter(letterId); + boolean letterExist = letterRepository.existsById(letterId); - if (!image.isEmpty()) { - photoUrl = s3Service.upload(image); - } + if (letterExist) { + Letter letter = letterService.getLetter(letterId); - letter = letter.update(photoUrl, createLetterRequestDTO); + if (!image.isEmpty()) { + photoUrl = s3Service.upload(image); + } - letterService.updateLetter(letter); + letter = letter.update(photoUrl, createLetterRequestDTO); + letterService.updateLetter(letter); + + return ResponseEntity.ok().body(ApiResponse.onSuccess(null)); + } + + return ResponseEntity.ok().body(ApiResponse.onFailure(401, "편지를 찾을 수 없습니다.", null)); - return ResponseEntity.ok().body(ApiResponse.onSuccess(null)); } }