Creating a profile's HTTP requests

Now create HTTP function requests for the profiles. 

Here is the function for creating a profile's POST request:

// New Profile registration
@PostMapping("/profile/new")
fun registerUser(@RequestBody profile: Profile): Any {
if (!userExist.isUserExist(profile.username)) {
profile.password = passwordEncoder.encode(profile.password)
profileRepository.save(profile)
return profile
}
return "{"duplicate": "${profile.username} is taken. Try another"}"
}

Here is the function for creating a profile's GET request:

// Get Profile by ID
@GetMapping("/profile/{id}")
fun getUserById(@PathVariable("id") id: Long): Any {
return profileRepository.findById(id)
}

Here is the function for creating a profile's PUT request:

//     Update Profile by ID
@PutMapping("/profile/{id}")
fun updateUserById(@PathVariable("id") id: Long, @RequestBody mUser: Profile): Any {
val profile = profileRepository.getOne(id)
if (mUser.firstName != null) profile.firstName = mUser.firstName
if (mUser.lastName != null) profile.lastName = mUser.lastName
if (mUser.contactNumber != null) profile.contactNumber = mUser.contactNumber
if (mUser.city != null) profile.city = mUser.city
if (mUser.country != null) profile.country = mUser.country
return profileRepository.save(profile)
}

Here is the function for creating a profile's DELETE request:

// Delete Profile by ID
@DeleteMapping("/profile/{userId}")
fun deleteUserById(@PathVariable("userId") userId: Long): Any {
deletePCLRepository.deleteAllUsersInfoByUserID(userId)
return profileRepository.deleteById(userId)
}
..................Content has been hidden....................

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