Skip to content

Instantly share code, notes, and snippets.

@dubeboy
Last active September 15, 2019 15:05
Show Gist options
  • Save dubeboy/663d2a923dacda98794bbbbbeb3c2b03 to your computer and use it in GitHub Desktop.
Save dubeboy/663d2a923dacda98794bbbbbeb3c2b03 to your computer and use it in GitHub Desktop.
@PutMapping
fun addGroceryItem(@RequestBody groceryItem: GroceryItem): ResponseEntity<StatusResponseEntity<GroceryItem>> {
val savedItem = groceryRepository.save(groceryItem)
return ResponseEntity(StatusResponseEntity(
true,
"Added new grocery item to your list",
savedItem
), HttpStatus.CREATED)
}
@PostMapping("/available")
fun toggleGroceryItemAvailability(@RequestBody availability: Boolean, @RequestParam("id") id: Long): ResponseEntity<StatusResponseEntity<Boolean>> {
val item = groceryRepository.findById(id)
return when {
item.isPresent -> {
item.get().isAvailable = availability
groceryRepository.save(item.get())
ResponseEntity(StatusResponseEntity(true, "item ${item.get().name} is now ${if (availability) "available" else "finished"} ", true), HttpStatus.OK)
}
else -> {
ResponseEntity(StatusResponseEntity(false, "sorry could not find that item", false), HttpStatus.NOT_FOUND)
}
}
}
@DeleteMapping("/{id}")
fun deleteGroceryItem(@PathVariable("id") id: Long): ResponseEntity<StatusResponseEntity<Boolean>> {
val item = groceryRepository.findById(id)
return when {
item.isPresent -> {
ResponseEntity(
StatusResponseEntity(true, "deleted item ${item.get().name} from list ", true),
HttpStatus.OK
)
}
else -> {
ResponseEntity<StatusResponseEntity<Boolean>>(
StatusResponseEntity(false, "sorry could not find item to delete", false),
HttpStatus.NOT_FOUND
)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment