diff --git a/.gitignore b/.gitignore index c2065bc..0e65c1d 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ bin/ out/ !**/src/main/**/out/ !**/src/test/**/out/ +src/test/resources/application.yml ### NetBeans ### /nbproject/private/ diff --git a/build.gradle b/build.gradle index 72fe741..e444a7a 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ repositories { mavenCentral() } -subprojects { +subprojects { //ktlint 적용 린트 repositories { mavenCentral() } @@ -41,7 +41,7 @@ dependencies { testImplementation 'org.springframework.boot:spring-boot-starter-test' } -compileKotlin { +compileKotlin { kotlinOptions { freeCompilerArgs = ['-Xjsr305=strict'] jvmTarget = '17' @@ -49,6 +49,9 @@ compileKotlin { } allOpen { + //JPA 엔티티 만들 때 규칙이 있음 (final 클래스 안돼) + //코틀린은 클래스 기본이 final이야. @Entity 등 annotation이 붙이면 자동으로 final 아니게 설정 + //이렇게 안 하면 모든 클래스에 open을 붙여야됨 annotation("jakarta.persistence.Entity") annotation("jakarta.persistence.MappedSuperclass") annotation("jakarta.persistence.Embeddable") @@ -58,6 +61,6 @@ noArg { annotation("jakarta.persistence.Entity") } -test{ +test { useJUnitPlatform() } diff --git a/src/main/kotlin/com/record/zooc/domain/entity/Account.kt b/src/main/kotlin/com/record/zooc/domain/entity/Account.kt new file mode 100644 index 0000000..336d27e --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/Account.kt @@ -0,0 +1,76 @@ +package com.record.zooc.domain.entity + +import com.record.zooc.domain.entity.relation.UserFamilyRelation +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.OneToMany +import jakarta.persistence.Table + +// @Builder가 필요한 상황 찾아보기 +// reflection 개념 찾아보기 + +@Entity +@Table(name = "account") +class Account( + role: String, + everRecorded: Boolean = false, + refreshToken: String, + snsToken: String, + snsType: String, + userEmail: String? = null, + profileImage: String? = null +) { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = 0 + protected set + // 객체의 id를 null로 두고 데이터베이스에 insert일어날 때 생성되기 때문에 nullable이어야됨 + + @Column + var role: String = role + protected set + + @Column(name = "user_email") + var userEmail: String? = userEmail + protected set + + @Column(name = "profile_image") + var profileImage: String? = profileImage + protected set + + @Column(name = "ever_recorded") + var everRecorded: Boolean = everRecorded + protected set + + @Column(name = "refresh_token") + var refreshToken: String = refreshToken + protected set + + @Column(name = "sns_token") + var snsToken: String = snsToken + protected set + + @Column(name = "sns_type") + var snsType: String = snsType + protected set + + @OneToMany(mappedBy = "account") + var relationsWithFamily: ArrayList = ArrayList() + protected set + + fun updateUserRoleAndProfile( + role: String? = null, + profileImage: String? = null, + ) { + // role이 null이 아니면 this에 role 저장 + role?.let { this.role = it } + profileImage?.let { this.profileImage = it } + } + + fun userRecordedForTheFirstTime() { + this.everRecorded = true + } +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/Family.kt b/src/main/kotlin/com/record/zooc/domain/entity/Family.kt new file mode 100644 index 0000000..1c0fa0f --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/Family.kt @@ -0,0 +1,27 @@ +package com.record.zooc.domain.entity + +import com.record.zooc.domain.entity.relation.UserFamilyRelation +import com.record.zooc.domain.entity.base.ModifiedTimeEntity +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.OneToMany + +@Entity +class Family( + code: String, +) : ModifiedTimeEntity() { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = 0 + protected set + + @Column + val code: String = code + + @OneToMany(mappedBy = "family") + var relationsWithUser: ArrayList = ArrayList() + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/Pet.kt b/src/main/kotlin/com/record/zooc/domain/entity/Pet.kt new file mode 100644 index 0000000..d392948 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/Pet.kt @@ -0,0 +1,33 @@ +package com.record.zooc.domain.entity + +import com.record.zooc.domain.entity.relation.MemoryPetRelation +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.OneToMany +import jakarta.persistence.Table + +@Entity +@Table(name = "pet") +class Pet( + name: String, + profileImage: String? = null +) { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null + + @Column + var name: String = name + protected set + + @Column(name = "profile_image") + var profileImage: String? = profileImage + protected set + + @OneToMany(mappedBy = "pet") + var relationsWithMemory: ArrayList = ArrayList() + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/base/CreatedTimeEntity.kt b/src/main/kotlin/com/record/zooc/domain/entity/base/CreatedTimeEntity.kt new file mode 100644 index 0000000..3ac4e89 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/base/CreatedTimeEntity.kt @@ -0,0 +1,20 @@ +package com.record.zooc.domain.entity.base + +import com.fasterxml.jackson.annotation.JsonFormat +import jakarta.persistence.Column +import jakarta.persistence.EntityListeners +import jakarta.persistence.MappedSuperclass +import org.springframework.data.annotation.CreatedDate +import org.springframework.data.jpa.domain.support.AuditingEntityListener +import java.time.LocalDateTime + +@MappedSuperclass +@EntityListeners(AuditingEntityListener::class) +abstract class CreatedTimeEntity { + @CreatedDate + @Column(nullable = false, updatable = false) + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + var createdAt: LocalDateTime = LocalDateTime.now() + protected set + // var로 해야지 시간 저장될 때 오류 안남 +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/base/ModifiedTimeEntity.kt b/src/main/kotlin/com/record/zooc/domain/entity/base/ModifiedTimeEntity.kt new file mode 100644 index 0000000..b12622e --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/base/ModifiedTimeEntity.kt @@ -0,0 +1,20 @@ +package com.record.zooc.domain.entity.base + +import com.fasterxml.jackson.annotation.JsonFormat +import jakarta.persistence.Column +import jakarta.persistence.EntityListeners +import jakarta.persistence.MappedSuperclass +import org.springframework.data.annotation.LastModifiedDate +import org.springframework.data.jpa.domain.support.AuditingEntityListener +import java.time.LocalDateTime + +@MappedSuperclass +@EntityListeners(AuditingEntityListener::class) +abstract class ModifiedTimeEntity : CreatedTimeEntity() { + + @LastModifiedDate + @Column(nullable = false) + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + var modifiedAt: LocalDateTime = LocalDateTime.now() + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/comment/Comment.kt b/src/main/kotlin/com/record/zooc/domain/entity/comment/Comment.kt new file mode 100644 index 0000000..fee5ca8 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/comment/Comment.kt @@ -0,0 +1,50 @@ +package com.record.zooc.domain.entity.comment + +import com.record.zooc.domain.entity.Account +import com.record.zooc.domain.entity.base.ModifiedTimeEntity +import jakarta.persistence.Column +import jakarta.persistence.DiscriminatorColumn +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.Inheritance +import jakarta.persistence.InheritanceType +import jakarta.persistence.JoinColumn +import jakarta.persistence.OneToOne +import jakarta.persistence.Table + +@DiscriminatorColumn(name = "comment_type") +@Entity +@Table(name = "comment") +@Inheritance(strategy = InheritanceType.JOINED) +class Comment( + content: String, + account: Account, + memoryId: Int, + +) : ModifiedTimeEntity() { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = 0 + protected set + + @Column + var content: String = content + protected set + + @OneToOne + @JoinColumn(name = "writer_id") + var writer: Account = account + protected set + + @Column + var memoryId: Int = memoryId + protected set + + fun updateContent( + content: String, + ) { + this.content = content + } +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/comment/Emojicomment.kt b/src/main/kotlin/com/record/zooc/domain/entity/comment/Emojicomment.kt new file mode 100644 index 0000000..9a7a157 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/comment/Emojicomment.kt @@ -0,0 +1,17 @@ +package com.record.zooc.domain.entity.comment + +import com.record.zooc.domain.entity.Account +import jakarta.persistence.Column +import jakarta.persistence.DiscriminatorValue + +@DiscriminatorValue("Emoji") +class Emojicomment( + content: String, + account: Account, + recordId: Int, + emoji: Int +) : Comment(content, account, recordId) { + @Column + var emoji: Int = emoji + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/comment/Textcomment.kt b/src/main/kotlin/com/record/zooc/domain/entity/comment/Textcomment.kt new file mode 100644 index 0000000..bd46e2c --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/comment/Textcomment.kt @@ -0,0 +1,11 @@ +package com.record.zooc.domain.entity.comment + +import com.record.zooc.domain.entity.Account +import jakarta.persistence.DiscriminatorValue + +@DiscriminatorValue("Text") +class Textcomment( + content: String, + account: Account, + recordId: Int, +) : Comment(content, account, recordId) diff --git a/src/main/kotlin/com/record/zooc/domain/entity/memory/Memory.kt b/src/main/kotlin/com/record/zooc/domain/entity/memory/Memory.kt new file mode 100644 index 0000000..e68a367 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/memory/Memory.kt @@ -0,0 +1,50 @@ +package com.record.zooc.domain.entity.memory + +import com.record.zooc.domain.entity.Account +import com.record.zooc.domain.entity.relation.MemoryPetRelation +import com.record.zooc.domain.entity.base.ModifiedTimeEntity +import jakarta.persistence.Column +import jakarta.persistence.DiscriminatorColumn +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.Inheritance +import jakarta.persistence.InheritanceType +import jakarta.persistence.JoinColumn +import jakarta.persistence.OneToMany +import jakarta.persistence.OneToOne +import jakarta.persistence.Table + +@DiscriminatorColumn(name = "memory_type") +@Entity +@Table(name = "memory") +@Inheritance(strategy = InheritanceType.JOINED) +class Memory( + image: String, + content: String, + writer: Account +) : ModifiedTimeEntity() { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = 0 + protected set + + @Column + var image: String = image + protected set + + @Column + var content: String = content + protected set + + @OneToMany(mappedBy = "memory") + @JoinColumn(name = "memory_pet") + var relationsWithPet: ArrayList = ArrayList() + protected set + + @OneToOne + @JoinColumn(name = "writer_id") + var writer: Account = writer + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/memory/Mission.kt b/src/main/kotlin/com/record/zooc/domain/entity/memory/Mission.kt new file mode 100644 index 0000000..f904700 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/memory/Mission.kt @@ -0,0 +1,23 @@ +package com.record.zooc.domain.entity.memory + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.Table + +@Entity +@Table(name = "mission") +class Mission( + missionContent: String +) { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = 0 + protected set + + @Column(name = "mission_content") + var missionContent: String = missionContent + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/memory/Missionmemory.kt b/src/main/kotlin/com/record/zooc/domain/entity/memory/Missionmemory.kt new file mode 100644 index 0000000..735f2e3 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/memory/Missionmemory.kt @@ -0,0 +1,21 @@ +package com.record.zooc.domain.entity.memory + +import com.record.zooc.domain.entity.Account +import jakarta.persistence.Column +import jakarta.persistence.DiscriminatorValue + +@DiscriminatorValue("Mission") +class Missionmemory( + image: String, + content: String, + missionId: Int, + writer: Account +) : Memory( + image, + content, + writer +) { + @Column(name = "mission_id") + var missionId: Int = missionId + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/memory/Recordmemory.kt b/src/main/kotlin/com/record/zooc/domain/entity/memory/Recordmemory.kt new file mode 100644 index 0000000..5aa30f2 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/memory/Recordmemory.kt @@ -0,0 +1,15 @@ +package com.record.zooc.domain.entity.memory + +import com.record.zooc.domain.entity.Account +import jakarta.persistence.DiscriminatorValue + +@DiscriminatorValue("Record") +class Recordmemory( + image: String, + content: String, + writer: Account +) : Memory( + image, + content, + writer +) diff --git a/src/main/kotlin/com/record/zooc/domain/entity/pushAlarm/Alarm.kt b/src/main/kotlin/com/record/zooc/domain/entity/pushAlarm/Alarm.kt new file mode 100644 index 0000000..1333eda --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/pushAlarm/Alarm.kt @@ -0,0 +1,39 @@ +package com.record.zooc.domain.entity.pushAlarm + +import com.record.zooc.domain.entity.base.CreatedTimeEntity +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.Table + +@Entity +@Table(name = "alarm") +class Alarm( + userId: Long, + writerId: Long, + familyId: Long, + recordId: Long, +) : CreatedTimeEntity() { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = 0 + protected set + + @Column(name = "user_id") + var userId: Long = userId + protected set + + @Column(name = "writer_id") + var writerId: Long = writerId + protected set + + @Column(name = "family_id") + var familyId: Long = familyId + protected set + + @Column(name = "record_id") + var recordId: Long = recordId + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/pushAlarm/FcmToken.kt b/src/main/kotlin/com/record/zooc/domain/entity/pushAlarm/FcmToken.kt new file mode 100644 index 0000000..912b83d --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/pushAlarm/FcmToken.kt @@ -0,0 +1,28 @@ +package com.record.zooc.domain.entity.pushAlarm + +import jakarta.persistence.Column +import jakarta.persistence.Entity +import jakarta.persistence.GeneratedValue +import jakarta.persistence.GenerationType +import jakarta.persistence.Id +import jakarta.persistence.Table + +@Entity +@Table(name = "fcmtoken") +class FcmToken( + fcmToken: String, + setting: Boolean, +) { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = 0 + protected set + + @Column(name = "fcm_token") + var fcmToken: String = fcmToken + protected set + + @Column + var setting: Boolean = setting + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/relation/MemoryPetRelation.kt b/src/main/kotlin/com/record/zooc/domain/entity/relation/MemoryPetRelation.kt new file mode 100644 index 0000000..93ef315 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/relation/MemoryPetRelation.kt @@ -0,0 +1,58 @@ +package com.record.zooc.domain.entity.relation + +import com.record.zooc.domain.entity.Pet +import com.record.zooc.domain.entity.memory.Memory +import jakarta.persistence.Column +import jakarta.persistence.Embeddable +import jakarta.persistence.EmbeddedId +import jakarta.persistence.Entity +import jakarta.persistence.JoinColumn +import jakarta.persistence.ManyToOne +import jakarta.persistence.MapsId +import jakarta.persistence.Table +import java.io.Serializable + +@Embeddable +class MemoryPetRelationId( + memoryId: Long?, + petId: Long? +) : Serializable { + // 사실 이게 notnull인게 맞는데... Memory에 id가 nullable이다보니까... + + @Column(name = "memory_id", nullable = false) + val memoryId = memoryId + + @Column(name = "pet_id", nullable = false) + val petId = petId + + override fun equals(other: Any?): Boolean { + return super.equals(other) + // 구현 예정 + } + + override fun hashCode(): Int { + return super.hashCode() + // 구현 예정 + } +} + +@Entity +@Table(name = "memory_pet") +class MemoryPetRelation(memory: Memory, pet: Pet) { + + @EmbeddedId + var memoryPetId: MemoryPetRelationId = MemoryPetRelationId(memory.id, pet.id) + protected set + + @MapsId("memory_id") + @ManyToOne + @JoinColumn(name = "memory_id") + var memory: Memory = memory + protected set + + @MapsId("pet_id") + @ManyToOne + @JoinColumn(name = "pet_id") + var pet: Pet = pet + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/entity/relation/UserFamilyRelation.kt b/src/main/kotlin/com/record/zooc/domain/entity/relation/UserFamilyRelation.kt new file mode 100644 index 0000000..5a30c66 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/entity/relation/UserFamilyRelation.kt @@ -0,0 +1,58 @@ +package com.record.zooc.domain.entity.relation + +import com.record.zooc.domain.entity.Family +import com.record.zooc.domain.entity.Account +import jakarta.persistence.Column +import jakarta.persistence.Embeddable +import jakarta.persistence.EmbeddedId +import jakarta.persistence.Entity +import jakarta.persistence.JoinColumn +import jakarta.persistence.ManyToOne +import jakarta.persistence.MapsId +import jakarta.persistence.Table +import java.io.Serializable + +@Embeddable +class UserFamilyRelationId( + userId: Long?, + familyId: Long? +) : Serializable { + // 사실 이게 notnull인게 맞는데... Memory에 id가 nullable이다보니까... + + @Column(name = "user_id", nullable = false) + val userId = userId + + @Column(name = "family_id", nullable = false) + val familyId = familyId + + override fun equals(other: Any?): Boolean { + return super.equals(other) + // 구현 예정 + } + + override fun hashCode(): Int { + return super.hashCode() + // 구현 예정 + } +} + +@Entity +@Table(name = "user_family") +class UserFamilyRelation(account: Account, family: Family) { + + @EmbeddedId + var userFamilyId = UserFamilyRelationId(account.id, family.id) + protected set + + @MapsId("user_id") + @ManyToOne + @JoinColumn(name = "user_id") + var account: Account = account + protected set + + @MapsId("family_id") + @ManyToOne + @JoinColumn(name = "family_id") + var family: Family = family + protected set +} diff --git a/src/main/kotlin/com/record/zooc/domain/repository/UserRepository.kt b/src/main/kotlin/com/record/zooc/domain/repository/UserRepository.kt new file mode 100644 index 0000000..ca45788 --- /dev/null +++ b/src/main/kotlin/com/record/zooc/domain/repository/UserRepository.kt @@ -0,0 +1,6 @@ +// package com.record.zooc.domain.repository +// +// import com.record.zooc.domain.entity.User +// import org.springframework.data.jpa.repository.JpaRepository +// +// interface UserRepository : JpaRepository diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index 67ac92d..cdff020 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -1,21 +1,21 @@ server: - port: 8082 + port: 8082 spring: - datasource: - driver-class-name: org.postgresql.Driver - url: ${url} - username: ${username} - password: ${password} - jpa: - hibernate: - ddl-auto: update - show-sql: true - database: postgresql - database-platform: org.hibernate.dialect.PostgreSQLDialect - open-in-view: false - generate-ddl: true - properties: - hibernate: - default_schema: dev - format_sql: true + datasource: + driver-class-name: org.postgresql.Driver + url: ${url} + username: ${username} + password: ${password} + jpa: + hibernate: + ddl-auto: update + show-sql: true + database: postgresql + database-platform: org.hibernate.dialect.PostgreSQLDialect + open-in-view: false + generate-ddl: true + properties: + hibernate: + default_schema: dev + format_sql: true diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 6bc3c67..9277c14 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -1,15 +1,15 @@ server: - port: 8080 + port: 8080 spring: - datasource: - driver-class-name: org.postgresql.Driver - url: ${url} - username: ${username} - password: ${password} - jpa: - database: postgresql - database-platform: org.hibernate.dialect.PostgreSQLDialect - properties: - hibernate: - default_schema: prod + datasource: + driver-class-name: org.postgresql.Driver + url: ${url} + username: ${username} + password: ${password} + jpa: + database: postgresql + database-platform: org.hibernate.dialect.PostgreSQLDialect + properties: + hibernate: + default_schema: prod