Place Reviewer business logic implementation

As previously explained, in an application that adheres to the MVC design pattern, there are three primary components of consequence. These components are the model, view, and controller. The models are the components that are in charge of data management and the execution of business logic. In our Place Reviewer application, we are going to implement our models in the form of services that can be used across the backend. At this juncture, we need to create two fundamental services. The first to manage data pertaining to users of the application, and the second to manage review data.

First, we must create a UserService interface that defines the behaviors that must be implemented by a valid UserServiceImpl class. We previously stated in our use cases for the Place Reviewer application that a user must be able to register (hence create an account) on the platform. As such, we must cater for this process in our model. Create a service package in the project's root package. Now, add the UserService interface to it:

package com.example.placereviewer.service

interface UserService {

fun register(username: String, email: String, password: String): Boolean
}

We declared one method that must be implemented by a valid UserService. This method is the register (String, String, String) method. register() takes three strings as arguments. The first is the username of the user to be registered, the second is a valid email address for the user, and the third is his/her password of choice. When invoked with appropriate arguments, register() attempts to register the user with his/her provided credentials and returns true if the user was registered successfully; otherwise, it returns false.

The following is the implementation of the preceding UserService. Add it to the service package:

package com.example.placereviewer.service

import com.example.placereviewer.data.model.User
import com.example.placereviewer.data.repository.UserRepository
import org.springframework.stereotype.Service

@Service
class UserServiceImpl(val userRepository: UserRepository) : UserService {

override fun register(username: String, email: String,
password: String): Boolean {
val user = User(email, username, password)
userRepository.save(user)

return true
}

}

The workings of the register() function implemented by our UserServiceImpl class is straightforward. When valid username, email, and password arguments are passed to it, it creates a new object of the user - passing the appropriate arguments to its constructor. After the creation of an object of the user, the user is saved to the database with the following line:

userRepository.save(user)

userRepository is an instance of the UserRepository we created earlier. This instance is injected into the constructor of UserServiceImpl automatically by Spring Framework. Once the user has been saved to the database, the Boolean value true is returned.

Up next is the implementation of a review service interface. Our review service must facilitate the creation of reviews and the listing of reviews that have been created by users of the platform. As a consequence of this requirement, we will mandate the implementation of the createReview() and listReview() methods in our user service interface.

Add the following ReviewService interface to the service package of the project:

package com.example.placereviewer.service

import com.example.placereviewer.data.model.Review

interface ReviewService {

fun createReview(reviewerUsername: String, reviewData: Review): Boolean

fun listReviews(): Iterable<Review>
}

The following is the ReviewServiceImpl class for the service we have just created. Add it, as well as all the services we will create later on in this chapter, to com.example.placereviewer.service:

package com.example.placereviewer.service

import com.example.placereviewer.data.model.Review
import com.example.placereviewer.data.model.User
import com.example.placereviewer.data.repository.ReviewRepository
import com.example.placereviewer.data.repository.UserRepository
import org.springframework.stereotype.Service

@Service
class ReviewServiceImpl(val reviewRepository: ReviewRepository, val userRepository: UserRepository) : ReviewService {

override fun listReviews(): Iterable<Review> {
return reviewRepository.findAll()
}

override fun createReview(reviewerUsername: String,
reviewData: Review): Boolean {
val reviewer: User? = userRepository.findByUsername(reviewerUsername)

if (reviewer != null) {
reviewData.reviewer = reviewer
reviewRepository.save(reviewData)
return true
}

return false
}
}

listReviews() returns an iterable containing all the review data that has been stored within the application's database. createReview(), on the other hand, takes a string whose value is the username of the user creating the review and an instance of Review containing the data for the review to be created. createReview() first retrieves the user with the specified username by invoking the findByUsername() method of UserRepository. This user retrieved is the creator of the review—hence, the reviewer.

If a null object is not returned by UserRepository, the user exists and as such, the retrieved user is assigned to the reviewer property of the review to be saved. After this assignment, the review is saved to the database and the function returns true – signifying that the process was successful. If no user with the username provided was found, false is returned by createReview().

Having created appropriate models in the form of services, let us work on securing our Place Reviewer application. This is an important procedure as we do not want unauthorized individuals to be able to access our application resources.

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

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