Skip to content

Commit

Permalink
Merge pull request #74 from Team-UMC/feature/#42/message
Browse files Browse the repository at this point in the history
[FEAT] 쪽지 API 구현
  • Loading branch information
junseokkim authored Feb 18, 2024
2 parents 468dfcc + 3aa3a83 commit abad03f
Show file tree
Hide file tree
Showing 17 changed files with 637 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package com.umc.networkingService.domain.message.controller;


import com.umc.networkingService.config.security.auth.CurrentMember;
import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.domain.message.dto.request.MessageRequest;
import com.umc.networkingService.domain.message.dto.response.MessageResponse;
import com.umc.networkingService.domain.message.facade.MessageFacade;
import com.umc.networkingService.global.common.base.BaseResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@Slf4j
@Tag(name = "채팅 API", description = "채팅 관련 API")
@RestController
@Validated
@RequestMapping("/messages")
@RequiredArgsConstructor
public class MessageController {


private final MessageFacade messageFacade;

@Operation(summary = "쪽지 작성 API",description = "쪽지 작성 API")
@PostMapping("/{messageRoomId}")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "MESSAGE002", description = "존재하지 않는 쪽지방"),
})
public BaseResponse<MessageResponse.MessageId>
postMessage(
@CurrentMember Member member,
@PathVariable UUID messageRoomId,
@RequestBody MessageRequest.Message message
){
return BaseResponse.onSuccess(messageFacade.postMessage(member, messageRoomId, message));
}

@Operation(summary = "쪽지 수정 API",description = "쪽지 수정 API")
@PatchMapping("/{messageId}")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "MESSAGE001", description = "존재하지 않는 쪽지")
})
public BaseResponse<MessageResponse.MessageId>
patchMessage(
@CurrentMember Member member,
@PathVariable UUID messageId,
@RequestBody MessageRequest.Message message
){
return BaseResponse.onSuccess(messageFacade.patchMessage(member, messageId, message));
}

@Operation(summary = "쪽지 삭제 API",description = "쪽지 삭제 API")
@DeleteMapping("/{messageId}")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "MESSAGE001", description = "존재하지 않는 쪽지")
})
public BaseResponse<MessageResponse.MessageId>
deleteMessage(
@CurrentMember Member member,
@PathVariable UUID messageId
){
return BaseResponse.onSuccess(messageFacade.deleteMessage(member, messageId));
}

@Operation(summary = "쪽지 상세 조회 API",description = "쪽지 상세 조회 API (페이지 0부터 시작) (isAnonymous가 true일 경우 익명인 메시지))")
@GetMapping("/{messageRoomId}")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "MESSAGE002", description = "존재하지 않는 쪽지방"),
@ApiResponse(responseCode = "COMMON500", description = "페이지 에러, 0<=page를 입력해주세요")
})
public BaseResponse<MessageResponse.JoinMessages>
joinMessages(
@CurrentMember Member member,
@PathVariable UUID messageRoomId,
@RequestParam(name= "page") int page //페이징 처리
){
return BaseResponse.onSuccess(messageFacade.joinMessages(member, messageRoomId, page));
}

@Operation(summary = "쪽지함 조회 API",description = "쪽지함 조회 API (isAnonymous가 true일 경우 상대가 익명인 메시지)")
@GetMapping("")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "MESSAGE005", description = "최신 쪽지를 찾을 수 없습니다."),
})
public BaseResponse<MessageResponse.JoinMessageRooms>
joinMessageRooms(
@CurrentMember Member member
){
return BaseResponse.onSuccess(messageFacade.joinMessageRooms(member));
}

@Operation(summary = "쪽지 시작하기 API",description = "쪽지 시작하기 API")
@PostMapping("")
@ApiResponses( value = {
@ApiResponse(responseCode = "COMMON200", description = "성공"),
@ApiResponse(responseCode = "MESSAGE003", description = "이미 존재하는 쪽지방"),
@ApiResponse(responseCode = "MESSAGE004", description = "보내는 메시지가 없음")
})
public BaseResponse<MessageResponse.MessageRoomId>
startMessage(
@CurrentMember Member member,
@RequestBody MessageRequest.StartMessageRoom messageRoom
){
return BaseResponse.onSuccess(messageFacade.createMessageRoom(member, messageRoom.getMessageRoomUserId(), messageRoom.getIsAnonymous(), messageRoom.getMessageContent()));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.umc.networkingService.domain.message.dto.request;

import com.umc.networkingService.global.common.enums.Semester;
import lombok.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

public class MessageRequest {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor // 쪽지 생성, 수정
public static class Message {
//todo: 글자수 제한
String message;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor // 쪽지방 생성
public static class StartMessageRoom{
@NonNull
UUID messageRoomUserId;
Boolean isAnonymous; //기본적으로 실명
@NonNull
String messageContent;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.umc.networkingService.domain.message.dto.response;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.UUID;

public class MessageResponse {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinMessageRooms { //쪽지함 조회
List<JoinMessageRoom> messageRooms;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinMessageRoom{
UUID messageRoomId;
UUID messageRoomUserId;
String messageRoomUserName;
String recentMessage;
String recentMessageTime;
Boolean isAnonymous;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinMessages { //쪽지 조회
List<JoinMessage> JoinMessage;
UUID messageRoomId;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class JoinMessage
{
UUID messageId;
String message;
String messageTime;
String messageMemberName;
UUID messageMemberId;
Boolean isAnonymous; //해당 메시지의 주인이 익명인지
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class MessageRoomId
{
UUID messageRoomId;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class MessageId
{
UUID messageId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@

import com.umc.networkingService.global.common.base.BaseEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.annotations.UuidGenerator;

import java.util.UUID;

@Getter
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor(access= AccessLevel.PROTECTED)
@SQLRestriction("deleted_at is null")
public class Message extends BaseEntity {
Expand All @@ -30,4 +30,8 @@ public class Message extends BaseEntity {
@Column(nullable = false)
private Boolean isSender;

public void updateContent(String content) {
this.content = content;
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.umc.networkingService.domain.message.entity;

import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.global.common.base.BaseEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.*;
import org.hibernate.annotations.SQLRestriction;
import org.hibernate.annotations.UuidGenerator;

import java.util.UUID;

@Getter
@Entity
@Builder
@AllArgsConstructor
@NoArgsConstructor(access= AccessLevel.PROTECTED)
@SQLRestriction("deleted_at is null")
public class MessageRoom extends BaseEntity {
Expand All @@ -22,13 +23,24 @@ public class MessageRoom extends BaseEntity {
private UUID id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn
private Message sender;
@JoinColumn(name = "sender_id")
private Member sender;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn
private Message receiver;
@JoinColumn(name = "receiver_id")
private Member receiver;

@Column(nullable = false)
private Boolean isAnonymous;
}

/*
1. 쪽지를 보내기 시작할 떄, 처음 보낸 사람이 익명이라면, 받는 사람에게는 보낸 사람이 익명으로 뜸
2. 익명으로 보낸 사람 입장에서는 받는 사람이 실명으로 뜸
3. 그럼,A 입장에서는 A가 B에게 익명으로 쪽지를 보낸 채팅방, A와 B가 실명으로 쪽지를 주고받는 채팅방이 따로 생길 수도 있음
4. A가 B에게 익명인 채팅방은 하나만 생성됨 (A가 sender 일떄, isAnonymous = true) isAnonymous = true -> receiver 입장에서 sender가 익명
5. A가 B에게 실명인 채팅방은 하나만 생성됨 (A가 sender 일떄, isAnonymous = false)
6. A가 receiver 일떄, isAnonymous = false인 채팅방이 2개가 생김
-> 필드 바꿔야함
(그럼 A가 )
*/
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.umc.networkingService.domain.message.facade;

import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.domain.message.dto.request.MessageRequest;
import com.umc.networkingService.domain.message.dto.response.MessageResponse;
import com.umc.networkingService.domain.message.service.MessageRoomService;
import com.umc.networkingService.domain.message.service.MessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
@RequiredArgsConstructor
public class MessageFacade {

private final MessageService messageService;
private final MessageRoomService messageRoomService;

public MessageResponse.MessageId postMessage(Member sender, UUID messageRoomId, MessageRequest.Message message) {
return messageService.postMessage(sender, messageRoomId, message, messageRoomService);
}

public MessageResponse.MessageId patchMessage(Member member, UUID messageId, MessageRequest.Message message) {
return messageService.patchMessage(member, messageId, message);
}

public MessageResponse.MessageId deleteMessage(Member member, UUID messageId) {
return messageService.deleteMessage(member, messageId);
}

public MessageResponse.JoinMessages joinMessages(Member member, UUID messageRoomId, int page) {
return messageService.joinMessages(member, messageRoomId, page, messageRoomService);
}

public MessageResponse.MessageRoomId createMessageRoom(Member member, UUID receiverId, boolean isAnonymous, String content){
MessageResponse.MessageRoomId resposnse = messageRoomService.createMessageRoom(member, receiverId, isAnonymous, content);
messageService.postMessage(member, resposnse.getMessageRoomId(), MessageRequest.Message.builder().message(content).build(), messageRoomService);
return resposnse;
}

public MessageResponse.JoinMessageRooms joinMessageRooms(Member member) {
return messageRoomService.joinMessageRooms(member, messageService);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.umc.networkingService.domain.message.mapper;

import com.umc.networkingService.domain.member.entity.Member;
import com.umc.networkingService.domain.message.dto.response.MessageResponse;
import com.umc.networkingService.domain.message.entity.Message;
import com.umc.networkingService.domain.message.entity.MessageRoom;

import java.time.format.DateTimeFormatter;


public class MessageMapper {

//쪽지방 리스트 조회ㅓ
public static MessageResponse.JoinMessageRoom toJoinMessageRoom(MessageRoom messageRoom, Message recentMessage, Member messageRoomUser){
return MessageResponse.JoinMessageRoom.builder()
.messageRoomId(messageRoom.getId())
.messageRoomUserId(messageRoomUser.getId())
.messageRoomUserName(messageRoomUser.getNickname()+"/"+messageRoomUser.getName())
.recentMessage(recentMessage.getContent())
.recentMessageTime(
recentMessage.getCreatedAt().format(DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss"))
)
.isAnonymous(messageRoom.getIsAnonymous()&&messageRoom.getSender().getId().equals(messageRoomUser.getId()))
.build();

}
}
Loading

0 comments on commit abad03f

Please sign in to comment.