diff options
| author | Kevin J Hoerr <khoerr@ksmpartners.com> | 2022-01-27 14:12:29 -0500 |
|---|---|---|
| committer | Kevin J Hoerr <khoerr@ksmpartners.com> | 2022-01-27 14:12:29 -0500 |
| commit | 615c8a0fc8d2b35da7df90115456d33444104c41 (patch) | |
| tree | aeab4261b4edaf63905bd008ce6992b0d2490916 /src/main/java/dev/submelon/pantry/PantryItemController.java | |
| parent | 8d8d8cdd639591c0c4c49b355db1152e310c22d6 (diff) | |
| download | pantry-615c8a0fc8d2b35da7df90115456d33444104c41.tar.gz pantry-615c8a0fc8d2b35da7df90115456d33444104c41.tar.bz2 pantry-615c8a0fc8d2b35da7df90115456d33444104c41.zip | |
Add error handling
Diffstat (limited to 'src/main/java/dev/submelon/pantry/PantryItemController.java')
| -rw-r--r-- | src/main/java/dev/submelon/pantry/PantryItemController.java | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/src/main/java/dev/submelon/pantry/PantryItemController.java b/src/main/java/dev/submelon/pantry/PantryItemController.java new file mode 100644 index 0000000..37a71e9 --- /dev/null +++ b/src/main/java/dev/submelon/pantry/PantryItemController.java @@ -0,0 +1,43 @@ +package dev.submelon.pantry; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +@RequestMapping(path="/items") +public class PantryItemController { + @Autowired + private PantryItemRepository itemRepository; + + @PostMapping(path="/add") + @ResponseBody + Integer addNewItem(@RequestParam String name, @RequestParam String description, @RequestParam String shortid, @RequestParam double quantity) { + PantryItem item = new PantryItem(); + item.setName(name); + item.setDescription(description); + item.setShortid(shortid); + item.setQuantity(quantity); + + PantryItem updatedItem = itemRepository.save(item); + return updatedItem.getId(); + } + + @GetMapping(path="") + @ResponseBody + Iterable<PantryItem> getAllItems() { + return itemRepository.findAll(); + } + + @GetMapping(path="/{shortid}") + @ResponseBody + PantryItem getByShortid(@PathVariable String shortid) { + return itemRepository.findByShortid(shortid).get(); + } + +} |
