Creating repositories

Create a repository for a profile named ProfileRepository.kt and implement the JpaRepository repository that has all the necessary CRUD request methods to fetch the database. Here is the code for this class:

@Repository
interface ProfileRepository : JpaRepository<Profile, Long>

Now create a repository for a post named PostRepository.kt and implement the JpaRepository repository that has all the necessary CRUD request methods to fetch the database. Here is the code for this class:

@Repository
interface PostRepository : JpaRepository<Post, Long>

Then create a repository for a comment named CommentRepository.kt and implement the JpaRepository<> repository that has all the necessary CRUD request methods to fetch the database. Here is the code for this class:

@Repository
interface CommentRepository : JpaRepository<Comment, Long>

Lastly, create a repository for the like model named LikeRepository.kt and implement the JpaRepository<> repository that has all the necessary CRUD request methods to fetch the database. Here is the code for this class:

@Repository
interface LikeRepository : JpaRepository<LikeObj, Long>

To delete all the data regarding the deleted post, we need to create a repository for the profile named DeletePCLRepository.kt and implement an interface named DeletePCLByIDInterface.kt with one function, which will delete all the data regarding the deleted user. Here is the code for the interface:

interface DeletePCLByIDInterface {
fun deleteAllUsersInfoByUserID(userID: Long): Any
}

Here is the code for the DeletePCLRepository.kt class:

@Repository
class DeletePCLRepository : DeletePCLByIDInterface {

@Autowired
private lateinit var jdbcTemplate: JdbcTemplate

override fun deleteAllUsersInfoByUserID(userID: Long): Any {

val deletePosts = "DELETE FROM post, comment WHERE profile_id = ?;"
val deleteComments = "DELETE FROM comment WHERE profile_id = ?"
val deleteLikes = "DELETE FROM like_obj WHERE profile_id = ?"

jdbcTemplate.update(deletePosts, userID)
jdbcTemplate.update(deleteComments, userID)
jdbcTemplate.update(deleteLikes, userID)

return "DONE"
}
}

To check a registered user, create a repository named UserExistRepository.kt and implement an interface named UserExistInterface.kt with two functions.

Here is the code for the interface:

interface UserExistInterface{
fun isUserExist(name: String): Boolean
}

In this interface, isUserExist(username: String) will search the Profile table of the database and return a Boolean based on the existing of the user. 

Here is the code for the UserExistRepository.kt class:

@Repository
class UserExistRepository: UserExistInterface {
@Autowired
private lateinit var jdbcTemplate: JdbcTemplate

override fun isUserExist(name: String): Boolean {
val sql = "SELECT count(*) FROM PROFILE WHERE username = ?"
val count = jdbcTemplate.queryForObject(sql, Int::class.java, name)
return count != 0
}
}

In this class, we add the @Autowired annotation to autowire the JdbcTemplate to utilize the JDBC database. We override the issue exist(name: String) function.

"SELECT count(*) FROM PROFILE WHERE username = ?" is an SQL query that is used to search the existing users from the Profile table of the database. If there is a user, then it will return true.

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

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