Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…024-mouda into feature/#666
  • Loading branch information
Mingyum-Kim committed Oct 18, 2024
2 parents ec54e73 + c689e82 commit dbdcc30
Show file tree
Hide file tree
Showing 38 changed files with 405 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import mouda.backend.auth.exception.AuthErrorMessage;
import mouda.backend.auth.exception.AuthException;
import mouda.backend.member.domain.Member;
import mouda.backend.member.domain.OauthType;

@Component
public class AccessTokenProvider {

public static final String MEMBER_ID_CLAIM_KEY = "id";
public static final String SOCIAL_LOGIN_ID_CLAIM_KEY = "socialLoginId";
private static final String MEMBER_ID_CLAIM_KEY = "id";
private static final String SOCIAL_LOGIN_ID_CLAIM_KEY = "socialLoginId";
private static final String OAUTH_TYPE = "oauthType";

@Value("${security.jwt.token.secret-key}")
private String secretKey;
Expand All @@ -33,6 +35,7 @@ public String provide(Member member) {
return Jwts.builder()
.claim(MEMBER_ID_CLAIM_KEY, member.getId())
.claim(SOCIAL_LOGIN_ID_CLAIM_KEY, member.getSocialLoginId())
.claim(OAUTH_TYPE, member.getOauthType())
.setIssuedAt(now)
.setExpiration(validity)
.signWith(SignatureAlgorithm.HS256, secretKey)
Expand All @@ -56,12 +59,15 @@ public Claims getPayload(String token) {
}
}

public void validateExpiration(String token) {
public void validateToken(String token) {
Claims claims = getPayload(token);

if (claims.getExpiration().before(new Date())) {
throw new AuthException(HttpStatus.UNAUTHORIZED, AuthErrorMessage.UNAUTHORIZED);
}
if (claims.get(OAUTH_TYPE).equals(OauthType.KAKAO.toString())) {
throw new AuthException(HttpStatus.UNAUTHORIZED, AuthErrorMessage.UNAUTHORIZED);
}
}
}

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;
}
}
Loading

0 comments on commit dbdcc30

Please sign in to comment.