aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/submelon/pantry/PantryItemController.java
blob: 37a71e9cce02d3b5b5eb22b28cb89e6b61aaf5bd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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();
    }
    
}