Skip to content
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.debatetimer.domain.customize.CustomizeTable;
import com.debatetimer.domain.customize.CustomizeTimeBox;
import com.debatetimer.domain.customize.CustomizeTimeBoxEntities;
import com.debatetimer.entity.customize.CustomizeTimeBoxEntities;
import com.debatetimer.domain.member.Member;
import jakarta.validation.Valid;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.debatetimer.domain.customize.CustomizeTable;
import com.debatetimer.domain.customize.CustomizeTimeBox;
import com.debatetimer.domain.customize.CustomizeTimeBoxEntities;
import com.debatetimer.entity.customize.CustomizeTimeBoxEntities;
import com.debatetimer.entity.customize.CustomizeTimeBoxEntity;
import java.util.List;

Expand All @@ -16,13 +16,6 @@ public CustomizeTableResponse(
toTimeBoxResponses(customizeTimeBoxes));
}

public CustomizeTableResponse(
CustomizeTable customizeTable,
List<CustomizeTimeBoxResponse> timeBoxResponses
) {
this(customizeTable.getId(), new CustomizeTableInfoResponse(customizeTable), timeBoxResponses);
}

private static List<CustomizeTimeBoxResponse> toTimeBoxResponses(CustomizeTimeBoxEntities timeBoxes) {
List<CustomizeTimeBoxEntity> customizeTimeBoxes = timeBoxes.getTimeBoxes();
return customizeTimeBoxes
Expand All @@ -31,13 +24,11 @@ private static List<CustomizeTimeBoxResponse> toTimeBoxResponses(CustomizeTimeBo
.toList();
}

public static CustomizeTableResponse ofDomain(CustomizeTable customizeTable,
List<CustomizeTimeBox> customizeTimeBoxes) { // TODO 정팩매 -> 생성자로 전환
return new CustomizeTableResponse(
customizeTable.getId(),
public CustomizeTableResponse(CustomizeTable customizeTable,
List<CustomizeTimeBox> customizeTimeBoxes) {
this(customizeTable.getId(),
new CustomizeTableInfoResponse(customizeTable),
toTimeBoxResponses(customizeTimeBoxes)
);
toTimeBoxResponses(customizeTimeBoxes));
}

private static List<CustomizeTimeBoxResponse> toTimeBoxResponses(List<CustomizeTimeBox> customizeTimeBoxes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.debatetimer.domain.customize.CustomizeTimeBox;
import com.debatetimer.domain.customize.Stance;
import com.debatetimer.entity.customize.CustomizeTimeBoxEntity;
import java.util.Collections;
import java.util.List;

public record CustomizeTimeBoxResponse(
Expand All @@ -32,19 +31,6 @@ public CustomizeTimeBoxResponse(CustomizeTimeBoxEntity customizeTimeBox) {
);
}

public CustomizeTimeBoxResponse(CustomizeTimeBoxEntity customizeTimeBox, List<BellResponse> bell) {
this(
customizeTimeBox.getStance(),
customizeTimeBox.getSpeechType(),
customizeTimeBox.getBoxType(),
convertTime(customizeTimeBox),
bell,
customizeTimeBox.getTimePerTeam(),
customizeTimeBox.getTimePerSpeaking(),
customizeTimeBox.getSpeaker()
);
}

private static Integer convertTime(CustomizeTimeBoxEntity customizeTimeBox) {
if (customizeTimeBox.getBoxType() == CustomizeBoxType.TIME_BASED) {
return null;
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/debatetimer/entity/customize/BellEntity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.debatetimer.entity.customize;

import com.debatetimer.domain.customize.Bell;
import com.debatetimer.exception.custom.DTClientErrorException;
import com.debatetimer.exception.errorcode.ClientErrorCode;
import jakarta.persistence.Column;
Expand Down Expand Up @@ -46,6 +47,12 @@ public BellEntity(CustomizeTimeBoxEntity customizeTimeBox, int time, int count)
this.count = count;
}

public BellEntity(CustomizeTimeBoxEntity customizeTimeBox, Bell bell) {
this.customizeTimeBox = customizeTimeBox;
this.time = bell.getTime();
this.count = bell.getCount();
}

private void validateTime(int time) {
if (time < 0) {
throw new DTClientErrorException(ClientErrorCode.INVALID_BELL_TIME);
Expand All @@ -57,4 +64,12 @@ private void validateCount(int count) {
throw new DTClientErrorException(ClientErrorCode.INVALID_BELL_COUNT);
}
}

public Bell toDomain() {
return new Bell(time, count);
}

public boolean isContained(CustomizeTimeBoxEntity timeBox) {
return this.customizeTimeBox.getId().equals(timeBox.getId());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[사소한 제안]

CustomizeTimeBoxEntity 한테 isSame(CustomizeTimeBoxEntity)로 시켜도 될 것 같아요

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.debatetimer.entity.customize;

import com.debatetimer.domain.customize.Bell;
import com.debatetimer.domain.customize.CustomizeTimeBox;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import lombok.Getter;

public class CustomizeTimeBoxEntities {

private static final Comparator<CustomizeTimeBoxEntity> TIME_BOX_COMPARATOR = Comparator
.comparing(CustomizeTimeBoxEntity::getSequence);

@Getter
private final List<CustomizeTimeBoxEntity> timeBoxes;

private final List<BellEntity> bells;

public CustomizeTimeBoxEntities(List<CustomizeTimeBoxEntity> timeBoxes) {
this.timeBoxes = timeBoxes.stream()
.sorted(TIME_BOX_COMPARATOR)
.toList();
this.bells = Collections.emptyList();
}

public CustomizeTimeBoxEntities(List<CustomizeTimeBoxEntity> timeBoxes, List<BellEntity> bells) {
this.timeBoxes = timeBoxes.stream()
.sorted(TIME_BOX_COMPARATOR)
.toList();
this.bells = bells;
}
Comment on lines +27 to +32
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나중에 V1 제거할 때 bell을 포함하는 이름으로 바꿔야 할려나요

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 이게... TimeBox 안에 Bell 이 포함된 개념이라고 생각해서 bell 관련 정보를 따로 명시하지는 않았음(ex. Car 안에 Name이 포함되어 있는 느낌?)
물론, 인지 방향이 어느정도 떨어질 수 있다는 것에는 일부 동의하긴 합니다. 그래도 나는 개념을 명확히 하고 서로 공유하는 컨텍스트에 있다면 위와 같은 클래스 네임도 납득할 수 있다고 생각합니다.


public List<CustomizeTimeBox> toDomain() {
return timeBoxes.stream()
.map(timebox -> timebox.toDomain(getBells(timebox)))
.toList();
}

private List<Bell> getBells(CustomizeTimeBoxEntity timeBox) {
return bells.stream()
.filter(bell -> bell.isContained(timeBox))
.map(BellEntity::toDomain)
.toList();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.debatetimer.entity.customize;

import com.debatetimer.domain.customize.Bell;
import com.debatetimer.domain.customize.CustomizeBoxType;
import com.debatetimer.domain.customize.CustomizeTimeBox;
import com.debatetimer.domain.customize.NormalTimeBox;
import com.debatetimer.domain.customize.Stance;
import com.debatetimer.domain.customize.TimeBasedTimeBox;
import com.debatetimer.exception.custom.DTClientErrorException;
import com.debatetimer.exception.errorcode.ClientErrorCode;
import jakarta.persistence.Entity;
Expand All @@ -16,6 +20,7 @@
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import java.util.List;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand Down Expand Up @@ -45,7 +50,7 @@ public class CustomizeTimeBoxEntity {
@Enumerated(EnumType.STRING)
private Stance stance;

private int time;
private Integer time;
private String speaker;

@NotBlank
Expand Down Expand Up @@ -110,6 +115,18 @@ public CustomizeTimeBoxEntity(
this.timePerSpeaking = timePerSpeaking;
}

public CustomizeTimeBoxEntity(CustomizeTableEntity customizeTable, CustomizeTimeBox timeBox, int sequence) {
this.customizeTable = customizeTable;
this.sequence = sequence;
this.stance = timeBox.getStance();
this.time = timeBox.getTime();
this.speaker = timeBox.getSpeaker();
this.speechType = timeBox.getSpeechType();
this.boxType = timeBox.getBoxType();
this.timePerTeam = timeBox.getTimePerTeam();
this.timePerSpeaking = timeBox.getTimePerSpeaking();
}

private static int convertToTime(Integer timePerTeam) {
if (timePerTeam == null) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BOX_FORMAT);
Expand Down Expand Up @@ -178,4 +195,11 @@ private void validateSpeechType(String speechType) {
throw new DTClientErrorException(ClientErrorCode.INVALID_TIME_BOX_SPEECH_TYPE_LENGTH);
}
}

public CustomizeTimeBox toDomain(List<Bell> bells) {
if (boxType.isTimeBased()) {
return new TimeBasedTimeBox(stance, speechType, speaker, timePerTeam, timePerSpeaking);
}
return new NormalTimeBox(stance, speechType, speaker, time, bells);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import com.debatetimer.entity.customize.BellEntity;
import com.debatetimer.entity.customize.CustomizeTimeBoxEntity;
import java.util.List;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;

public interface BellRepository extends Repository<BellEntity, Long> {

BellEntity save(BellEntity bell);

List<BellEntity> findByCustomizeTimeBox(CustomizeTimeBoxEntity customizeTimeBox);

void deleteAllByCustomizeTimeBoxIn(List<CustomizeTimeBoxEntity> customizeTimeBoxes);
@Query("DELETE FROM BellEntity b WHERE b.customizeTimeBox.customizeTable.id = :tableId")
@Modifying(clearAutomatically = true, flushAutomatically = true)
void deleteAllByTable(long tableId);
Comment on lines +14 to +16
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이거 쿼리가 궁금해서 한번 테스트로 확인해봤더니

delete 
    from
        bell dml_target_ 
    where
        exists(select
            1 
        from
            bell be1_0 
        join
            customize_time_box ctb1_0 
                on ctb1_0.id=be1_0.customize_time_box_id 
        where
            ctb1_0.table_id=? 
            and dml_target_._rowid_=be1_0._rowid_)

흠... 데이터 양을 생각하면 괜찮을 것 같긴한데... 나중에 최적화를 해보면 좋을 것 같네요

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[궁금한 질문]

이거 진짜 그냥 궁금해서 남기는 질문인데 timeBox in 절이 아니라 tableid 받아서 삭제하게 한 이유가 있나요? 사용성 측면에선 더 좋아졌다고 생각하는데 도메인 적으로는 시간표 - 타임박스의 벨이 약간 거리감이 있는 느낌이라 커찬이 생각한 리팩터링 의도가 순수히 궁금해요!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

동시성 이슈 기억하시나요?
timeBox 조회 -> 삭제 하는 로직 덕분에 동시성 이슈 일어났잖아요. deleteByTimeBoxIn(List<TimeBox> timBoxes) 이런 거 쓰면 timeBox를 조회해야 되니까 동시성 이슈 해결이 안되겠죠?


List<BellEntity> findAllByCustomizeTimeBoxIn(List<CustomizeTimeBoxEntity> timeBoxes);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.debatetimer.repository.customize;

import com.debatetimer.domain.customize.CustomizeTimeBoxEntities;
import com.debatetimer.entity.customize.CustomizeTableEntity;
import com.debatetimer.entity.customize.CustomizeTimeBoxEntities;
import com.debatetimer.entity.customize.CustomizeTimeBoxEntity;
import java.util.List;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -27,7 +27,7 @@ default CustomizeTimeBoxEntities findTableTimeBoxes(CustomizeTableEntity table)
return new CustomizeTimeBoxEntities(timeBoxes);
}

@Query("DELETE FROM CustomizeTimeBoxEntity ctb WHERE ctb.customizeTable = :table")
@Query("DELETE FROM CustomizeTimeBoxEntity ctb WHERE ctb.customizeTable.id = :tableId")
@Modifying(clearAutomatically = true, flushAutomatically = true)
void deleteAllByTable(CustomizeTableEntity table);
void deleteAllByTable(long tableId);
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.debatetimer.service.customize;

import com.debatetimer.domain.customize.CustomizeTable;
import com.debatetimer.domain.customize.CustomizeTimeBoxEntities;
import com.debatetimer.domain.member.Member;
import com.debatetimer.dto.customize.request.CustomizeTableCreateRequest;
import com.debatetimer.dto.customize.response.CustomizeTableResponse;
import com.debatetimer.entity.customize.CustomizeTableEntity;
import com.debatetimer.entity.customize.CustomizeTimeBoxEntities;
import com.debatetimer.entity.customize.CustomizeTimeBoxEntity;
import com.debatetimer.repository.customize.CustomizeTableRepository;
import com.debatetimer.repository.customize.CustomizeTimeBoxRepository;
Expand Down Expand Up @@ -47,7 +47,7 @@ public CustomizeTableResponse updateTable(
CustomizeTable renewedTable = tableCreateRequest.toTable(member);
existingTable.updateTable(renewedTable);

timeBoxRepository.deleteAllByTable(existingTable);
timeBoxRepository.deleteAllByTable(existingTable.getId());
CustomizeTimeBoxEntities savedCustomizeTimeBoxes = saveTimeBoxes(tableCreateRequest, existingTable.toDomain());
return new CustomizeTableResponse(existingTable.toDomain(), savedCustomizeTimeBoxes);
}
Expand All @@ -64,7 +64,7 @@ public CustomizeTableResponse updateUsedAt(long tableId, Member member) {
@Transactional
public void deleteTable(long tableId, Member member) {
CustomizeTableEntity table = tableRepository.getByIdAndMember(tableId, member);
timeBoxRepository.deleteAllByTable(table);
timeBoxRepository.deleteAllByTable(table.getId());
tableRepository.delete(table);
}

Expand Down
Loading