Created
September 13, 2019 00:08
-
-
Save dubeboy/fe23938c11035a5b8ab599c147200547 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
package za.co.dubedivine.groceryapp.controller | |
import org.springframework.http.HttpStatus | |
import org.springframework.http.ResponseEntity | |
import org.springframework.web.bind.annotation.* | |
import za.co.dubedivine.groceryapp.model.GroceryItem | |
import za.co.dubedivine.groceryapp.model.responseModel.StatusResponseEntity | |
import za.co.dubedivine.groceryapp.repository.GroceryItemRepository | |
@RestController | |
@RequestMapping("groceries") | |
class GroceriesController(private val groceryRepository: GroceryItemRepository) { | |
@GetMapping | |
fun getGroceriesList(): MutableIterable<GroceryItem> { | |
return groceryRepository.findAll() | |
} | |
@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) | |
} | |
@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 | |
) | |
} | |
} | |
} | |
@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) | |
} | |
} | |
} | |
} |
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
package za.co.dubedivine.groceryapp.model | |
import javax.persistence.Entity | |
import javax.persistence.GeneratedValue | |
import javax.persistence.GenerationType | |
import javax.persistence.Id | |
@Entity | |
data class GroceryItem(var name: String, var isAvailable: Boolean) { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
var id: Long = 0L | |
constructor(): this ("", false) | |
} |
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
package za.co.dubedivine.groceryapp.model.responseModel | |
data class StatusResponseEntity<T>(val status: Boolean, val message: String, val entity: T?) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment