Creation of base template (#1)

* Sample template created

* added findByName functionality for item

* Solve Cors errors and inhibit DefaultExposure

* changed project structure

* Added frontend
This commit is contained in:
cato
2022-06-01 01:36:40 +02:00
committed by GitHub
parent edaf3c557e
commit cb74fe133c
32 changed files with 7028 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
package whattocook.controller;
import whattocook.exception.ItemNotFoundException;
import lombok.extern.slf4j.Slf4j;
import whattocook.models.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import whattocook.models.Unit;
import whattocook.services.ItemService;
import java.util.List;
@Slf4j
@RestController()
@RequestMapping("/api/v1")
public class ItemController {
@Autowired
private ItemService itemService;
@GetMapping("/item")
public Item getItem(@RequestParam String name){
return itemService.findByName(name).orElseThrow(() -> new ItemNotFoundException("item " + name + " not found"));
}
@GetMapping("/items")
public List<Item> getItemList() {
return itemService.findAll();
}
@GetMapping("/items/{itemId}")
public Item getItem(@PathVariable(value = "itemId") Long itemId) {
return itemService.findById(itemId).orElseThrow(() -> new ItemNotFoundException("itemId " + itemId + " not found"));
}
@PostMapping("/items")
public Item createItem(@RequestBody Item item) {
itemService.save(item);
return item;
}
@PutMapping("/items/{itemId}")
public String updateItem(@PathVariable(value = "itemId") Long itemId, @RequestBody Item item) {
return itemService.findById(itemId).map(i -> {
i.setName(item.getName());
i.setQuantity(item.getQuantity());
i.setUnit(item.getUnit());
itemService.save(i);
return "Item updated";
}).orElseThrow(() -> new ItemNotFoundException("itemId " + itemId + " not found"));
}
@DeleteMapping("/items/{itemId}")
public String deleteItem(@PathVariable(value = "itemId") Long itemId) {
return itemService.findById(itemId).map(p -> {
itemService.deleteById(itemId);
return "Item deleted";
}).orElseThrow(() -> new ItemNotFoundException("itemId " + itemId + " not found"));
}
}