Skip to content
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

Upload Inventory By Vendor #48

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@
@Repository
public interface SellerRepository extends JpaRepository<Seller, Integer> {
Optional<Seller> findByEmail(String email);
Optional<Seller> findById(Integer id);
aditiyars marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import com.unsri.ecommerce.domain.models.InventoryResponse;
import com.unsri.ecommerce.infrastructure.repository.InventoryRepository;

import com.unsri.ecommerce.infrastructure.repository.SellerRepository;
import com.unsri.ecommerce.infrastructure.security.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;
Expand All @@ -26,12 +28,14 @@
public class InventoryController extends BaseController {

private InventoryRepository inventoryRepository;
private SellerRepository sellerRepository;
aditiyars marked this conversation as resolved.
Show resolved Hide resolved

@Autowired
JwtUtils jwtUtils;

public InventoryController(InventoryRepository inventoryRepository) {
public InventoryController(InventoryRepository inventoryRepository, SellerRepository sellerRepository) {
this.inventoryRepository = inventoryRepository;
this.sellerRepository = sellerRepository;
}

@GetMapping("api/v1/storefront/products")
Expand Down Expand Up @@ -87,9 +91,17 @@ public BaseResponse<List<InventoryResponse>> getInventoriesPaginatedBySellerType
}

@PostMapping(value = "/api/v1/storefront/products")
public Inventory addInventory(@RequestBody Inventory item) {
public BaseResponse addInventory(HttpServletRequest request, @RequestBody UploadInventoryRequest item) {
int sellerId = getAuthorizedUser(request.getUserPrincipal());
Inventory inventory = new Inventory(item.getProductName(), item.getPrice(),sellerId, item.getPhoto());

CreateInventory command = new CreateInventory(inventoryRepository);
return command.execute(Optional.ofNullable(item));
command.execute(Optional.of(inventory));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buat pengecekan jika proses sudah berhasil atau tidak

BaseResponse baseResponse= new BaseResponse<>();

baseResponse.setMessage("Create Inventory Successfully");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pesan sukses dibuat berdasarkan pengecekan jika proses sudah berhasil

baseResponse.setStatusCode(HttpStatus.CREATED.toString());
return baseResponse;
}

@PutMapping("/api/v1/storefront/products/{id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.unsri.ecommerce.presentation.payload.request;

import com.unsri.ecommerce.domain.models.PhotoInventory;

import java.util.List;

public class UploadInventoryRequest {
private String productName;
private Double price;
private List<PhotoInventory> photo;

public UploadInventoryRequest() {
}

public UploadInventoryRequest(String productName, Double price, List<PhotoInventory> photo) {
this.productName = productName;
this.price = price;
this.photo = photo;
}

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<PhotoInventory> getPhoto() {
return photo;
}

public void setPhoto(List<PhotoInventory> photo) {
this.photo = photo;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.unsri.ecommerce.domain.models.Inventory;
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;
Expand All @@ -26,11 +27,22 @@ public CreateInventoryTests() {
}

@Test
public void CreateInventoryTests_ReturnSuccess() {
public void CreateInventoryWithSellerIdTests_ReturnSuccess() {

String itemName = "Apple";
Double price = 1000.0;
int sellerId = 1;

// Arrange
UploadInventoryRequest inventoryRequest = new UploadInventoryRequest();
inventoryRequest.setProductName(itemName);
inventoryRequest.setPrice(price);

Inventory inventory = new Inventory();
inventory.setItemName("Apple");
inventory.setPrice(1000.0);
inventory.setItemName(inventoryRequest.getProductName());
inventory.setPrice(inventoryRequest.getPrice());
inventory.setPhotos(inventoryRequest.getPhoto());
aditiyars marked this conversation as resolved.
Show resolved Hide resolved
inventory.setFkSellerId(sellerId);

when(inventoryRepository.save(inventory)).thenReturn(inventory);

Expand All @@ -41,6 +53,7 @@ public void CreateInventoryTests_ReturnSuccess() {
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.getFkSellerId() == sellerId, "seller id should be same");

// Verify
verify(inventoryRepository, times(1)).save(inventory);
Expand Down