-
Notifications
You must be signed in to change notification settings - Fork 10
final #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
final #4
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <module version="4"> | ||
| <component name="FacetManager"> | ||
| <facet type="jpa" name="JPA"> | ||
| <configuration> | ||
| <setting name="validation-enabled" value="true" /> | ||
| <setting name="provider-name" value="Hibernate" /> | ||
| <datasource-mapping> | ||
| <factory-entry name="entityManagerFactory" value="004f8ecb-91a0-4ba4-b379-2c3e7fa5293e" /> | ||
| </datasource-mapping> | ||
| <naming-strategy-map /> | ||
| </configuration> | ||
| </facet> | ||
| </component> | ||
| </module> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| ### GET request to example server | ||
| GET https://examples.http-client.intellij.net/get | ||
| ?generated-in=IntelliJ IDEA | ||
|
|
||
| ### |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package com.ironhack.helloSupermarket; | ||
|
|
||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
|
||
| @SpringBootApplication | ||
| public class HelloSupermarketApplication { | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication.run(HelloSupermarketApplication.class, args); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not as we have seen it but it can work |
||
| 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(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| package com.ironhack.helloSupermarket.enums; | ||
|
|
||
| public enum Category { | ||
| PRODUCE, | ||
| DIARY, | ||
| BAKERY, | ||
| BEVERAGES, | ||
| PANTRY, | ||
| FROZEN | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| package com.ironhack.helloSupermarket.model; | ||
|
|
||
| import com.ironhack.helloSupermarket.enums.Category; | ||
| import jakarta.persistence.*; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.NotNull; | ||
| import jakarta.validation.constraints.Positive; | ||
| import jakarta.validation.constraints.PositiveOrZero; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| @Entity | ||
| @Table(name = "item") | ||
| public class Item { | ||
| @Id | ||
| @GeneratedValue(strategy = GenerationType.IDENTITY) | ||
| private Long id; | ||
|
|
||
|
|
||
| @NotBlank (message = "Name is required") | ||
| private String name; | ||
|
|
||
| @NotNull (message = "Category is required") | ||
| @Enumerated(EnumType.STRING) | ||
| private Category category; | ||
|
|
||
| @Positive (message = "Price must be bigger than 0") | ||
| private double price; | ||
|
|
||
| @PositiveOrZero (message = "Stock value cannot be negative") | ||
| private int stock; | ||
|
|
||
|
|
||
| //getters and setters | ||
| public Long getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public Category getCategory() { | ||
| return category; | ||
| } | ||
|
|
||
| public void setCategory(Category category) { | ||
| this.category = category; | ||
| } | ||
|
|
||
| public double getPrice() { | ||
| return price; | ||
| } | ||
|
|
||
| public void setPrice(double price) { | ||
| this.price = price; | ||
| } | ||
|
|
||
| public int getStock() { | ||
| return stock; | ||
| } | ||
|
|
||
| public void setStock(int stock) { | ||
| this.stock = stock; | ||
| } | ||
|
|
||
| //constructors | ||
| public Item (){} | ||
|
|
||
| public Item(String name, Category category, double price, int stock) { | ||
| this.name = name; | ||
| this.category = category; | ||
| this.price = price; | ||
| this.stock = stock; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.ironhack.helloSupermarket.model; | ||
|
|
||
| public class ProduceStock { | ||
| private String name; | ||
| private int stock; | ||
|
|
||
| public ProduceStock(String name, int stock){ | ||
| this.name = name; | ||
| this.stock = stock; | ||
| } | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public void setName(String name) { | ||
| this.name = name; | ||
| } | ||
|
|
||
| public int getStock() { | ||
| return stock; | ||
| } | ||
|
|
||
| public void setStock(int stock) { | ||
| this.stock = stock; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice, we have not seen this in class. Do you know why this is interesting to do?