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

임시저장 시 필드값이나 게임 갯수가 모자라도 저장되도록 구현 #798

Merged
merged 4 commits into from
Dec 11, 2024
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
Expand Up @@ -43,6 +43,12 @@ public void createTempGame(CreateTempGameSetRequest request, ApiMember apiMember
Member member = apiMember.toMember(memberRepository);

List<CreateTempGameRequest> tempGames = request.getTempGames();

if (request.getTempGames() == null || request.getTempGames().isEmpty()) {
TempGameSet tempGameSet = request.toEntity(member);
tempGameSetRepository.save(tempGameSet);
return;
}
validateFileIds(tempGames); // 파일이 DB에 존재하는지 검사

List<TempGame> newTempGames = tempGames.stream()
Expand Down
21 changes: 13 additions & 8 deletions src/main/java/balancetalk/game/domain/TempGame.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,24 +39,29 @@ public class TempGame extends BaseTimeEntity {
@OneToMany(mappedBy = "tempGame", cascade = CascadeType.ALL)
private List<TempGameOption> tempGameOptions = new ArrayList<>();

@NotBlank
@Size(max = 100)
private String description;

public void assignTempGameSet(TempGameSet tempGameSet) {
this.tempGameSet = tempGameSet;
}

public void addTempGameOption(TempGameOption tempGameOption) {
tempGameOption.assignTempGame(this);
tempGameOptions.add(tempGameOption);
}

public void updateTempGame(TempGame newTempGame) {
this.description = newTempGame.getDescription();
IntStream.range(0, newTempGame.getTempGameOptions().size()).forEach(i -> {
TempGameOption currentOption = this.tempGameOptions.get(i);
TempGameOption newOption = newTempGame.getTempGameOptions().get(i);
currentOption.updateTempGameOption(newOption);
if (i < this.tempGameOptions.size()) {
TempGameOption currentOption = this.tempGameOptions.get(i);
TempGameOption newOption = newTempGame.getTempGameOptions().get(i);
currentOption.updateTempGameOption(newOption);
} else {
TempGameOption newOption = newTempGame.getTempGameOptions().get(i);
this.addTempGameOption(newOption);
}
});
}

public List<Long> getGameOptionIds() {
return tempGameOptions.stream().map(TempGameOption::getId).toList();
}
}
2 changes: 0 additions & 2 deletions src/main/java/balancetalk/game/domain/TempGameOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -31,7 +30,6 @@ public class TempGameOption {
@Column(name = "id")
private Long id;

@NotBlank
@Size(max = 30)
private String name;

Expand Down
2 changes: 0 additions & 2 deletions src/main/java/balancetalk/game/domain/TempGameSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -39,7 +38,6 @@ public class TempGameSet extends BaseTimeEntity {
@OneToMany(mappedBy = "tempGameSet", cascade = CascadeType.ALL)
private List<TempGame> tempGames = new ArrayList<>();

@NotBlank
@Size(max = 50)
private String title;

Expand Down
9 changes: 1 addition & 8 deletions src/main/java/balancetalk/game/dto/TempGameSetDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import balancetalk.game.domain.TempGameSet;
import balancetalk.game.dto.TempGameDto.CreateTempGameRequest;
import balancetalk.game.dto.TempGameDto.TempGameResponse;
import balancetalk.global.exception.BalanceTalkException;
import balancetalk.global.exception.ErrorCode;
import balancetalk.member.domain.Member;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -17,9 +15,7 @@
import lombok.Data;

public class TempGameSetDto {

private static final int GAME_SIZE = 10;


@Data
public static class CreateTempGameSetRequest {

Expand All @@ -34,9 +30,6 @@ public static class CreateTempGameSetRequest {
private List<CreateTempGameRequest> tempGames;

public TempGameSet toEntity(Member member) {
if (tempGames == null || tempGames.size() < GAME_SIZE) {
throw new BalanceTalkException(ErrorCode.BALANCE_GAME_SIZE_TEN);
}
return TempGameSet.builder()
.title(title)
.member(member)
Expand Down
Loading