Task: Add domain entities with account and user for graphql demo
This commit is contained in:
parent
2d7d87c3f8
commit
7293a05766
@ -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
|
@ -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 )"
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package com.cubetiqs.graphql.demo.domain.account
|
||||||
|
|
||||||
|
enum class AccountCurrency {
|
||||||
|
USD,
|
||||||
|
KHR,
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package com.cubetiqs.graphql.demo.domain.account
|
||||||
|
|
||||||
|
import javax.persistence.PrePersist
|
||||||
|
|
||||||
|
class AccountEntityListener {
|
||||||
|
@PrePersist
|
||||||
|
fun beforeSave(account: Account) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.cubetiqs.graphql.demo.domain.account
|
||||||
|
|
||||||
|
enum class AccountType {
|
||||||
|
BASIC,
|
||||||
|
PREMIUM,
|
||||||
|
BUSINESS,
|
||||||
|
}
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,9 @@ spring:
|
|||||||
username: ${DB_USER:your-username}
|
username: ${DB_USER:your-username}
|
||||||
password: ${DB_PASSWORD:your-password}
|
password: ${DB_PASSWORD:your-password}
|
||||||
driver-class-name: org.postgresql.Driver
|
driver-class-name: org.postgresql.Driver
|
||||||
|
jpa:
|
||||||
|
hibernate:
|
||||||
|
ddl-auto: update
|
||||||
|
|
||||||
# Spring Boot Actuator
|
# Spring Boot Actuator
|
||||||
management:
|
management:
|
||||||
|
Loading…
Reference in New Issue
Block a user