Skip to content

Commit

Permalink
[COZY-190] feat: 방 생성 API 호출 시 방 로그 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
eple0329 authored Aug 20, 2024
1 parent 046ca86 commit 6d78890
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.cozymate.cozymate_server.domain.room.enums.RoomStatus;
import com.cozymate.cozymate_server.domain.room.repository.RoomRepository;
import com.cozymate.cozymate_server.domain.roomlog.repository.RoomLogRepository;
import com.cozymate.cozymate_server.domain.roomlog.service.RoomLogCommandService;
import com.cozymate.cozymate_server.domain.rule.repository.RuleRepository;
import com.cozymate.cozymate_server.domain.todo.repository.TodoRepository;
import com.cozymate.cozymate_server.global.response.code.status.ErrorStatus;
Expand Down Expand Up @@ -54,18 +55,21 @@ public class RoomCommandService {
private final FeedRepository feedRepository;
private final FriendRepository friendRepository;
private final RoomQueryService roomQueryService;
private final RoomLogCommandService roomLogCommandService;

public RoomCreateResponse createRoom(RoomCreateRequest request, Member member) {
Member creator = memberRepository.findById(member.getId())
.orElseThrow(() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));

if (roomRepository.existsByMemberIdAndStatuses(creator.getId(), RoomStatus.ENABLE, RoomStatus.WAITING)) {
if (roomRepository.existsByMemberIdAndStatuses(creator.getId(), RoomStatus.ENABLE,
RoomStatus.WAITING)) {
throw new GeneralException(ErrorStatus._ROOM_ALREADY_EXISTS);
}

String inviteCode = generateUniqueUppercaseKey();
Room room = RoomConverter.toEntity(request, inviteCode);
roomRepository.save(room);
room = roomRepository.save(room);
roomLogCommandService.addRoomLogCreationRoom(room);

Mate mate = MateConverter.toEntity(room, creator, true);
mateRepository.save(mate);
Expand All @@ -84,7 +88,8 @@ public void joinRoom(Long roomId, Long memberId) {
throw new GeneralException(ErrorStatus._ROOM_ALREADY_JOINED);
}

if (roomRepository.existsByMemberIdAndStatuses(memberId, RoomStatus.ENABLE, RoomStatus.WAITING)) {
if (roomRepository.existsByMemberIdAndStatuses(memberId, RoomStatus.ENABLE,
RoomStatus.WAITING)) {
throw new GeneralException(ErrorStatus._ROOM_ALREADY_EXISTS);
}

Expand All @@ -111,7 +116,7 @@ public void deleteRoom(Long roomId, Long memberId) {
.orElseThrow(() -> new GeneralException(ErrorStatus._NOT_ROOM_MATE));

Mate manager = mateRepository.findByRoomIdAndIsRoomManager(roomId, true)
.orElseThrow(()-> new GeneralException(ErrorStatus._ROOM_MANAGER_NOT_FOUND));
.orElseThrow(() -> new GeneralException(ErrorStatus._ROOM_MANAGER_NOT_FOUND));
if (!manager.getMember().getId().equals(memberId)) {
throw new GeneralException(ErrorStatus._NOT_ROOM_MANAGER);
}
Expand All @@ -127,7 +132,7 @@ public void deleteRoom(Long roomId, Long memberId) {
roomLogRepository.deleteByRoomId(roomId);

// 피드를 생성하지 않고 방이 삭제될 경우도 고려함
if(feedRepository.existsByRoomId(roomId)){
if (feedRepository.existsByRoomId(roomId)) {
Feed feed = feedRepository.findByRoomId(roomId);
List<Post> posts = postRepository.findByFeedId(feed.getId());
for (Post post : posts) {
Expand All @@ -138,7 +143,6 @@ public void deleteRoom(Long roomId, Long memberId) {
feedRepository.deleteByRoomId(roomId);
}


roomRepository.delete(room);
}

Expand All @@ -154,7 +158,7 @@ public void sendInvitation(Long roomId, List<Long> inviteeIdList, Long inviterId
.orElseThrow(() -> new GeneralException(ErrorStatus._NOT_ROOM_MATE));

Mate inviter = mateRepository.findByRoomIdAndIsRoomManager(roomId, true)
.orElseThrow(()-> new GeneralException(ErrorStatus._ROOM_MANAGER_NOT_FOUND));
.orElseThrow(() -> new GeneralException(ErrorStatus._ROOM_MANAGER_NOT_FOUND));
if (!inviter.getMember().getId().equals(inviterId)) {
throw new GeneralException(ErrorStatus._NOT_ROOM_MANAGER);
}
Expand All @@ -179,7 +183,8 @@ public void sendInvitation(Long roomId, List<Long> inviteeIdList, Long inviterId
}
}

if (roomRepository.existsByMemberIdAndStatuses(inviteeId, RoomStatus.ENABLE, RoomStatus.WAITING)) {
if (roomRepository.existsByMemberIdAndStatuses(inviteeId, RoomStatus.ENABLE,
RoomStatus.WAITING)) {
throw new GeneralException(ErrorStatus._ROOM_ALREADY_EXISTS);
}

Expand All @@ -202,16 +207,16 @@ public void respondToInviteRequest(Long roomId, Long inviteeId, boolean accept)
.orElseThrow(() -> new GeneralException(ErrorStatus._MEMBER_NOT_FOUND));

Room room = roomRepository.findById(roomId)
.orElseThrow(()-> new GeneralException(ErrorStatus._ROOM_NOT_FOUND));
.orElseThrow(() -> new GeneralException(ErrorStatus._ROOM_NOT_FOUND));

if (room.getNumOfArrival()+1 > room.getMaxMateNum()) {
if (room.getNumOfArrival() + 1 > room.getMaxMateNum()) {
throw new GeneralException(ErrorStatus._ROOM_FULL);
}

Mate invitee = mateRepository.findByRoomIdAndMemberId(roomId, inviteeId)
.orElseThrow(() -> new GeneralException(ErrorStatus._INVITATION_NOT_FOUND));

if (invitee.getEntryStatus()==EntryStatus.JOINED) {
if (invitee.getEntryStatus() == EntryStatus.JOINED) {
throw new GeneralException(ErrorStatus._ROOM_ALREADY_JOINED);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.cozymate.cozymate_server.domain.roomlog.service;

import com.cozymate.cozymate_server.domain.room.Room;
import com.cozymate.cozymate_server.domain.roomlog.RoomLog;
import com.cozymate.cozymate_server.domain.roomlog.converter.RoomLogConverter;
import com.cozymate.cozymate_server.domain.roomlog.repository.RoomLogRepository;
Expand All @@ -26,7 +27,9 @@ public class RoomLogCommandService {
"완료했어요! 얼른 칭찬해주세요!",
"완료하여, 최고의 cozy mate가 되었어요!"
);
public static final String DEFAULT_CREATION_MESSAGE = "의 역사적인 하루가 시작됐어요!";

// 투두 추가되었을 때
public void addRoomLogFromTodo(Todo todo) {
Optional<RoomLog> existingLog = roomLogRepository.findByTodoId(todo.getId());
// False일 때 기존 로그 삭제, True일 때 새로운 로그 생성
Expand All @@ -50,4 +53,10 @@ public void addRoomLogFromTodo(Todo todo) {

}

// 방 생성이 완료되었을 때 실행
public void addRoomLogCreationRoom(Room room) {
String content = "{" + room.getName() + "}" + DEFAULT_CREATION_MESSAGE;
roomLogRepository.save(RoomLogConverter.toEntity(content, room, null));
}

}

0 comments on commit 6d78890

Please sign in to comment.