diff --git a/src/main/java/com/example/blism/controller/LetterController.java b/src/main/java/com/example/blism/controller/LetterController.java index 4443e09..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,31 +30,81 @@ 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 = "이미지와 내용을 포함하여 새로운 편지를 생성합니다.", - 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 +116,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,65 +175,194 @@ 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); - 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") @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); - 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) - @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}"))) + @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.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)); } } 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) {