From b8ef1947efa20912fd07afbbaf8f1171bac1cff1 Mon Sep 17 00:00:00 2001 From: Cho jiwon <77895305+Jiwon-cho@users.noreply.github.com> Date: Sat, 17 Feb 2024 16:16:41 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20baseEntity=20=EC=83=9D=EC=84=B1,=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9=20(#29)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/mara/server/common/BaseEntity.kt | 31 +++++++++++++++++++ .../mara/server/domain/friend/Friendship.kt | 3 +- .../server/domain/ingredient/Ingredient.kt | 3 +- .../domain/ingredient/IngredientDetail.kt | 3 +- .../domain/refrigerator/Refrigerator.kt | 3 +- .../mara/server/domain/share/ApplyShare.kt | 3 +- .../kotlin/mara/server/domain/share/Share.kt | 3 +- .../kotlin/mara/server/domain/user/User.kt | 3 +- 8 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/mara/server/common/BaseEntity.kt diff --git a/src/main/kotlin/mara/server/common/BaseEntity.kt b/src/main/kotlin/mara/server/common/BaseEntity.kt new file mode 100644 index 0000000..49d3178 --- /dev/null +++ b/src/main/kotlin/mara/server/common/BaseEntity.kt @@ -0,0 +1,31 @@ +package mara.server.common + +import com.fasterxml.jackson.annotation.JsonFormat +import com.fasterxml.jackson.annotation.JsonIgnoreProperties +import jakarta.persistence.Column +import jakarta.persistence.EntityListeners +import jakarta.persistence.MappedSuperclass +import org.springframework.data.annotation.CreatedDate +import org.springframework.data.annotation.LastModifiedDate +import org.springframework.data.jpa.domain.support.AuditingEntityListener +import java.time.ZonedDateTime + +@MappedSuperclass +@JsonIgnoreProperties( + value = [ + "createdAt, updatedAt", + ], + allowGetters = true, +) +@EntityListeners(AuditingEntityListener::class) +abstract class BaseEntity { + @CreatedDate + @Column(name = "created_at") + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS", timezone = "Asia/Seoul") + var createdAt: ZonedDateTime = ZonedDateTime.now() + + @LastModifiedDate + @Column(name = "updated_at") + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss.SSS", timezone = "Asia/Seoul") + var updatedAt: ZonedDateTime = ZonedDateTime.now() +} diff --git a/src/main/kotlin/mara/server/domain/friend/Friendship.kt b/src/main/kotlin/mara/server/domain/friend/Friendship.kt index 78497b7..1dcd9e6 100644 --- a/src/main/kotlin/mara/server/domain/friend/Friendship.kt +++ b/src/main/kotlin/mara/server/domain/friend/Friendship.kt @@ -8,6 +8,7 @@ import jakarta.persistence.GenerationType import jakarta.persistence.Id import jakarta.persistence.JoinColumn import jakarta.persistence.ManyToOne +import mara.server.common.BaseEntity import mara.server.domain.user.User @Entity @@ -20,7 +21,7 @@ class Friendship( @JoinColumn(name = "to_user_id") val toUser: User, -) { +) : BaseEntity() { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "friendship_id", nullable = false) diff --git a/src/main/kotlin/mara/server/domain/ingredient/Ingredient.kt b/src/main/kotlin/mara/server/domain/ingredient/Ingredient.kt index 812779f..94fe651 100644 --- a/src/main/kotlin/mara/server/domain/ingredient/Ingredient.kt +++ b/src/main/kotlin/mara/server/domain/ingredient/Ingredient.kt @@ -4,6 +4,7 @@ import jakarta.persistence.Entity import jakarta.persistence.GeneratedValue import jakarta.persistence.GenerationType import jakarta.persistence.Id +import mara.server.common.BaseEntity @Entity class Ingredient( @@ -11,7 +12,7 @@ class Ingredient( var name: String, var iconImage: String, var expirationDays: Int = 0 -) { +) : BaseEntity() { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val ingredientId: Long = 0L diff --git a/src/main/kotlin/mara/server/domain/ingredient/IngredientDetail.kt b/src/main/kotlin/mara/server/domain/ingredient/IngredientDetail.kt index d9ba5f2..a79121a 100644 --- a/src/main/kotlin/mara/server/domain/ingredient/IngredientDetail.kt +++ b/src/main/kotlin/mara/server/domain/ingredient/IngredientDetail.kt @@ -9,6 +9,7 @@ import jakarta.persistence.GenerationType import jakarta.persistence.Id import jakarta.persistence.JoinColumn import jakarta.persistence.ManyToOne +import mara.server.common.BaseEntity import mara.server.domain.refrigerator.Refrigerator import java.time.LocalDateTime @@ -30,7 +31,7 @@ class IngredientDetail( var addDate: LocalDateTime, var expirationDate: LocalDateTime, var isDeleted: Boolean = false -) { +) : BaseEntity() { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val ingredientDetailId: Long = 0L diff --git a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt index 9fa9775..3550a60 100644 --- a/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt +++ b/src/main/kotlin/mara/server/domain/refrigerator/Refrigerator.kt @@ -8,6 +8,7 @@ import jakarta.persistence.GenerationType import jakarta.persistence.Id import jakarta.persistence.JoinColumn import jakarta.persistence.ManyToOne +import mara.server.common.BaseEntity import mara.server.domain.user.User import java.time.LocalDateTime @@ -19,7 +20,7 @@ class Refrigerator( @JoinColumn(name = "userId") val user: User, -) { +) : BaseEntity() { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "refrigerator_id", nullable = false) diff --git a/src/main/kotlin/mara/server/domain/share/ApplyShare.kt b/src/main/kotlin/mara/server/domain/share/ApplyShare.kt index b0d0077..b867429 100644 --- a/src/main/kotlin/mara/server/domain/share/ApplyShare.kt +++ b/src/main/kotlin/mara/server/domain/share/ApplyShare.kt @@ -8,6 +8,7 @@ import jakarta.persistence.GenerationType import jakarta.persistence.Id import jakarta.persistence.JoinColumn import jakarta.persistence.ManyToOne +import mara.server.common.BaseEntity import mara.server.domain.user.User @Entity @@ -18,7 +19,7 @@ data class ApplyShare( @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "shareId") val share: Share, -) { +) : BaseEntity() { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "apply_share_id") diff --git a/src/main/kotlin/mara/server/domain/share/Share.kt b/src/main/kotlin/mara/server/domain/share/Share.kt index 8947085..f64dddc 100644 --- a/src/main/kotlin/mara/server/domain/share/Share.kt +++ b/src/main/kotlin/mara/server/domain/share/Share.kt @@ -10,6 +10,7 @@ import jakarta.persistence.Id import jakarta.persistence.JoinColumn import jakarta.persistence.ManyToOne import jakarta.persistence.OneToMany +import mara.server.common.BaseEntity import mara.server.domain.ingredient.IngredientDetail import mara.server.domain.user.User import java.time.LocalDate @@ -34,7 +35,7 @@ data class Share( var location: String, var status: ShareStatus, var thumbNailImage: String -) { +) : BaseEntity() { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val id: Long = 0 diff --git a/src/main/kotlin/mara/server/domain/user/User.kt b/src/main/kotlin/mara/server/domain/user/User.kt index ce8fb2f..7b7a145 100644 --- a/src/main/kotlin/mara/server/domain/user/User.kt +++ b/src/main/kotlin/mara/server/domain/user/User.kt @@ -7,6 +7,7 @@ import jakarta.persistence.GeneratedValue import jakarta.persistence.GenerationType import jakarta.persistence.Id import jakarta.persistence.Table +import mara.server.common.BaseEntity enum class Role { USER } @Entity @@ -20,7 +21,7 @@ class User( val inviteCode: String, @Enumerated(EnumType.STRING) var profileImage: ProfileImage, -) { +) : BaseEntity() { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) val userId: Long = 0L