Skip to content

Commit

Permalink
Merge pull request #57 from UniBond-jijijin/fix/33-letter-transmissio…
Browse files Browse the repository at this point in the history
…n-interval

[fix/33-letter-transmission-interval] 편지 전송 간격 설정
  • Loading branch information
5jisoo authored Dec 31, 2023
2 parents 72a8df4 + bc97f92 commit 6c7a188
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableJpaAuditing
@EnableScheduling
public class UnibondApplication {

public static void main(String[] args) {
SpringApplication.run(UnibondApplication.class, args);
}
public static void main(String[] args) {
SpringApplication.run(UnibondApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
Expand All @@ -17,6 +18,7 @@

@Getter
@MappedSuperclass
@DynamicInsert
@EntityListeners(AuditingEntityListener.class)
public class BaseEntity {

Expand All @@ -29,5 +31,6 @@ public class BaseEntity {

@Enumerated(STRING)
@Setter
private BaseEntityStatus status = ACTIVE;
@Column(columnDefinition = "VARCHAR(255) DEFAULT 'ACTIVE'")
private BaseEntityStatus status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public enum BaseResponseStatus {
// letter (3300 ~ 3399)
NOT_ENOUGH_CHARS(false, 3300, "편지는 최소 50자 이상이어야 합니다."),
NOT_YOUR_LETTER(false, 3301, "해당 편지를 접근할 권한이 없습니다."),
CANT_SEND_LETTER(false, 3302, "아직 한 시간이 지나지 않았으므로 편지를 또 다시 보낼 수 없습니다."),
NOT_YET_ARRIVED(false, 3303, "아직 도착하지 않은 편지입니다."),

// letter_room (3400 ~ 3499)
NOT_YOUR_LETTER_ROOM(false, 3400, "해당 편지방에 접근할 권한이 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.unibond.unibond.letter.domain;

import com.unibond.unibond.common.BaseEntity;
import com.unibond.unibond.common.BaseException;
import com.unibond.unibond.common.BaseResponseStatus;
import com.unibond.unibond.letter_room.domain.LetterRoom;
import com.unibond.unibond.member.domain.Member;
import jakarta.persistence.*;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;

import java.time.LocalDateTime;

import static com.unibond.unibond.letter.domain.LetterStatus.*;
import static jakarta.persistence.EnumType.STRING;
import static jakarta.persistence.FetchType.LAZY;
import static jakarta.persistence.GenerationType.IDENTITY;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Getter
@DynamicInsert
@NoArgsConstructor(access = PROTECTED)
public class Letter extends BaseEntity {

Expand Down Expand Up @@ -47,7 +52,7 @@ public class Letter extends BaseEntity {

@Enumerated(STRING)
@Setter
@Column(columnDefinition = "varchar(10) default 'SENDING'")
@Column(columnDefinition = "VARCHAR(255) DEFAULT 'SENDING'")
private LetterStatus letterStatus;

@Builder
Expand All @@ -67,4 +72,18 @@ public Letter(Member receiver, Member sender, LetterRoom letterRoom, String cont
public LocalDateTime getArrivalDate() {
return this.getCreatedDate().plusHours(1);
}

public void checkIsArrived() throws BaseException {
if (this.letterStatus.equals(SENDING)) {
throw new BaseException(BaseResponseStatus.NOT_YET_ARRIVED);
}
}

public Boolean isSender(Long senderId) {
return this.sender.getId().equals(senderId);
}

public Boolean isReceiver(Long receiverId) {
return this.receiver.getId().equals(receiverId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.time.LocalDateTime;
import java.util.Optional;

@Repository
Expand All @@ -33,4 +34,15 @@ Optional<Letter> findByReceiverAndLetterId(@Param("receiver") Member receiver,
"order by l.createdDate desc")
Page<Letter> findLettersByLetterRoomAndReceiverOrSender(@Param("letterRoom") Long letterRoomId,
@Param("participant") Long participantId, Pageable pageable);

@Query("select case when count(l)> 0 then true else false end from Letter l " +
"where (l.sender.id = :sender and l.receiver.id = :receiver) " +
"and l.createdDate > :currentTimeMinusOneHour ")
Boolean hasSentLetterToSamePersonWithinHour(@Param("sender") Long senderId, @Param("receiver") Long receiverId,
@Param("currentTimeMinusOneHour") LocalDateTime currentTimeMinusOneHour);

@Modifying
@Query("update Letter l set l.letterStatus = 'ARRIVED' " +
"where l.createdDate <= :currentTimeMinusOneHour and l.letterStatus = 'SENDING'")
void bulkSendLetter(@Param("currentTimeMinusOneHour") LocalDateTime currentTime);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.unibond.unibond.letter.service;

import com.unibond.unibond.letter.repository.LetterRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
public class LetterScheduler {
private final LetterRepository letterRepository;

@Transactional
@Scheduled(cron = "0 0,30 * * * *")
public void sendLetter() {
letterRepository.bulkSendLetter(LocalDateTime.now().minusHours(1L));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Optional;

import static com.unibond.unibond.common.BaseResponseStatus.*;
Expand All @@ -34,13 +35,12 @@ public class LetterService {
public SendLetterResDto sendLetter(SendLetterReqDto reqDto) throws BaseException {
try {
checkLetterLength(reqDto);
checkSendingCapability(loginInfoService.getLoginMemberId(), reqDto.getReceiverId());

Member sender = loginInfoService.getLoginMember();
Member receiver = getReceiver(reqDto.getReceiverId());

LetterRoom letterRoom = findLetterRoom(sender, receiver);
letterRoomRepository.save(letterRoom);

Letter letter = reqDto.toEntity(letterRoom, sender, receiver);
Letter savedLetter = letterRepository.save(letter);

Expand All @@ -58,7 +58,7 @@ public LetterLikeResDto likeLetter(Long letterId) throws BaseException {
try {
Member loginMember = loginInfoService.getLoginMember();
Letter letter = findLetterByIdAndReceiver(loginMember, letterId);
// TODO: 도착하는 시간 확인 필요
letter.checkIsArrived();
letter.setLiked(!letter.getLiked());
return new LetterLikeResDto(letter);
} catch (BaseException e) {
Expand All @@ -74,12 +74,12 @@ public LetterDetailResDto getLetterDetail(Long letterId) throws BaseException {
Letter letter = letterRepository.findById(letterId).orElseThrow(() -> new BaseException(INVALID_LETTER_ID));
Long loginMemberId = loginInfoService.getLoginMemberId();

if (letter.getReceiver().getId().equals(loginMemberId)) {
if (letter.isReceiver(loginMemberId)) {
letter.checkIsArrived();
return LetterDetailResDto.getReceivedLetter(letter);
} else if (letter.getSender().getId().equals(loginMemberId)) {
} else if (letter.isSender(loginMemberId)) {
return LetterDetailResDto.getSentLetter(letter);
}

throw new BaseException(NOT_YOUR_LETTER);
} catch (BaseException e) {
throw e;
Expand All @@ -101,14 +101,26 @@ private Letter findLetterByIdAndReceiver(Member receiver, Long id) throws BaseEx

private LetterRoom findLetterRoom(Member sender, Member receiver) {
Optional<LetterRoom> letterRoom = letterRoomRepository.findLetterRoomBy2Member(sender, receiver);
return letterRoom.orElseGet(
() -> new LetterRoom(sender, receiver)
);
if (letterRoom.isPresent()) {
return letterRoom.get();
} else {
LetterRoom newLetterRoom = new LetterRoom(sender, receiver);
letterRoomRepository.save(newLetterRoom);
return newLetterRoom;
}
}

private void checkLetterLength(SendLetterReqDto reqDto) throws BaseException {
if (reqDto.getContent().length() < 50) {
throw new BaseException(BaseResponseStatus.NOT_ENOUGH_CHARS);
}
}

private void checkSendingCapability(Long senderId, Long receiverId) throws BaseException {
System.out.println(LocalDateTime.now().minusHours(1L));
Boolean result = letterRepository.hasSentLetterToSamePersonWithinHour(senderId, receiverId, LocalDateTime.now().minusHours(1L));
if (result) {
throw new BaseException(CANT_SEND_LETTER);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,6 @@ public GetLetterRoomDetailResDto getAllLetters(Long letterRoomId, Pageable pagea
}
}

private Member findAnotherParticipant(Letter letter, Long loginId) throws BaseException {
if (letter.getSender().getId().equals(loginId)) {
return letter.getReceiver();
} else if (letter.getReceiver().getId().equals(loginId)) {
return letter.getSender();
}
throw new BaseException(NOT_YOUR_LETTER_ROOM);
}

public GetAllLetterRoomsResDto getAllLetterRooms(Pageable pageable) throws BaseException {
try {
Long loginMemberId = loginInfoService.getLoginMemberId();
Expand All @@ -66,4 +57,13 @@ public GetAllLetterRoomsResDto getAllLetterRooms(Pageable pageable) throws BaseE
throw new BaseException(DATABASE_ERROR);
}
}

private Member findAnotherParticipant(Letter letter, Long loginId) throws BaseException {
if (letter.getSender().getId().equals(loginId)) {
return letter.getReceiver();
} else if (letter.getReceiver().getId().equals(loginId)) {
return letter.getSender();
}
throw new BaseException(NOT_YOUR_LETTER_ROOM);
}
}

0 comments on commit 6c7a188

Please sign in to comment.