From 7293a05766c144dc8f213f5a941ad745abc93f78 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Sat, 7 Aug 2021 10:51:56 +0700 Subject: [PATCH] Task: Add domain entities with account and user for graphql demo --- .../graphql/demo/domain/AbstractEntity.kt | 7 +++ .../graphql/demo/domain/account/Account.kt | 62 +++++++++++++++++++ .../demo/domain/account/AccountCurrency.kt | 6 ++ .../domain/account/AccountEntityListener.kt | 10 +++ .../demo/domain/account/AccountType.kt | 7 +++ .../cubetiqs/graphql/demo/domain/user/User.kt | 51 +++++++++++++++ .../demo/domain/user/UserEntityListener.kt | 11 ++++ src/main/resources/application.yml | 3 + 8 files changed, 157 insertions(+) create mode 100644 src/main/kotlin/com/cubetiqs/graphql/demo/domain/AbstractEntity.kt create mode 100644 src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/Account.kt create mode 100644 src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountCurrency.kt create mode 100644 src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountEntityListener.kt create mode 100644 src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountType.kt create mode 100644 src/main/kotlin/com/cubetiqs/graphql/demo/domain/user/User.kt create mode 100644 src/main/kotlin/com/cubetiqs/graphql/demo/domain/user/UserEntityListener.kt diff --git a/src/main/kotlin/com/cubetiqs/graphql/demo/domain/AbstractEntity.kt b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/AbstractEntity.kt new file mode 100644 index 0000000..c4cdf05 --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/AbstractEntity.kt @@ -0,0 +1,7 @@ +package com.cubetiqs.graphql.demo.domain + +import java.io.Serializable +import javax.persistence.MappedSuperclass + +@MappedSuperclass +abstract class AbstractEntity : Serializable \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/Account.kt b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/Account.kt new file mode 100644 index 0000000..fbd798f --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/Account.kt @@ -0,0 +1,62 @@ +package com.cubetiqs.graphql.demo.domain.account + +import com.cubetiqs.graphql.demo.domain.AbstractEntity +import org.hibernate.Hibernate +import org.springframework.data.annotation.CreatedDate +import org.springframework.data.annotation.LastModifiedDate +import java.math.BigDecimal +import java.util.* +import javax.persistence.* + +@Entity +@Table( + name = "accounts", indexes = [ + Index(name = "idx_account_id", columnList = "id") + ] +) +@EntityListeners(AccountEntityListener::class) +open class Account( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + + @Column + var balance: BigDecimal = BigDecimal.ZERO, + + @Column + var currentBalance: BigDecimal = BigDecimal.ZERO, + + @Column(length = 10) + @Enumerated(EnumType.STRING) + var accountType: AccountType = AccountType.BASIC, + + @Column(length = 3) + @Enumerated(EnumType.STRING) + var currency: AccountCurrency = AccountCurrency.USD, + + @Version + var version: Long? = null, + + @CreatedDate + @Temporal(TemporalType.TIMESTAMP) + var createdDate: Date? = null, + + @LastModifiedDate + @Temporal(TemporalType.TIMESTAMP) + var updatedDate: Date? = null, +) : AbstractEntity() { + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other == null || Hibernate.getClass(this) != Hibernate.getClass(other)) return false + other as Account + + return id != null && id == other.id + } + + override fun hashCode(): Int = 2083479647 + + @Override + override fun toString(): String { + return this::class.simpleName + "(id = $id , balance = $balance , currentBalance = $currentBalance , accountType = $accountType , currency = $currency , version = $version , createdDate = $createdDate , updatedDate = $updatedDate )" + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountCurrency.kt b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountCurrency.kt new file mode 100644 index 0000000..6c9bb3d --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountCurrency.kt @@ -0,0 +1,6 @@ +package com.cubetiqs.graphql.demo.domain.account + +enum class AccountCurrency { + USD, + KHR, +} \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountEntityListener.kt b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountEntityListener.kt new file mode 100644 index 0000000..855442b --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountEntityListener.kt @@ -0,0 +1,10 @@ +package com.cubetiqs.graphql.demo.domain.account + +import javax.persistence.PrePersist + +class AccountEntityListener { + @PrePersist + fun beforeSave(account: Account) { + + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountType.kt b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountType.kt new file mode 100644 index 0000000..f1306bf --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/account/AccountType.kt @@ -0,0 +1,7 @@ +package com.cubetiqs.graphql.demo.domain.account + +enum class AccountType { + BASIC, + PREMIUM, + BUSINESS, +} \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/graphql/demo/domain/user/User.kt b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/user/User.kt new file mode 100644 index 0000000..74ba91e --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/user/User.kt @@ -0,0 +1,51 @@ +package com.cubetiqs.graphql.demo.domain.user + +import com.cubetiqs.graphql.demo.domain.AbstractEntity +import org.springframework.data.annotation.CreatedDate +import org.springframework.data.annotation.LastModifiedDate +import java.util.* +import javax.persistence.* + +@Entity +@Table( + name = "users", indexes = [ + Index(name = "idx_user_id", columnList = "id"), + Index(name = "idx_user_code", columnList = "code"), + Index(name = "idx_user_username", columnList = "username"), + ] +) +@EntityListeners(UserEntityListener::class) +open class User( + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + var id: Long? = null, + + @Column(unique = true, length = 65, nullable = false) + var code: String? = null, + + @Column(unique = true, length = 35, nullable = false) + var username: String? = null, + + @Column(length = 100) + var password: String? = null, + + @Column(length = 50) + var name: String? = null, + + @Version + var version: Long? = null, + + @CreatedDate + @Temporal(TemporalType.TIMESTAMP) + var createdDate: Date? = null, + + @LastModifiedDate + @Temporal(TemporalType.TIMESTAMP) + var updatedDate: Date? = null, +) : AbstractEntity() { + @Transient + fun updatePassword(newPassword: String) { + // hash it here + this.password = newPassword + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/cubetiqs/graphql/demo/domain/user/UserEntityListener.kt b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/user/UserEntityListener.kt new file mode 100644 index 0000000..b69a723 --- /dev/null +++ b/src/main/kotlin/com/cubetiqs/graphql/demo/domain/user/UserEntityListener.kt @@ -0,0 +1,11 @@ +package com.cubetiqs.graphql.demo.domain.user + +import java.util.* +import javax.persistence.PrePersist + +class UserEntityListener { + @PrePersist + fun beforeSave(user: User) { + user.code = UUID.randomUUID().toString() + } +} \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index caedf9a..e3adbc4 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -8,6 +8,9 @@ spring: username: ${DB_USER:your-username} password: ${DB_PASSWORD:your-password} driver-class-name: org.postgresql.Driver + jpa: + hibernate: + ddl-auto: update # Spring Boot Actuator management: