Skip to content

Commit

Permalink
chore : Separate the basic design for comment logic and time-related …
Browse files Browse the repository at this point in the history
…columns into the BaseTimeEntity abstract class. #8
  • Loading branch information
cheomuk committed Feb 10, 2025
1 parent 15b1ea5 commit 6beb5ba
Show file tree
Hide file tree
Showing 16 changed files with 179 additions and 187 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.games.balancegameback.domain.game;

import lombok.Builder;
import lombok.Data;

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

@Data
public class GameResourceComments {

private Long id;
private String comment;
private GameResources gameResources;
private Long parentId;
private LocalDateTime createdDate;
private LocalDateTime updatedDate;
private List<GameResourceComments> children;

@Builder
public GameResourceComments(Long id, String comment, GameResources gameResources, Long parentId,
LocalDateTime createdDate, LocalDateTime updatedDate,
List<GameResourceComments> children) {
this.id = id;
this.comment = comment;
this.gameResources = gameResources;
this.parentId = parentId;
this.createdDate = createdDate;
this.updatedDate = updatedDate;
this.children = children;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.games.balancegameback.domain.game;

import lombok.Builder;

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

public record GameResultComments(Long id, String comment, Games games, Long parentId,
LocalDateTime createdDate, LocalDateTime updatedDate, List<GameResultComments> children) {

@Builder
public GameResultComments {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.games.balancegameback.infra.entity;

import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseTimeEntity {

@Column(updatable = false)
@CreatedDate
private LocalDateTime createdDate;

@Column
@LastModifiedDate
private LocalDateTime updatedDate;
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,11 @@
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import java.time.LocalDateTime;

@Getter
@Entity
@Table(name = "game_invite_code")
public class GameInviteCodeEntity {
public class GameInviteCodeEntity extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -29,14 +25,6 @@ public class GameInviteCodeEntity {
@JoinColumn(name = "games_id", nullable = false)
private GamesEntity games;

@Column(updatable = false)
@CreatedDate
private LocalDateTime createdDate;

@Column
@LastModifiedDate
private LocalDateTime updatedDate;

public static GameInviteCodeEntity from(GameInviteCode gameInviteCode) {
if (gameInviteCode == null) return null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import com.games.balancegameback.domain.game.GamePlay;
import jakarta.persistence.*;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

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

@Getter
@Entity
@Table(name = "games_play")
public class GamePlayEntity {
public class GamePlayEntity extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -38,14 +35,6 @@ public class GamePlayEntity {
@Column
private boolean gameEnded = false;

@Column(updatable = false)
@CreatedDate
private LocalDateTime createdDate;

@Column
@LastModifiedDate
private LocalDateTime updatedDate;

public static GamePlayEntity from(GamePlay gamePlay) {
GamePlayEntity gamePlayEntity = new GamePlayEntity();
gamePlayEntity.id = gamePlay.getId();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.games.balancegameback.infra.entity;

import com.games.balancegameback.domain.game.GameResourceComments;
import jakarta.persistence.*;
import lombok.Getter;
import java.util.ArrayList;
import java.util.List;

@Getter
@Entity
@Table(name = "game_resource_comments")
public class GameResourceCommentsEntity extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String comment;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
private GameResourceCommentsEntity parent;

@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
private List<GameResourceCommentsEntity> children = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "game_resources_id")
private GameResourcesEntity gameResources;

public static GameResourceCommentsEntity from(GameResourceComments gameResourceComments) {
GameResourceCommentsEntity gameResourceCommentsEntity = new GameResourceCommentsEntity();
gameResourceCommentsEntity.comment = gameResourceComments.getComment();
gameResourceCommentsEntity.gameResources = GameResourcesEntity.from(gameResourceComments.getGameResources());

// 부모 댓글 설정
if (gameResourceComments.getParentId() != null) {
GameResourceCommentsEntity parent = new GameResourceCommentsEntity();
parent.id = gameResourceComments.getParentId();
gameResourceCommentsEntity.parent = parent;
}

return gameResourceCommentsEntity;
}

public GameResourceComments toModel() {
return GameResourceComments.builder()
.id(id)
.comment(comment)
.parentId(parent != null ? parent.getId() : null)
.gameResources(gameResources.toModel())
.createdDate(this.getCreatedDate())
.updatedDate(this.getUpdatedDate())
.children(children.stream() // 대댓글 리스트 변환
.map(GameResourceCommentsEntity::toModel)
.toList())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@
import com.games.balancegameback.domain.game.GameResources;
import jakarta.persistence.*;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import java.time.LocalDateTime;

@Getter
@Entity
@Table(name = "game_resources")
public class GameResourcesEntity {
public class GameResourcesEntity extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Expand All @@ -20,14 +16,6 @@ public class GameResourcesEntity {
@Column
private String title;

@Column(updatable = false)
@CreatedDate
private LocalDateTime createdDate;

@Column
@LastModifiedDate
private LocalDateTime updatedDate;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "games_id", nullable = false)
private GamesEntity games;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.games.balancegameback.infra.entity;

import com.games.balancegameback.domain.game.GameResultComments;
import jakarta.persistence.*;
import lombok.Getter;

@Getter
@Entity
@Table(name = "game_result_comments")
public class GameResultCommentsEntity extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String comment;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "games_id")
private GamesEntity games;

public static GameResultCommentsEntity from(GameResultComments gameResultComments) {
GameResultCommentsEntity gameResultCommentsEntity = new GameResultCommentsEntity();
gameResultCommentsEntity.comment = gameResultComments.comment();
gameResultCommentsEntity.games = GamesEntity.from(gameResultComments.games());

return gameResultCommentsEntity;
}

public GameResultComments toModel() {
return GameResultComments.builder()
.id(id)
.comment(comment)
.games(games.toModel())
.createdDate(this.getCreatedDate())
.updatedDate(this.getUpdatedDate())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@
import com.games.balancegameback.domain.game.GameResults;
import jakarta.persistence.*;
import lombok.Getter;
import org.springframework.data.annotation.CreatedDate;

import java.time.LocalDateTime;

@Getter
@Entity
@Table(name = "game_results")
public class GameResultsEntity {
public class GameResultsEntity extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(updatable = false)
@CreatedDate
private LocalDateTime createdDate;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "game_resources_id")
private GameResourcesEntity gameResources;
Expand Down
Loading

0 comments on commit 6beb5ba

Please sign in to comment.