Skip to content

Commit

Permalink
Merge pull request #665 from woowacourse-teams/fix/#653
Browse files Browse the repository at this point in the history
chat 작성자 이름 오류 및 chatroom 조회 오류 수정
  • Loading branch information
ay-eonii authored Oct 17, 2024
2 parents 4a2f21d + 2a55bd9 commit 04507dd
Show file tree
Hide file tree
Showing 36 changed files with 395 additions and 211 deletions.
15 changes: 4 additions & 11 deletions backend/src/main/java/mouda/backend/bet/domain/Bet.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
import java.util.List;
import java.util.Random;

import org.springframework.http.HttpStatus;

import lombok.Builder;
import lombok.Getter;
import mouda.backend.bet.exception.BetErrorMessage;
import mouda.backend.bet.exception.BetException;

@Getter
public class Bet {
Expand Down Expand Up @@ -53,13 +49,6 @@ private boolean isParticipated(Long id) {
.anyMatch(participant -> participant.getId() == id);
}

public long getLoserId() {
if (loserId == null) {
throw new BetException(HttpStatus.NOT_FOUND, BetErrorMessage.LOSER_NOT_FOUND);
}
return loserId;
}

public boolean isLoser(long otherId) {
return loserId == otherId;
}
Expand All @@ -75,4 +64,8 @@ public String getTitle() {
public long timeDifferenceInMinutesWithNow() {
return betDetails.timeDifferenceInMinutesWithNow();
}

public boolean canNotParticipate() {
return hasLoser() || betDetails.pastBettingTime();
}
}
66 changes: 39 additions & 27 deletions backend/src/main/java/mouda/backend/bet/domain/BetDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,43 @@
@Getter
public class BetDetails {

private final Long id;
private final String title;
private final BettingTime bettingTime;

@Builder
public BetDetails(Long id, String title, LocalDateTime bettingTime) {
this.id = id;
this.title = title;
this.bettingTime = new BettingTime(bettingTime);
}

public static BetDetails create(String title, int waitingMinutes) {
LocalDateTime bettingTime = LocalDateTime.now().plusMinutes(waitingMinutes);

return BetDetails.builder()
.title(title)
.bettingTime(bettingTime)
.build();
}

public LocalDateTime getBettingTime() {
return bettingTime.getBettingTime();
}

public long timeDifferenceInMinutesWithNow() {
return bettingTime.timeDifferenceInMinutesWithNow();
}
private final long id;
private final String title;
private final BettingTime bettingTime;
private final long moimerId;
private final Long loserId;

@Builder
public BetDetails(long id, String title, LocalDateTime bettingTime, long moimerId, Long loserId) {
this.id = id;
this.title = title;
this.bettingTime = new BettingTime(bettingTime);
this.moimerId = moimerId;
this.loserId = loserId;
}

public static BetDetails create(String title, int waitingMinutes) {
LocalDateTime bettingTime = LocalDateTime.now().plusMinutes(waitingMinutes);

return BetDetails.builder()
.title(title)
.bettingTime(bettingTime)
.build();
}

public LocalDateTime getBettingTime() {
return bettingTime.getBettingTime();
}

public long timeDifferenceInMinutesWithNow() {
return bettingTime.timeDifferenceInMinutesWithNow();
}

public boolean hasLoser() {
return loserId != null;
}

public boolean pastBettingTime() {
return bettingTime.isPast();
}
}
28 changes: 16 additions & 12 deletions backend/src/main/java/mouda/backend/bet/domain/BettingTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@
@Getter
public class BettingTime {

private static final int NO_SECONDS = 0;
private static final int NO_NANOS = 0;
private static final int NO_SECONDS = 0;
private static final int NO_NANOS = 0;

private final LocalDateTime bettingTime;
private final LocalDateTime bettingTime;

public BettingTime(LocalDateTime bettingTime) {
this.bettingTime = normalize(bettingTime);
}
public BettingTime(LocalDateTime bettingTime) {
this.bettingTime = normalize(bettingTime);
}

private LocalDateTime normalize(LocalDateTime bettingTime) {
return bettingTime.withSecond(NO_SECONDS).withNano(NO_NANOS);
}
private LocalDateTime normalize(LocalDateTime bettingTime) {
return bettingTime.withSecond(NO_SECONDS).withNano(NO_NANOS);
}

public long timeDifferenceInMinutesWithNow() {
return Math.abs(ChronoUnit.MINUTES.between(normalize(LocalDateTime.now()), this.bettingTime));
}
public long timeDifferenceInMinutesWithNow() {
return Math.abs(ChronoUnit.MINUTES.between(normalize(LocalDateTime.now()), this.bettingTime));
}

public boolean isPast() {
return LocalDateTime.now().isAfter(bettingTime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.persistence.UniqueConstraint;
import lombok.Getter;
import lombok.NoArgsConstructor;
import mouda.backend.bet.domain.BetRole;
import mouda.backend.darakbangmember.domain.DarakbangMember;

@Entity
@Getter
@NoArgsConstructor
@Table(name = "bet_darakbang_member")
@Table(
name = "bet_darakbang_member",
uniqueConstraints = {
@UniqueConstraint(
columnNames = {"bet_id", "darakbang_member_id"}
)
}
)
public class BetDarakbangMemberEntity {

@Id
Expand All @@ -40,4 +49,11 @@ public BetDarakbangMemberEntity(DarakbangMember darakbangMember, BetEntity bet)
public void updateLastChat(Long lastReadChatId) {
this.lastReadChatId = lastReadChatId;
}

public String getRole(long moimerId) {
if (darakbangMember.getId() == moimerId) {
return BetRole.MOIMER.name();
}
return BetRole.MOIMEE.name();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public class BetEntity {
private long moimerId;

@Builder
private BetEntity(Long id, String title, LocalDateTime bettingTime, Long loserDarakbangMemberId, long darakbangId, long moimerId) {
private BetEntity(Long id, String title, LocalDateTime bettingTime, Long loserDarakbangMemberId, long darakbangId,
long moimerId) {
this.id = id;
this.title = title;
this.bettingTime = bettingTime;
Expand Down Expand Up @@ -74,6 +75,7 @@ public BetDetails toBetDetails() {
.id(id)
.title(title)
.bettingTime(bettingTime)
.loserId(loserDarakbangMemberId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public enum BetErrorMessage {

BET_DARAKBANG_MEMBER_NOT_FOUND("참여하지 않은 안내면진다입니다."),
BET_NOT_FOUND("안내면진다가 존재하지 않습니다."),
LOSER_NOT_FOUND("당첨자가 존재하지 않습니다.");

LOSER_NOT_FOUND("당첨자가 존재하지 않습니다."),
CAN_NOT_PARTICIPATE("참여할 수 없는 안내면진다입니다."),
ALREADY_PARTICIPATED_BET("이미 참여한 안내면진다입니다.");

private final String message;
}
18 changes: 15 additions & 3 deletions backend/src/main/java/mouda/backend/bet/implement/BetWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

Expand All @@ -21,6 +22,7 @@ public class BetWriter {

private final BetRepository betRepository;
private final BetDarakbangMemberRepository betDarakbangMemberRepository;
private final BetFinder betFinder;

public void saveAll(List<Bet> bets) {
List<BetEntity> betEntities = bets.stream()
Expand All @@ -36,11 +38,21 @@ public long save(long darakbangId, Bet bet) {
}

public void participate(long darakbangId, long betId, DarakbangMember darakbangMember) {
BetEntity betEntity = betRepository.findByIdAndDarakbangId(betId, darakbangId)
.orElseThrow(() -> new BetException(HttpStatus.NOT_FOUND, BetErrorMessage.BET_NOT_FOUND));
Bet bet = betFinder.find(darakbangId, betId);
if (bet.canNotParticipate()) {
throw new BetException(HttpStatus.BAD_REQUEST, BetErrorMessage.CAN_NOT_PARTICIPATE);
}
participate(darakbangMember, bet);
}

private void participate(DarakbangMember darakbangMember, Bet bet) {
BetEntity betEntity = BetEntity.from(bet);
BetDarakbangMemberEntity betDarakbangMemberEntity = new BetDarakbangMemberEntity(darakbangMember, betEntity);
betDarakbangMemberRepository.save(betDarakbangMemberEntity);
try {
betDarakbangMemberRepository.save(betDarakbangMemberEntity);
} catch (DataIntegrityViolationException e) {
throw new BetException(HttpStatus.BAD_REQUEST, BetErrorMessage.ALREADY_PARTICIPATED_BET);
}
}

public void updateLoser(Bet bet) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import org.springframework.transaction.annotation.Transactional;

import lombok.RequiredArgsConstructor;
import mouda.backend.chat.domain.ChatOwnership;
import mouda.backend.chat.domain.ChatRoom;
import mouda.backend.chat.domain.ChatWithAuthor;
import mouda.backend.chat.domain.Chats;
import mouda.backend.chat.implement.ChatRoomFinder;
import mouda.backend.chat.implement.ChatWriter;
Expand Down Expand Up @@ -59,7 +59,7 @@ public ChatFindUnloadedResponse findUnloadedChats(
ChatRoom chatRoom = chatRoomFinder.read(darakbangId, chatRoomId, darakbangMember);

Chats chats = chatRoomFinder.findAllUnloadedChats(chatRoom.getId(), recentChatId);
List<ChatWithAuthor> chatsWithAuthor = chats.getChatsWithAuthor(darakbangMember);
List<ChatOwnership> chatsWithAuthor = chats.getChatsWithAuthor(darakbangMember);

return ChatFindUnloadedResponse.toResponse(chatsWithAuthor);
}
Expand Down
19 changes: 19 additions & 0 deletions backend/src/main/java/mouda/backend/chat/domain/Author.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mouda.backend.chat.domain;

import lombok.Builder;
import lombok.Getter;

@Getter
public class Author {

private final long id;
private final String nickname;
private final String profile;

@Builder
public Author(long id, String nickname, String profile) {
this.id = id;
this.nickname = nickname;
this.profile = profile;
}
}
12 changes: 5 additions & 7 deletions backend/src/main/java/mouda/backend/chat/domain/Chat.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,28 @@
import lombok.Builder;
import lombok.Getter;
import mouda.backend.chat.entity.ChatType;
import mouda.backend.darakbangmember.domain.DarakbangMember;

@Getter
public class Chat {

private final long id;
private final String content;
private final DarakbangMember darakbangMember;
private final Author author;
private final LocalDate date;
private final LocalTime time;
private final ChatType chatType;

@Builder
public Chat(long id, String content, DarakbangMember darakbangMember, LocalDate date, LocalTime time,
ChatType chatType) {
public Chat(long id, String content, Author author, LocalDate date, LocalTime time, ChatType chatType) {
this.id = id;
this.content = content;
this.darakbangMember = darakbangMember;
this.author = author;
this.date = date;
this.time = time;
this.chatType = chatType;
}

public boolean isMyMessage(long darakbangMemberId) {
return darakbangMember.getId() == darakbangMemberId;
public boolean isMine(long darakbangMemberId) {
return author.getId() == darakbangMemberId;
}
}
15 changes: 15 additions & 0 deletions backend/src/main/java/mouda/backend/chat/domain/ChatOwnership.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package mouda.backend.chat.domain;

import lombok.Getter;

@Getter
public class ChatOwnership {

private final Chat chat;
private final boolean isMine;

public ChatOwnership(Chat chat, boolean isMine) {
this.chat = chat;
this.isMine = isMine;
}
}
21 changes: 8 additions & 13 deletions backend/src/main/java/mouda/backend/chat/domain/ChatRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,24 @@
import java.time.LocalDateTime;

import lombok.Getter;
import mouda.backend.chat.entity.ChatEntity;
import mouda.backend.chat.entity.ChatRoomEntity;

@Getter
public class ChatRoom {

private final Long id;
private final long id;
private final long targetId;
private final ChatRoomType type;
private final LastChat lastChat;

public ChatRoom(ChatRoomEntity chatRoomEntity, ChatEntity lastChat) {
this.id = chatRoomEntity.getId();
this.targetId = chatRoomEntity.getTargetId();
this.type = chatRoomEntity.getType();
this.lastChat = new LastChat(lastChat.getDateTime(), lastChat.getContent());
public ChatRoom(Long id, long targetId, ChatRoomType type, LastChat lastChat) {
this.id = id;
this.targetId = targetId;
this.type = type;
this.lastChat = lastChat;
}

public ChatRoom(ChatRoomEntity chatRoomEntity) {
this.id = chatRoomEntity.getId();
this.targetId = chatRoomEntity.getTargetId();
this.type = chatRoomEntity.getType();
this.lastChat = LastChat.empty();
public ChatRoom(Long id, long targetId, ChatRoomType type) {
this(id, targetId, type, LastChat.empty());
}

public boolean isMoim() {
Expand Down

This file was deleted.

Loading

0 comments on commit 04507dd

Please sign in to comment.