Skip to content

Commit

Permalink
[FEAT] 채팅관련 Entity 작성 및 매칭 생성, 업데이트, 삭제 로직 구현 (#54)
Browse files Browse the repository at this point in the history
* feat: Matching Entity 생성

* feat: isSuccess 필드 EnumType의 MatchingType으로 교체

* feat: ChatRoom Entity 생성

* feat: RoomState Enum 생성

* feat: ChatMessage Entity 및 MessageType Enum 생성

* feat: Matching 생성 관련 파일 작성

Matching Controller - Matching 생성 로직 작성 및, Matching 업데이트,
삭제 URL 선언
Matching Service - MatchingReqDto로 post, taker, giver Id를 받고
ChatRoom을 만들어 Matching 생성(test 데이터 부분 변경 필요)

* feat: ChatRoomRepository 선언

* feat: Matching Update 로직 작성

MatchingUpdateDto - matchingType을 받아옴
Matching - 주석 추가 (taker, giver)
MatchingController - update 로직 작성
MatchingService - update 로직 작성 및 게시글 작성 회원과 현재 로그인한
회원 일치 여부 확인 로직 작성

* feat: Matching 삭제 구현
  • Loading branch information
injae-348 authored Jul 7, 2024
1 parent 27357df commit e362d74
Show file tree
Hide file tree
Showing 12 changed files with 393 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package econo.buddybridge.chat.chatmessage.entity;

import econo.buddybridge.chat.chatroom.entity.ChatRoom;
import econo.buddybridge.common.persistence.BaseEntity;
import econo.buddybridge.member.entity.Member;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Table(name = "CHAT_MESSAGE")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ChatMessage extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "room_id")
private ChatRoom chatRoom;

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

private String content;

@Enumerated(EnumType.STRING)
private MessageType messageType;

@Builder
public ChatMessage(ChatRoom chatRoom, Member sender, String content, MessageType messageType){
this.chatRoom = chatRoom;
this.sender = sender;
this.content = content;
this.messageType = messageType;
}

public void updateChatMessage(String content,MessageType messageType){
this.content = content;
this.messageType = messageType;
}

public void deleteChatMessage(ChatMessage chatMessage){
chatMessage.messageType = MessageType.DELETE;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package econo.buddybridge.chat.chatmessage.entity;

import lombok.Getter;

@Getter
public enum MessageType {
JOIN("JOIN"),
CHAT("CHAT"),
LEAVE("LEAVE"),
DELETE("DELETE");

private final String messageType;

private MessageType(String messageType){
this.messageType = messageType;
}

}
41 changes: 41 additions & 0 deletions src/main/java/econo/buddybridge/chat/chatroom/entity/ChatRoom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package econo.buddybridge.chat.chatroom.entity;


import econo.buddybridge.common.persistence.BaseEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@Entity
@Getter
@NoArgsConstructor(access=AccessLevel.PROTECTED)
@Table(name="CHAT_ROOM")
public class ChatRoom extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Enumerated(EnumType.STRING)
private RoomState roomState;

private String lastMessage;

private LocalDateTime lastMessageTime;

@Builder
public ChatRoom(RoomState roomState, String lastMessage, LocalDateTime lastMessageTime){
this.roomState = roomState;
this.lastMessage = lastMessage;
this.lastMessageTime = lastMessageTime;
}

public void updateChatRoom(RoomState roomState,String lastMessage,LocalDateTime lastMessageTime){
this.roomState = roomState;
this.lastMessage = lastMessage;
this.lastMessageTime = lastMessageTime;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package econo.buddybridge.chat.chatroom.entity;

import lombok.Getter;

@Getter
public enum RoomState {
ACCEPT("ACCEPT"),
REJECT("REJECT");

private final String roomState;

private RoomState(String roomState){
this.roomState = roomState;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package econo.buddybridge.chat.chatroom.repository;

import econo.buddybridge.chat.chatroom.entity.ChatRoom;
import org.springframework.data.jpa.repository.JpaRepository;

public interface ChatRoomRepository extends JpaRepository<ChatRoom,Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package econo.buddybridge.matching.controller;

import econo.buddybridge.matching.dto.MatchingReqDto;
import econo.buddybridge.matching.dto.MatchingUpdateDto;
import econo.buddybridge.matching.service.MatchingService;
import econo.buddybridge.utils.api.ApiResponse;
import econo.buddybridge.utils.api.ApiResponseGenerator;
import econo.buddybridge.utils.session.SessionUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/matching")
public class MatchingController {

private final MatchingService matchingService;

@PostMapping("/accept")
public ApiResponse<ApiResponse.CustomBody<Long>> createMatching(
@RequestBody MatchingReqDto matchingReqDto,
HttpServletRequest request
){
Long memberId = SessionUtils.getMemberId(request);
Long createdMatchingId = matchingService.createMatchingById(matchingReqDto,memberId);
return ApiResponseGenerator.success(createdMatchingId,HttpStatus.OK);
}

// matching 완료 -> DONE 변경, 거절 -> FAILED 변경
@PutMapping("/{matching-id}")
public ApiResponse<ApiResponse.CustomBody<Long>> updateMatching(
@PathVariable("matching-id") Long matchingId,
@RequestBody MatchingUpdateDto matchingUpdateDto,
HttpServletRequest request
){
Long memberId = SessionUtils.getMemberId(request);
Long updatedMatchingId = matchingService.updateMatching(matchingId,matchingUpdateDto,memberId);
return ApiResponseGenerator.success(updatedMatchingId,HttpStatus.OK);
}

// matching 완전 삭제 -> 좋은 방법인가
// 실수로 제거한 경우는 채팅방을 다시 만들어야하는지
@DeleteMapping("/{matching-id}")
public ApiResponse<ApiResponse.CustomBody<Void>> deleteMatching(
@PathVariable("matching-id") Long matchingId,
HttpServletRequest request
){
Long memberId = SessionUtils.getMemberId(request);
matchingService.deleteMatching(matchingId,memberId);
return ApiResponseGenerator.success(HttpStatus.NO_CONTENT);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package econo.buddybridge.matching.dto;

public record MatchingReqDto(
Long postId,
Long takerId,
Long giverId
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package econo.buddybridge.matching.dto;

import econo.buddybridge.matching.entity.MatchingType;

public record MatchingUpdateDto(
MatchingType matchingType
) {
}
59 changes: 59 additions & 0 deletions src/main/java/econo/buddybridge/matching/entity/Matching.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package econo.buddybridge.matching.entity;

import econo.buddybridge.chat.chatroom.entity.ChatRoom;
import econo.buddybridge.common.persistence.BaseEntity;
import econo.buddybridge.member.entity.Member;
import econo.buddybridge.post.entity.Post;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@Table(name = "MATCHING")
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Matching extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "matching_id")
private Long id;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="post_id")
private Post post;

// 도움이 필요한 사람
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "taker_id")
private Member taker;

// 도움을 주는 사람
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "giver_id")
private Member giver;

// 채팅방 id 필드
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "room_id")
private ChatRoom chatRoom;

// 매칭 상태
private MatchingType matchingType;

@Builder
public Matching(Post post,Member taker, Member giver,ChatRoom chatRoom,MatchingType matchingType){
this.post = post;
this.taker = taker;
this.giver = giver;
this.chatRoom = chatRoom;
this.matchingType = matchingType;
}

// 매칭 상태 변경
public void updateMatching(MatchingType matchingType){
this.matchingType = matchingType;
}
}
16 changes: 16 additions & 0 deletions src/main/java/econo/buddybridge/matching/entity/MatchingType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package econo.buddybridge.matching.entity;

import lombok.Getter;

@Getter
public enum MatchingType {
DONE("DONE"),
FAILED("FAILED"),
PENDING("PENDING");

private final String matchingType;

private MatchingType(String matchingType){
this.matchingType = matchingType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package econo.buddybridge.matching.repository;

import econo.buddybridge.matching.entity.Matching;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MatchingRepository extends JpaRepository<Matching, Long> {

}
Loading

0 comments on commit e362d74

Please sign in to comment.