From e484c4db0d762132cc9894576ed5d4d1ab047928 Mon Sep 17 00:00:00 2001 From: dd-jiyun Date: Thu, 29 Feb 2024 00:19:28 +0900 Subject: [PATCH 1/2] feat: Message Details MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 선택한 사용자와 서로 주고 받은 메세지 조회 기능을 구현합니다. --- .../message/controller/MessageController.java | 61 ++++++++++++++++--- .../titto_backend/message/domain/Message.java | 5 +- .../titto_backend/message/dto/MessageDTO.java | 26 ++++++++ .../message/repository/MessageRepository.java | 8 +++ .../message/service/MessageService.java | 41 ++++++++++++- 5 files changed, 130 insertions(+), 11 deletions(-) diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java index 6d2e64d..bf5e0f3 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/controller/MessageController.java @@ -13,6 +13,7 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; @@ -36,8 +37,40 @@ public class MessageController { @ApiResponse(responseCode = "500", description = "관리자 문의") }) public ResponseEntity writeMessage(@RequestBody MessageDTO.Request request, - @AuthenticationPrincipal UserDetails userDetails) { - return ResponseEntity.status(HttpStatus.CREATED).body(messageService.writeMessage(request, userDetails.getUsername())); + @AuthenticationPrincipal UserDetails userDetails) { + return ResponseEntity.status(HttpStatus.CREATED) + .body(messageService.writeMessage(request, userDetails.getUsername())); + } + + //쪽지함 전체 조회 + @GetMapping("/all") + @Operation( + summary = "전체 메시지 조회", + description = "전체 메시지를 조회합니다.", + responses = { + @ApiResponse(responseCode = "200", description = "요청 성공"), + @ApiResponse(responseCode = "400", description = "잘못된 요청"), + @ApiResponse(responseCode = "500", description = "관리자 문의") + }) + public ResponseEntity> getAllMessagePreview( + @AuthenticationPrincipal UserDetails userDetails) { + return new ResponseEntity<>(messageService.getAllMessagePreview(userDetails.getUsername()), HttpStatus.OK); + } + + //쪽지함 세부 조회 ( 주고 받은 사용자와의 대화 내용을 뿌려줄 수 있는 api) + @GetMapping("/{selectedUserId}") + @Operation( + summary = "메시지 세부 조회", + description = "메시지 세부를 조회합니다.", + responses = { + @ApiResponse(responseCode = "200", description = "요청 성공"), + @ApiResponse(responseCode = "400", description = "잘못된 요청"), + @ApiResponse(responseCode = "500", description = "관리자 문의") + }) + public ResponseEntity> getBothMessages ( + @PathVariable Long selectedUserId, + @AuthenticationPrincipal UserDetails userDetails) { + return new ResponseEntity<>(messageService.getBothMessages(userDetails.getUsername(),selectedUserId), HttpStatus.OK); } @GetMapping("/receiver") @@ -49,7 +82,8 @@ public ResponseEntity writeMessage(@RequestBody MessageDTO.Request reque @ApiResponse(responseCode = "400", description = "잘못된 요청"), @ApiResponse(responseCode = "500", description = "관리자 문의") }) - public ResponseEntity> getMessagesByReceiver(@AuthenticationPrincipal UserDetails userDetails) { + public ResponseEntity> getMessagesByReceiver( + @AuthenticationPrincipal UserDetails userDetails) { return new ResponseEntity<>(messageService.getMessagesByReceiver(userDetails.getUsername()), HttpStatus.OK); } @@ -62,13 +96,26 @@ public ResponseEntity> getMessagesByReceiver(@Authenti @ApiResponse(responseCode = "400", description = "잘못된 요청"), @ApiResponse(responseCode = "500", description = "관리자 문의") }) - public ResponseEntity> getMessagesBySender(@AuthenticationPrincipal UserDetails userDetails) { + public ResponseEntity> getMessagesBySender( + @AuthenticationPrincipal UserDetails userDetails) { return new ResponseEntity<>(messageService.getMessagesBySender(userDetails.getUsername()), HttpStatus.OK); } - //메세지 삭제 (sender인 유저가 receiver유저와 한 대화내용을 삭제) - //쪽지함 전체 조회 - //쪽지함 세부 조회 ( 주고 받은 사용자와의 대화 내용을 뿌려줄 수 있는 api) + //TODO : 메세지 삭제 (sender인 유저가 receiver유저와 한 대화내용을 삭제) +// @DeleteMapping("/{messageId}") +// @Operation( +// summary = "메시지 삭제", +// description = "메시지를 삭제합니다.", +// responses = { +// @ApiResponse(responseCode = "200", description = "요청 성공"), +// @ApiResponse(responseCode = "400", description = "잘못된 요청"), +// @ApiResponse(responseCode = "500", description = "관리자 문의") +// }) +// public ResponseEntity deleteMessage(@PathVariable Long messageId, +// @AuthenticationPrincipal UserDetails userDetails) { +// messageService.deleteMessage(messageId, userDetails.getUsername()); +// return ResponseEntity.ok("메시지 삭제 성공"); +// } } diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/domain/Message.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/domain/Message.java index e4870fb..0b42f98 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/domain/Message.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/domain/Message.java @@ -2,7 +2,6 @@ import com.example.titto_backend.auth.domain.User; import com.example.titto_backend.common.BaseEntity; -import jakarta.persistence.CascadeType; import jakarta.persistence.Column; import jakarta.persistence.Entity; import jakarta.persistence.FetchType; @@ -32,11 +31,11 @@ public class Message extends BaseEntity { @Column(name = "message_id") private Long id; - @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "sender_id") private User sender; - @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "receiver_id") private User receiver; diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/dto/MessageDTO.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/dto/MessageDTO.java index 2f85f32..d4e3165 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/dto/MessageDTO.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/dto/MessageDTO.java @@ -60,5 +60,31 @@ public Response(Message message) { this.senderNickname = message.getSenderNickname(); } } + + @Data + @JsonInclude(JsonInclude.Include.NON_NULL) + @Schema(description = "메세지함 조회") + public static class Preview { + @Schema(description = "메시지 ID") + private Long id; + + @Schema(description = "내용") + private String content; + + @Schema(description = "보낸 사람 닉네임") + private String senderNickname; + + @Schema(description = "보낸 시간") + private String sentAt; + + public Preview(Message message) { + this.id = message.getId(); + this.content = message.getContent(); + this.senderNickname = message.getSenderNickname(); + this.sentAt = message.getSentAt().toString(); + } + } + + } diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/repository/MessageRepository.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/repository/MessageRepository.java index bb9fadc..7d3bcfd 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/repository/MessageRepository.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/repository/MessageRepository.java @@ -9,4 +9,12 @@ public interface MessageRepository extends JpaRepository { List findAllByReceiver(User user); List findAllBySender(User user); + + // user id를 받고 receiver이거나 sender인 메시지를 조회 + List findAllByReceiverOrSender(User receiver, User sender); + + // selectedUserId를 받고 자신과 서로 메시지를 주고받은 메시지를 조회 + List findAllBySenderAndReceiverOrReceiverAndSender(User sender, User receiver, User receiver2, + User sender2); + // isDeleted가 false인 메시지만 조회 } diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java b/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java index 9cd1ffb..a86855d 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/message/service/MessageService.java @@ -40,7 +40,28 @@ public String writeMessage(MessageDTO.Request request, String email) throws Cust } @Transactional - public List getMessagesByReceiver(String email) { + public List getBothMessages(String email, Long selectedUserId) { + User user = userRepository.findByEmail(email) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + User selectedUser = userRepository.findById(selectedUserId) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + List messages = messageRepository.findAllBySenderAndReceiverOrReceiverAndSender(user, selectedUser, user, selectedUser); + return convertMessagesToDTO(messages); + } + + @Transactional + public List getAllMessagePreview(String email) { + User user = userRepository.findByEmail(email) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + + List messages = messageRepository.findAllByReceiver(user); + return convertMessagesToPreviewDTO(messages); + } + + @Transactional + public List getMessagesByReceiver(String email) { User receiver = userRepository.findByEmail(email) .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); @@ -48,6 +69,7 @@ public List getMessagesByReceiver(String email) { return convertMessagesToDTO(messages); } + @Transactional public List getMessagesBySender(String email) { User sender = userRepository.findByEmail(email) @@ -57,10 +79,27 @@ public List getMessagesBySender(String email) { return convertMessagesToDTO(messages); } + /**1 : 로그인한 사용자와 관련된 메세지인지 + 2 : sender 인지 receiver 인지 + 3 : isDeleted 가 false 인지 true 인지 **/ + @Transactional + public void deleteMessage(Long messageId, String email) { + User user = userRepository.findByEmail(email) + .orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)); + messageRepository.deleteById(messageId); + } + private List convertMessagesToDTO(List messages) { return messages.stream() .map(MessageDTO.Response::new) .toList(); } + + private List convertMessagesToPreviewDTO(List messages) { + return messages.stream() + .map(MessageDTO.Preview::new) + .toList(); + } + } From 7250b361f30bcb9de75bf320848003f7e59edf6a Mon Sep 17 00:00:00 2001 From: dd-jiyun Date: Fri, 1 Mar 2024 22:42:06 +0900 Subject: [PATCH 2/2] feat: Add Swagger Production Server MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit swagger에 local과 production url 목록 추가 --- .../example/titto_backend/auth/config/SwaggerConfig.java | 7 +++++++ Titto_Backend/src/main/resources/application.yml | 2 ++ 2 files changed, 9 insertions(+) diff --git a/Titto_Backend/src/main/java/com/example/titto_backend/auth/config/SwaggerConfig.java b/Titto_Backend/src/main/java/com/example/titto_backend/auth/config/SwaggerConfig.java index e2ebd7b..b84e403 100644 --- a/Titto_Backend/src/main/java/com/example/titto_backend/auth/config/SwaggerConfig.java +++ b/Titto_Backend/src/main/java/com/example/titto_backend/auth/config/SwaggerConfig.java @@ -5,12 +5,17 @@ import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; +import io.swagger.v3.oas.models.servers.Server; +import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class SwaggerConfig { + @Value("${server}") + private String serverUrl; + @Bean public OpenAPI openAPI() { Info info = new Info() @@ -33,6 +38,8 @@ public OpenAPI openAPI() { ); return new OpenAPI() + .addServersItem(new Server().url("http://localhost:8080").description("Local Server")) + .addServersItem(new Server().url(serverUrl).description("Production Server")) .addSecurityItem(securityRequirement) .components(components) .info(info); diff --git a/Titto_Backend/src/main/resources/application.yml b/Titto_Backend/src/main/resources/application.yml index 1f4a3d5..d6953ec 100644 --- a/Titto_Backend/src/main/resources/application.yml +++ b/Titto_Backend/src/main/resources/application.yml @@ -32,6 +32,8 @@ spring: port: 6379 host: redis +server: ${PRODUCTION_SERVER_URL} + # JWT jwt: secret: ${JWT_SECRET}