-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathItemController.java
More file actions
90 lines (72 loc) · 2.59 KB
/
ItemController.java
File metadata and controls
90 lines (72 loc) · 2.59 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.ironhack.helloSupermarket.controller;
import com.ironhack.helloSupermarket.enums.Category;
import com.ironhack.helloSupermarket.model.Item;
import com.ironhack.helloSupermarket.model.ProduceStock;
import com.ironhack.helloSupermarket.service.ItemService;
import jakarta.validation.Valid;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import java.net.URI;
import java.util.List;
@RestController
public class ItemController {
private final ItemService itemService;
public ItemController(ItemService itemService){
this.itemService = itemService;
}
//crud
//create
@PostMapping("/inventory")
public ResponseEntity<Item> create(@Valid @RequestBody Item item){
Item createdItem = itemService.create(item);
URI location = ServletUriComponentsBuilder
.fromCurrentRequest()
.path("/{name}")
.buildAndExpand(createdItem.getName())
.toUri();
return ResponseEntity.created(location).body(createdItem);
}
//read
@GetMapping("/inventory/{id}")
public Item findById(@PathVariable Long id){
return itemService.findById(id);
}
//read all
@GetMapping("/inventory")
public List<Item> readAll (){
return itemService.readAll();
}
//update
@PutMapping("/inventory/{id}")
public Item update(@PathVariable Long id, @Valid @RequestBody Item item){
return itemService.update(id, item);
}
//delete
@DeleteMapping("/inventory/{id}")
public void delete(@PathVariable Long id){
itemService.delete(id);
}
//repository custom methods
@GetMapping("/inventory/category/{categoryString}")
public List<Item> findItemsByCategory(@PathVariable String categoryString) {
Category categoryEnum = Category.valueOf(categoryString.toUpperCase());
return itemService.findItemsByCategory(categoryEnum);
}
@GetMapping("/inventory/low-stock")
public List<Item> findLowStock(){
return itemService.findLowStock();
};
@GetMapping("/inventory/to-discount/{price}")
public List<Item> findItemsToDiscount(@PathVariable double price){
return itemService.findItemsToDiscount(price);
};
@GetMapping("inventory/alphabetically")
public List<Item> sortByName(){
return itemService.sortByName();
}
@GetMapping("/inventory/produce-stock")
public List<ProduceStock> getProduceStock(){
return itemService.getProduceStock();
}
}