aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/dev/submelon/pantry/ItemController.java
blob: 7021f0a46d5bdcb3487023e839fc4cc214f56a45 (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 ItemController {
    @Autowired
    private ItemRepository itemRepository;

    @PostMapping(path="/add")
    @ResponseBody
    Integer addNewItem(@RequestParam String name, @RequestParam String description, @RequestParam String shortid, @RequestParam double quantity) {
        Item item = new Item();
        item.setName(name);
        item.setDescription(description);
        item.setShortid(shortid);
        item.setQuantity(quantity);

        Item updatedItem = itemRepository.save(item);
        return updatedItem.getId();
    }

    @GetMapping(path="")
    @ResponseBody
    Iterable<Item> getAllItems() {
        return itemRepository.findAll();
    }
    
    @GetMapping(path="/{shortid}")
    @ResponseBody
    Item getByShortid(@PathVariable String shortid) {
        return itemRepository.findByShortid(shortid);
    }
    
}