-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
510 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/main/java/com/samcodesthings/shelfliferestapi/controller/PantryController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.samcodesthings.shelfliferestapi.controller; | ||
|
||
import com.samcodesthings.shelfliferestapi.dto.HouseholdDTO; | ||
import com.samcodesthings.shelfliferestapi.dto.PantryItemDTO; | ||
import com.samcodesthings.shelfliferestapi.dto.ProductDTO; | ||
import com.samcodesthings.shelfliferestapi.service.PantryService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.modelmapper.ModelMapper; | ||
import org.modelmapper.TypeToken; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.validation.Valid; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@CrossOrigin(origins = "*", maxAge = 3600) | ||
@RestController | ||
@RequestMapping("/pantry") | ||
public class PantryController { | ||
|
||
@Autowired | ||
PantryService pantryService; | ||
|
||
@Autowired | ||
ModelMapper modelMapper; | ||
|
||
@GetMapping(path = "/ping", produces = MediaType.APPLICATION_JSON_VALUE) | ||
public Map<String, String> ping() { | ||
log.info("[GET] ping"); | ||
return Collections.singletonMap("response", "pong"); | ||
} | ||
|
||
@GetMapping | ||
public List<PantryItemDTO> getPantryItems() { | ||
List<PantryItemDTO> list = modelMapper.map(pantryService.getPantryItemsByHousehold(), new TypeToken<List<PantryItemDTO>>() {}.getType()); | ||
log.info("Mapped items: " + list); | ||
return list; | ||
} | ||
|
||
@PostMapping | ||
public PantryItemDTO savePantryItem(@Valid @RequestBody PantryItemDTO pantryItemDTO) { | ||
return modelMapper.map(pantryService.savePantryItem(pantryItemDTO), PantryItemDTO.class); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/samcodesthings/shelfliferestapi/controller/ProductController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.samcodesthings.shelfliferestapi.controller; | ||
|
||
import com.samcodesthings.shelfliferestapi.dto.ProductDTO; | ||
import com.samcodesthings.shelfliferestapi.service.ProductService; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.Map; | ||
|
||
@Slf4j | ||
@CrossOrigin(origins = "*", maxAge = 3600) | ||
@RestController | ||
@RequestMapping("/product") | ||
public class ProductController { | ||
|
||
@Autowired | ||
ProductService productService; | ||
|
||
@GetMapping("/scan{code}") | ||
public ProductDTO scanItem(@PathVariable("code") String code) throws Exception { | ||
log.info("[GET] Scan Item"); | ||
return productService.scanProduct(code); | ||
} | ||
|
||
@PostMapping("/request") | ||
public ProductDTO requestProductToBeAdded(@RequestBody ProductDTO requestedProductDTO) throws Exception { | ||
log.info("[POST] Adding Request for Product: " + requestedProductDTO.getName()); | ||
return productService.requestProductToBeAdded(requestedProductDTO); | ||
} | ||
|
||
} |
17 changes: 17 additions & 0 deletions
17
src/main/java/com/samcodesthings/shelfliferestapi/dao/PantryDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.samcodesthings.shelfliferestapi.dao; | ||
|
||
import com.samcodesthings.shelfliferestapi.model.Household; | ||
import com.samcodesthings.shelfliferestapi.model.PantryItem; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
@Repository | ||
public interface PantryDAO extends CrudRepository<PantryItem, String> { | ||
|
||
Optional<PantryItem> findById(String id); | ||
Set<PantryItem> findPantryItemsByHousehold(Household household); | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/samcodesthings/shelfliferestapi/dao/ProductDAO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.samcodesthings.shelfliferestapi.dao; | ||
|
||
import com.samcodesthings.shelfliferestapi.model.PantryItem; | ||
import com.samcodesthings.shelfliferestapi.model.Product; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
@Repository | ||
public interface ProductDAO extends CrudRepository<Product, String> { | ||
|
||
Optional<Product> findById(String id); | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/samcodesthings/shelfliferestapi/dto/OpenFoodFactsItemDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.samcodesthings.shelfliferestapi.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Data; | ||
|
||
@Data | ||
public class OpenFoodFactsItemDTO { | ||
|
||
@JsonProperty("code") | ||
private String code; | ||
|
||
@JsonProperty("product_name") | ||
private String name; | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/samcodesthings/shelfliferestapi/dto/OpenFoodFactsResponseDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.samcodesthings.shelfliferestapi.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Data; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
public class OpenFoodFactsResponseDTO { | ||
|
||
@JsonProperty("count") | ||
private int count; | ||
|
||
@JsonProperty("page") | ||
private int page; | ||
|
||
@JsonProperty("page_count") | ||
private int pageCount; | ||
|
||
@JsonProperty("page_size") | ||
private int pageSize; | ||
|
||
@JsonProperty("products") | ||
private List<OpenFoodFactsItemDTO> products; | ||
|
||
@JsonProperty("skip") | ||
private int skip; | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/samcodesthings/shelfliferestapi/dto/PantryItemDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.samcodesthings.shelfliferestapi.dto; | ||
|
||
import lombok.Data; | ||
|
||
import java.util.Date; | ||
|
||
@Data | ||
public class PantryItemDTO { | ||
|
||
private String id; | ||
|
||
private ProductDTO product; | ||
|
||
private Date expirationDate; | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/samcodesthings/shelfliferestapi/dto/ProductDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.samcodesthings.shelfliferestapi.dto; | ||
|
||
import lombok.Data; | ||
|
||
@Data | ||
public class ProductDTO { | ||
|
||
private String code; | ||
|
||
private String name; | ||
|
||
private boolean isPendingRequestGrant; | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/samcodesthings/shelfliferestapi/exception/NotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.samcodesthings.shelfliferestapi.exception; | ||
|
||
public class NotFoundException extends Exception { | ||
public NotFoundException(String errorMessage) { | ||
super(errorMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
src/main/java/com/samcodesthings/shelfliferestapi/model/PantryItem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.samcodesthings.shelfliferestapi.model; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.Data; | ||
|
||
import java.util.Date; | ||
|
||
@Entity | ||
@Table(name = "pantry_items") | ||
@Data | ||
public class PantryItem { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.UUID) | ||
private String id; | ||
|
||
@ManyToOne | ||
private Product product; | ||
|
||
@ManyToOne | ||
private Household household; | ||
|
||
private Date expirationDate; | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/samcodesthings/shelfliferestapi/model/Product.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.samcodesthings.shelfliferestapi.model; | ||
|
||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.Table; | ||
import lombok.Data; | ||
|
||
@Entity | ||
@Table(name = "products") | ||
@Data | ||
public class Product { | ||
|
||
@Id | ||
private String code; | ||
|
||
private String name; | ||
|
||
private boolean isPendingRequestGrant; | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/com/samcodesthings/shelfliferestapi/service/OpenFoodFactsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.samcodesthings.shelfliferestapi.service; | ||
|
||
import com.samcodesthings.shelfliferestapi.dto.OpenFoodFactsItemDTO; | ||
|
||
import java.util.Optional; | ||
|
||
public interface OpenFoodFactsService { | ||
|
||
Optional<OpenFoodFactsItemDTO> getItemByCode(String code); | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/com/samcodesthings/shelfliferestapi/service/PantryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.samcodesthings.shelfliferestapi.service; | ||
|
||
import com.samcodesthings.shelfliferestapi.dto.PantryItemDTO; | ||
import com.samcodesthings.shelfliferestapi.dto.ProductDTO; | ||
import com.samcodesthings.shelfliferestapi.exception.NotFoundException; | ||
import com.samcodesthings.shelfliferestapi.model.PantryItem; | ||
import com.samcodesthings.shelfliferestapi.model.Product; | ||
|
||
import java.util.List; | ||
|
||
public interface PantryService { | ||
|
||
PantryItem savePantryItem(PantryItemDTO productItemDTO); | ||
List<PantryItem> getPantryItemsByHousehold(); | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/samcodesthings/shelfliferestapi/service/ProductService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.samcodesthings.shelfliferestapi.service; | ||
|
||
import com.samcodesthings.shelfliferestapi.dto.OpenFoodFactsItemDTO; | ||
import com.samcodesthings.shelfliferestapi.dto.ProductDTO; | ||
import com.samcodesthings.shelfliferestapi.exception.NotFoundException; | ||
import com.samcodesthings.shelfliferestapi.model.Product; | ||
|
||
import java.util.Optional; | ||
|
||
public interface ProductService { | ||
Optional<Product> findProductByCode(String code); | ||
Product saveFoodItem(OpenFoodFactsItemDTO foodItem); | ||
ProductDTO scanProduct(String code) throws Exception; | ||
ProductDTO requestProductToBeAdded(ProductDTO requestedProductDTO) throws Exception; | ||
} |
3 changes: 0 additions & 3 deletions
3
src/main/java/com/samcodesthings/shelfliferestapi/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.