Creating models

We previously identified two distinct types of entities that must be catered for in our system: the User entity and the Review entity. It is time to create appropriate models for these entities. The first of these entities we will concern ourselves with is the User. Create a data package within the com.example.placereviewer package. Add a model package within the newly created data package. Now, add a User.kt file within the newly created com.example.placereviewer.data.model package with the following content:

package com.example.placereviewer.data.model

import com.example.placereviewer.listener.UserListener
import org.springframework.format.annotation.DateTimeFormat
import java.time.Instant
import java.util.*
import javax.persistence.*
import javax.validation.constraints.Pattern
import javax.validation.constraints.Size


@Entity
@Table(name = "`user`")
@EntityListeners(UserListener::class)
data class User(
@Column(unique = true)
@Size(min = 2)
@Pattern(regexp = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z] {2,6}$")
var email: String = "",
@Column(unique = true)
var username: String = "",
@Size(min = 60, max = 60)
var password: String = "",
@Column(name = "account_status")
@Pattern(regexp = "\A(activated|deactivated)\z")
var accountStatus: String = "activated",
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
var id: Long = 0,
@DateTimeFormat
@Column(name = "created_at")
var createdAt: Date = Date.from(Instant.now())
) {
@OneToMany(mappedBy = "reviewer", targetEntity = Review::class)
private var reviews: Collection<Review>? = null
}

We don't need spend time explaining much of what is going on in the preceding code snippet as we have prior experience with the creation of entities in Spring. In the preceding snippet, we defined a User entity with email, username, password, accountStatus, Id, and createdAt properties as its attributes. In addition, we specified that a User has many Review entities. We also specified an entity listener for the entity with the @EntityListener annotation. We have created neither a Review entity nor a UserListener for the User entity. As we are still focused on the User entity, let us focus on creating its entity listener before concerning ourselves with the Review entity. Add a new listener package to com.example.placereviewer and add a UserListener.kt file to it containing the following code:

package com.example.placereviewer.listener

import com.example.placereviewer.data.model.User
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import javax.persistence.PrePersist
import javax.persistence.PreUpdate

class UserListener {

@PrePersist
@PreUpdate
fun hashPassword(user: User) {
user.password = BCryptPasswordEncoder().encode(user.password)
}
}

UserListener has a single hashPassword function, which is invoked before persisting and before updating a User entity. The method has the single job of encoding the password property of a user into its bcrypt equivalent before persisting it in the database.

Having created the necessary listeners for the User entity, let us turn our attention to the definition of a Review entity. Create a Review.kt file in com.example.placereviewer.data.models with the following content:

package com.example.placereviewer.data.model

import org.springframework.format.annotation.DateTimeFormat
import java.time.Instant
import java.util.*
import javax.persistence.*
import javax.validation.constraints.Size

@Entity
@Table(name = "`review`")
data class Review(
@ManyToOne(optional = false)
@JoinColumn(name = "user_id", referencedColumnName = "id")
var reviewer: User? = null,
@Size(min = 5)
var title: String = "",
@Size(min = 10)
var body: String = "",
@Column(name = "place_address")
@Size(min = 2)
var placeAddress: String = "",
@Column(name = "place_name")
var placeName: String = "",
@Column(name = "place_id")
var placeId: String = "",
var latitude: Double = 0.0,
var longitude: Double = 0.0,
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
var id: Long = 0,
@DateTimeFormat
@Column(name = "created_at")
var createdAt: Date = Date.from(Instant.now())
)

As can be seen in the preceding code snippet, we created a Review data class with the following properties: reviewer, title, body, placeAddress, placeName, placeId, latitude, longitude, id, and createdAt. The reviewer property is of the type User. It references the creator of the review. Every review must be created by a user. In addition, many reviews are created by a single user. We use the @ManyToOne annotation to properly declare this relationship between the Review and User entities.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.18.101