Last active
September 15, 2019 15:05
-
-
Save dubeboy/663d2a923dacda98794bbbbbeb3c2b03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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