Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] 참여 코드 관련 서비스를 만료 가능한 버전으로 변경 #455

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package harustudy.backend.config;

import harustudy.backend.room.domain.CodeGenerationStrategy;
import harustudy.backend.room.domain.GenerationStrategy;
import harustudy.backend.participantcode.domain.CodeGenerationStrategy;
import harustudy.backend.participantcode.domain.GenerationStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
@Getter
public class ParticipantCode {

private Long id;
// private Long id;

// @OneToOne
private final Long pomodoroRoomId;

@Transient
private GenerationStrategy generationStrategy;
Expand All @@ -22,8 +25,8 @@ public class ParticipantCode {
private LocalDateTime createdDate;


public ParticipantCode(GenerationStrategy generationStrategy) {
this.id = null;
public ParticipantCode(Long pomodoroRoomId, GenerationStrategy generationStrategy) {
this.pomodoroRoomId = pomodoroRoomId;
this.generationStrategy = generationStrategy;
this.code = generationStrategy.generate();
this.expirationPeriod = generationStrategy.EXPIRATION_PERIOD_IN_SECONDS;
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,10 @@
import harustudy.backend.room.exception.PomodoroTotalCycleException;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -39,10 +36,6 @@ public class PomodoroRoom extends BaseTimeEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "participant_code_id")
private ParticipantCode participantCode;

@NotNull
@Column(length = 10)
private String name;
Expand All @@ -57,12 +50,11 @@ public class PomodoroRoom extends BaseTimeEntity {
private List<PomodoroProgress> pomodoroProgresses = new ArrayList<>();

public PomodoroRoom(@NotNull String name, @NotNull Integer totalCycle,
@NotNull Integer timePerCycle, @NotNull ParticipantCode participantCode) {
@NotNull Integer timePerCycle) {
validate(name, totalCycle, timePerCycle);
this.totalCycle = totalCycle;
this.timePerCycle = timePerCycle;
this.name = name;
this.participantCode = participantCode;
}

private void validate(String name, Integer totalCycle, Integer timePerCycle) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package harustudy.backend.room.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import harustudy.backend.room.domain.ParticipantCode;
import harustudy.backend.participantcode.domain.ParticipantCode;
import harustudy.backend.room.domain.PomodoroRoom;

public record CreatePomodoroRoomResponse(@JsonIgnore Long studyId, String participantCode) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package harustudy.backend.room.repository;

import harustudy.backend.room.domain.ParticipantCode;
import harustudy.backend.room.domain.PomodoroRoom;
import java.util.List;
import harustudy.backend.room.exception.RoomNotFoundException;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PomodoroRoomRepository extends JpaRepository<PomodoroRoom, Long> {

// TODO: Optional로 변경
List<PomodoroRoom> findByParticipantCode(ParticipantCode participantCode);

default PomodoroRoom findByIdIfExists(Long id) {
return findById(id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
import harustudy.backend.member.domain.Member;
import harustudy.backend.member.exception.MemberNotFoundException;
import harustudy.backend.member.repository.MemberRepository;
import harustudy.backend.participantcode.domain.GenerationStrategy;
import harustudy.backend.participantcode.domain.ParticipantCode;
import harustudy.backend.participantcode.repository.InMemoryParticipantCodeRepository;
import harustudy.backend.progress.domain.PomodoroProgress;
import harustudy.backend.progress.repository.PomodoroProgressRepository;
import harustudy.backend.room.domain.GenerationStrategy;
import harustudy.backend.room.domain.ParticipantCode;
import harustudy.backend.room.domain.PomodoroRoom;
import harustudy.backend.room.dto.CreatePomodoroRoomRequest;
import harustudy.backend.room.dto.CreatePomodoroRoomResponse;
import harustudy.backend.room.dto.PomodoroRoomResponse;
import harustudy.backend.room.dto.PomodoroRoomsResponse;
import harustudy.backend.room.exception.ParticipantCodeNotFoundException;
import harustudy.backend.room.exception.RoomNotFoundException;
import harustudy.backend.room.repository.ParticipantCodeRepository;
import harustudy.backend.room.repository.PomodoroRoomRepository;
import java.util.List;
import java.util.Objects;
Expand All @@ -29,9 +29,9 @@ public class PomodoroRoomService {

private final PomodoroRoomRepository pomodoroRoomRepository;
private final PomodoroProgressRepository pomodoroProgressRepository;
private final ParticipantCodeRepository participantCodeRepository;
private final MemberRepository memberRepository;
private final GenerationStrategy generationStrategy;
private final InMemoryParticipantCodeRepository participantCodeRepository;

public PomodoroRoomResponse findPomodoroRoom(Long roomId) {
return PomodoroRoomResponse.from(pomodoroRoomRepository.findById(roomId)
Expand All @@ -42,11 +42,9 @@ public PomodoroRoomsResponse findPomodoroRoomWithFilter(Long memberId, String co
if (Objects.nonNull(code)) {
ParticipantCode participantCode = participantCodeRepository.findByCode(code)
.orElseThrow(ParticipantCodeNotFoundException::new);
List<PomodoroRoom> pomodoroRooms = pomodoroRoomRepository.findByParticipantCode(
participantCode);
validateIsPresent(pomodoroRooms);

return PomodoroRoomsResponse.from(pomodoroRooms);
Long pomodoroRoomId = participantCode.getPomodoroRoomId();
PomodoroRoom pomodoroRoom = pomodoroRoomRepository.findByIdIfExists(pomodoroRoomId);
return PomodoroRoomsResponse.from(List.of(pomodoroRoom));
}
if (Objects.nonNull(memberId)) {
return findPomodoroRoomByMemberId(memberId);
Expand All @@ -55,12 +53,6 @@ public PomodoroRoomsResponse findPomodoroRoomWithFilter(Long memberId, String co
return PomodoroRoomsResponse.from(pomodoroRoomRepository.findAll());
}

private void validateIsPresent(List<PomodoroRoom> pomodoroRooms) {
if (pomodoroRooms.isEmpty()) {
throw new RoomNotFoundException();
}
}

private PomodoroRoomsResponse findPomodoroRoomByMemberId(Long memberId) {
Member member = memberRepository.findById(memberId)
.orElseThrow(MemberNotFoundException::new);
Expand All @@ -78,18 +70,17 @@ private List<PomodoroRoom> mapToPomodoroRooms(List<PomodoroProgress> pomodoroPro
}

public CreatePomodoroRoomResponse createPomodoroRoom(CreatePomodoroRoomRequest request) {
ParticipantCode participantCode = regenerateUniqueCode();
participantCodeRepository.save(participantCode);

PomodoroRoom pomodoroRoom = new PomodoroRoom(request.name(), request.totalCycle(),
request.timePerCycle(), participantCode);
request.timePerCycle());
PomodoroRoom savedRoom = pomodoroRoomRepository.save(pomodoroRoom);
ParticipantCode participantCode = generateUniqueCode(pomodoroRoom.getId());
participantCodeRepository.save(participantCode);

return CreatePomodoroRoomResponse.from(savedRoom, participantCode);
}

private ParticipantCode regenerateUniqueCode() {
ParticipantCode participantCode = new ParticipantCode(generationStrategy);
private ParticipantCode generateUniqueCode(Long pomodoroRoomId) {
ParticipantCode participantCode = new ParticipantCode(pomodoroRoomId, generationStrategy);
while (isParticipantCodePresent(participantCode)) {
participantCode.regenerate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import harustudy.backend.member.domain.LoginType;
import harustudy.backend.member.domain.Member;
import harustudy.backend.progress.domain.PomodoroProgress;
import harustudy.backend.room.domain.CodeGenerationStrategy;
import harustudy.backend.room.domain.ParticipantCode;
import harustudy.backend.room.domain.PomodoroRoom;
import java.util.List;
import java.util.Map;
Expand All @@ -34,12 +32,10 @@ class PomodoroContentRepositoryTest {

@BeforeEach
void setUp() {
ParticipantCode participantCode = new ParticipantCode(new CodeGenerationStrategy());
PomodoroRoom pomodoroRoom = new PomodoroRoom("roomName", 3, 20, participantCode);
PomodoroRoom pomodoroRoom = new PomodoroRoom("roomName", 3, 20);
Member member = new Member("member", "email", "imageUrl", LoginType.GUEST);
pomodoroProgress = new PomodoroProgress(pomodoroRoom, member, "nickname");

testEntityManager.persist(participantCode);
testEntityManager.persist(pomodoroRoom);
testEntityManager.persist(member);
testEntityManager.persist(pomodoroProgress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,9 @@
import harustudy.backend.content.dto.WriteRetrospectRequest;
import harustudy.backend.member.domain.LoginType;
import harustudy.backend.member.domain.Member;
import harustudy.backend.member.exception.MemberNotFoundException;
import harustudy.backend.progress.domain.PomodoroProgress;
import harustudy.backend.progress.domain.PomodoroStatus;
import harustudy.backend.progress.exception.PomodoroProgressNotFoundException;
import harustudy.backend.progress.exception.PomodoroProgressStatusException;
import harustudy.backend.room.domain.CodeGenerationStrategy;
import harustudy.backend.room.domain.ParticipantCode;
import harustudy.backend.room.domain.PomodoroRoom;
import harustudy.backend.room.exception.RoomNotFoundException;
import jakarta.persistence.EntityManager;
Expand Down Expand Up @@ -51,13 +47,11 @@ class PomodoroContentServiceTest {

@BeforeEach
void setUp() {
ParticipantCode participantCode = new ParticipantCode(new CodeGenerationStrategy());
pomodoroRoom = new PomodoroRoom("roomName", 1, 20, participantCode);
pomodoroRoom = new PomodoroRoom("roomName", 1, 20);
member = new Member("nickname", "email", "imageUrl", LoginType.GUEST);
pomodoroProgress = new PomodoroProgress(pomodoroRoom, member, "nickname");
pomodoroContent = new PomodoroContent(pomodoroProgress, 1);

entityManager.persist(participantCode);
entityManager.persist(pomodoroRoom);
entityManager.persist(member);
entityManager.persist(pomodoroProgress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import harustudy.backend.member.dto.MemberResponse;
import harustudy.backend.progress.domain.PomodoroProgress;
import harustudy.backend.room.domain.CodeGenerationStrategy;
import harustudy.backend.room.domain.ParticipantCode;
import harustudy.backend.room.domain.PomodoroRoom;
import java.nio.charset.StandardCharsets;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -29,8 +27,7 @@ class MemberIntegrationTest extends IntegrationTest {
void setUp() {
super.setUp();

ParticipantCode participantCode = new ParticipantCode(new CodeGenerationStrategy());
room = new PomodoroRoom("roomName", 1, 20, participantCode);
room = new PomodoroRoom("roomName", 1, 20);

memberDto1 = createMember("member1");
memberDto2 = createMember("member2");
Expand All @@ -40,7 +37,6 @@ void setUp() {
PomodoroProgress pomodoroProgress2 = new PomodoroProgress(room, memberDto2.member(),
"name2");

entityManager.persist(participantCode);
entityManager.persist(room);
entityManager.persist(pomodoroProgress1);
entityManager.persist(pomodoroProgress2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import harustudy.backend.content.dto.WritePlanRequest;
import harustudy.backend.content.dto.WriteRetrospectRequest;
import harustudy.backend.progress.domain.PomodoroProgress;
import harustudy.backend.room.domain.CodeGenerationStrategy;
import harustudy.backend.room.domain.ParticipantCode;
import harustudy.backend.room.domain.PomodoroRoom;
import java.nio.charset.StandardCharsets;
import java.util.List;
Expand All @@ -40,13 +38,11 @@ public class PomodoroContentIntegrationTest extends IntegrationTest {
void setUp() {
super.setUp();

ParticipantCode participantCode = new ParticipantCode(new CodeGenerationStrategy());
pomodoroRoom = new PomodoroRoom("roomName", 2, 20, participantCode);
pomodoroRoom = new PomodoroRoom("roomName", 2, 20);
memberDto = createMember("member1");
pomodoroProgress = new PomodoroProgress(pomodoroRoom, memberDto.member(), "nickname");
pomodoroContent = new PomodoroContent(pomodoroProgress, 1);

entityManager.persist(participantCode);
entityManager.persist(pomodoroRoom);
entityManager.persist(pomodoroProgress);
entityManager.persist(pomodoroContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
import harustudy.backend.progress.domain.PomodoroProgress;
import harustudy.backend.progress.domain.PomodoroStatus;
import harustudy.backend.progress.dto.PomodoroProgressResponse;
import harustudy.backend.room.domain.CodeGenerationStrategy;
import harustudy.backend.room.domain.ParticipantCode;
import harustudy.backend.room.domain.PomodoroRoom;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
Expand All @@ -31,13 +29,10 @@ class PomodoroProgressIntegrationTest extends IntegrationTest {
@BeforeEach
void setUp() {
super.setUp();
ParticipantCode participantCode = new ParticipantCode(new CodeGenerationStrategy());
pomodoroRoom = new PomodoroRoom("roomName", 3, 20,
participantCode);
pomodoroRoom = new PomodoroRoom("roomName", 3, 20);
memberDto = createMember("member1");
pomodoroProgress = new PomodoroProgress(pomodoroRoom, memberDto.member(), "nickname");

entityManager.persist(participantCode);
entityManager.persist(pomodoroRoom);
entityManager.persist(pomodoroProgress);

Expand Down
Loading