After creating the repository class, let's create the service class where we will autowire the repository class using the @autowired annotation. Let's create a service class named UserService.kt with the @Service annotation that implements the UserInterface and overrides all functions.
Here is the piece of code for the UserService.kt:
@Service
class UserService: UsersInterface {
@Autowired
private lateinit var userRepository: UserRepository
------
------
}
Let's override and modify the functions with the help of UserRepository. Here is the full code of the UserService class:
@Service
class UserService: UsersInterface {
@Autowired
private lateinit var userRepository: UserRepository
override fun getAllUserList(): List<UserModel> {
return userRepository.getAllUserList()
}
override fun getUserByID(id: Int): UserModel? {
return userRepository.getUserByID(id)
}
override fun addNewUser(userModel: UserModel) {
userRepository.addNewUser(userModel)
}
override fun updateUser(userModel: UserModel, id: Int) {
userRepository.updateUser(userModel, id)
}
override fun deleteUser(id: Int) {
userRepository.deleteUser(id)
}
}
- getAllUserList(): This function will fetch all the users
- getUserByID(id: Int): This function will fetch a user by ID
- addNewUser(userModel: UserModel): This function will insert a new user
- updateUser(userModel: UserModel, id: Int): This function will update a user by ID
- deleteUser(id: Int): This function will delete a user by ID