diff --git a/.gitignore b/.gitignore index 5eac309..549e00a 100644 --- a/.gitignore +++ b/.gitignore @@ -30,4 +30,4 @@ build/ !**/src/test/**/build/ ### VS Code ### -.vscode/ \ No newline at end of file +.vscode/ diff --git a/pom.xml b/pom.xml index 3ef40e0..09db2e5 100644 --- a/pom.xml +++ b/pom.xml @@ -91,6 +91,25 @@ spring-boot-starter-mail + + org.springframework.boot + spring-boot-starter-validation + 2.5.1 + + + + javax.mail + mail + 1.4.7 + test + + + + javax.validation + validation-api + 2.0.1.Final + + @@ -99,6 +118,7 @@ org.springframework.boot spring-boot-maven-plugin + maven-surefire-plugin 2.22.0 diff --git a/src/main/java/com/unsri/ecommerce/domain/models/Inventory.java b/src/main/java/com/unsri/ecommerce/domain/models/Inventory.java index f6295f6..76bf375 100644 --- a/src/main/java/com/unsri/ecommerce/domain/models/Inventory.java +++ b/src/main/java/com/unsri/ecommerce/domain/models/Inventory.java @@ -1,5 +1,8 @@ package com.unsri.ecommerce.domain.models; +import com.sun.istack.NotNull; +import org.springframework.data.annotation.QueryAnnotation; + import javax.persistence.*; import java.util.List; @@ -21,7 +24,7 @@ public class Inventory { @Column(name = "fk_seller_id") private Integer fkSellerId; - @OneToMany(targetEntity = PhotoInventory.class) + @OneToMany(targetEntity = PhotoInventory.class, cascade = CascadeType.ALL) @JoinColumn(name = "fk_inventory_id", referencedColumnName = "id") private List photoInventories; diff --git a/src/main/java/com/unsri/ecommerce/domain/models/PhotoInventory.java b/src/main/java/com/unsri/ecommerce/domain/models/PhotoInventory.java index 2c4b0e3..1140dc1 100644 --- a/src/main/java/com/unsri/ecommerce/domain/models/PhotoInventory.java +++ b/src/main/java/com/unsri/ecommerce/domain/models/PhotoInventory.java @@ -17,17 +17,15 @@ public class PhotoInventory { @Column private String name; - public PhotoInventory() { } + @Column(name= "fk_inventory_id") + private Integer fkInventoryId; - public PhotoInventory(String path, String name) { - this.path = path; - this.name = name; - } + public PhotoInventory() { } - public PhotoInventory(Integer id, String path, String name) { - this.id = id; + public PhotoInventory(String path, String name, Integer fkInventoryId) { this.path = path; this.name = name; + this.fkInventoryId = fkInventoryId; } public String getPath() { @@ -49,4 +47,12 @@ public void setName(String name) { public Integer getId() { return id; } + + public Integer getFkInventoryId() { + return fkInventoryId; + } + + public void setFkInventoryId(Integer fkSellerId) { + this.fkInventoryId = fkSellerId; + } } diff --git a/src/main/java/com/unsri/ecommerce/presentation/controllers/InventoryController.java b/src/main/java/com/unsri/ecommerce/presentation/controllers/InventoryController.java index 25dea08..55be407 100644 --- a/src/main/java/com/unsri/ecommerce/presentation/controllers/InventoryController.java +++ b/src/main/java/com/unsri/ecommerce/presentation/controllers/InventoryController.java @@ -12,12 +12,16 @@ import com.unsri.ecommerce.domain.models.InventoryResponse; import com.unsri.ecommerce.infrastructure.repository.InventoryRepository; +import com.unsri.ecommerce.infrastructure.webconfig.jwt.JwtUtils; +import com.unsri.ecommerce.presentation.payload.request.UploadInventoryRequest; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; +import javax.validation.Valid; import java.util.Optional; @RestController @@ -25,6 +29,9 @@ public class InventoryController extends BaseController { private InventoryRepository inventoryRepository; + @Autowired + JwtUtils jwtUtils; + public InventoryController(InventoryRepository inventoryRepository) { this.inventoryRepository = inventoryRepository; } @@ -82,9 +89,17 @@ public BaseResponse> getInventoriesPaginatedBySellerId(H } @PostMapping(value = "/api/v1/storefront/products") - public Inventory addInventory(@RequestBody Inventory item) { + public BaseResponse addInventory(HttpServletRequest request, @Valid @RequestBody UploadInventoryRequest item) { + int sellerId = getAuthorizedUser(request.getUserPrincipal()); + BaseResponse baseResponse = new BaseResponse<>(); + Inventory inventory = new Inventory(item.getProductName(), item.getPrice(), sellerId, item.getPhotos()); CreateInventory command = new CreateInventory(inventoryRepository); - return command.execute(Optional.ofNullable(item)); + command.execute(Optional.of(inventory)); + + baseResponse.setMessage("Create Inventory Successfully"); + baseResponse.setStatusCode(HttpStatus.CREATED.toString()); + + return baseResponse; } @PutMapping("/api/v1/storefront/products/{id}") diff --git a/src/main/java/com/unsri/ecommerce/presentation/payload/request/UploadInventoryRequest.java b/src/main/java/com/unsri/ecommerce/presentation/payload/request/UploadInventoryRequest.java new file mode 100644 index 0000000..56b6459 --- /dev/null +++ b/src/main/java/com/unsri/ecommerce/presentation/payload/request/UploadInventoryRequest.java @@ -0,0 +1,50 @@ +package com.unsri.ecommerce.presentation.payload.request; + +import com.unsri.ecommerce.domain.models.PhotoInventory; + +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.Positive; +import java.util.List; + +public class UploadInventoryRequest { + @NotEmpty + private String productName; + + @Positive + private Double price; + + private List photos; + + public UploadInventoryRequest() { + } + + public UploadInventoryRequest(String productName, Double price, List photos) { + this.productName = productName; + this.price = price; + this.photos = photos; + } + + public String getProductName() { + return productName; + } + + public void setProductName(String productName) { + this.productName = productName; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public List getPhotos() { + return photos; + } + + public void setPhotos(List photos) { + this.photos = photos; + } +} diff --git a/src/test/java/com/unsri/ecommerce/application/behaviours/inventory/commands/CreateInventoryTests.java b/src/test/java/com/unsri/ecommerce/application/behaviours/inventory/commands/CreateInventoryTests.java index 5b52cf1..dbf8d20 100644 --- a/src/test/java/com/unsri/ecommerce/application/behaviours/inventory/commands/CreateInventoryTests.java +++ b/src/test/java/com/unsri/ecommerce/application/behaviours/inventory/commands/CreateInventoryTests.java @@ -5,13 +5,18 @@ import static org.mockito.Mockito.when; import com.unsri.ecommerce.domain.models.Inventory; +import com.unsri.ecommerce.domain.models.PhotoInventory; import com.unsri.ecommerce.infrastructure.repository.InventoryRepository; +import com.unsri.ecommerce.presentation.payload.request.UploadInventoryRequest; import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.util.Assert; +import java.util.ArrayList; +import java.util.List; + public class CreateInventoryTests { @Mock @@ -26,11 +31,23 @@ public CreateInventoryTests() { } @Test - public void CreateInventoryTests_ReturnSuccess() { + public void CreateInventoryWithSellerIdTests_ReturnSuccess() { + + String itemName = "Apple"; + Double price = 1000.0; + PhotoInventory photo = new PhotoInventory("https://photolemur.com/uploads/blog/unnamed.jpg","unnamed.jpg", 1); + List photos = new ArrayList<>(); + photos.add(photo); + // Arrange + UploadInventoryRequest inventoryRequest = new UploadInventoryRequest(); + inventoryRequest.setProductName(itemName); + inventoryRequest.setPrice(price); + inventoryRequest.setPhotos(photos); Inventory inventory = new Inventory(); - inventory.setItemName("Apple"); - inventory.setPrice(1000.0); + inventory.setItemName(inventoryRequest.getProductName()); + inventory.setPrice(inventoryRequest.getPrice()); + inventory.setPhotos(inventoryRequest.getPhotos()); when(inventoryRepository.save(inventory)).thenReturn(inventory); @@ -39,8 +56,9 @@ public void CreateInventoryTests_ReturnSuccess() { // Assert Assert.isTrue(expectedResult != null, "should not null"); - Assert.isTrue(expectedResult.getItemName() == inventory.getItemName(), "item name should be same"); - Assert.isTrue(expectedResult.getPrice() == inventory.getPrice(), "price should be same"); + Assert.isTrue(expectedResult.getItemName().equals(inventory.getItemName()) , "item name should be same"); + Assert.isTrue(expectedResult.getPrice().equals(inventory.getPrice()), "price should be same"); + Assert.isTrue(expectedResult.getPhotos().get(0) == inventory.getPhotos().get(0), "photos should be same"); // Verify verify(inventoryRepository, times(1)).save(inventory);