Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions .idea/ExerciseCheckPointSpringJPA.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/data_source_mapping.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 20 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/sqldialects.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions helloSupermarket.iml
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>
5 changes: 5 additions & 0 deletions src/http-requests.http
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
Copy link
Contributor

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?

.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());
Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/ironhack/helloSupermarket/enums/Category.java
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
}
80 changes: 80 additions & 0 deletions src/main/java/com/ironhack/helloSupermarket/model/Item.java
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;
}
}
Loading