Task: Add domain entities with account and user for graphql demo

This commit is contained in:
Sambo Chea 2021-08-07 10:51:56 +07:00
parent 2d7d87c3f8
commit 7293a05766
8 changed files with 157 additions and 0 deletions

View File

@ -0,0 +1,7 @@
package com.cubetiqs.graphql.demo.domain
import java.io.Serializable
import javax.persistence.MappedSuperclass
@MappedSuperclass
abstract class AbstractEntity<T, ID : Serializable> : Serializable

View File

@ -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<Account, Long>() {
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 )"
}
}

View File

@ -0,0 +1,6 @@
package com.cubetiqs.graphql.demo.domain.account
enum class AccountCurrency {
USD,
KHR,
}

View File

@ -0,0 +1,10 @@
package com.cubetiqs.graphql.demo.domain.account
import javax.persistence.PrePersist
class AccountEntityListener {
@PrePersist
fun beforeSave(account: Account) {
}
}

View File

@ -0,0 +1,7 @@
package com.cubetiqs.graphql.demo.domain.account
enum class AccountType {
BASIC,
PREMIUM,
BUSINESS,
}

View File

@ -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<User, Long>() {
@Transient
fun updatePassword(newPassword: String) {
// hash it here
this.password = newPassword
}
}

View File

@ -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()
}
}

View File

@ -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: