Skip to content

Commit

Permalink
add product service, controller, dto, update progress
Browse files Browse the repository at this point in the history
  • Loading branch information
yennanliu committed Nov 27, 2023
1 parent 2e527c9 commit 04145c1
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.yen.ShoppingCart.controller;

import com.yen.ShoppingCart.common.ApiResponse;
import com.yen.ShoppingCart.model.Category;
import com.yen.ShoppingCart.model.dto.ProductDto;
import com.yen.ShoppingCart.service.CategoryService;
import com.yen.ShoppingCart.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
@RequestMapping("/product")
public class ProductController {

@Autowired
ProductService productService;

@Autowired
CategoryService categoryService;

@PostMapping("/add")
public ResponseEntity<ApiResponse> addProduct(@RequestBody ProductDto productDto) {

// TODO : optimize below
Optional<Category> optionalCategory = categoryService.readCategory(productDto.getCategoryId());
if (!optionalCategory.isPresent()) {
return new ResponseEntity<ApiResponse>(new ApiResponse(false, "category is invalid"), HttpStatus.CONFLICT);
}

Category category = optionalCategory.get();
productService.addProduct(productDto, category);
return new ResponseEntity<>(new ApiResponse(true, "Product has been added"), HttpStatus.CREATED);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ public Product(String name, String imageURL, double price, String description, C
this.category = category;
}

public Product() {

}

public Integer getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.yen.ShoppingCart.model.dto;

import com.sun.istack.NotNull;
import com.yen.ShoppingCart.model.Product;

public class ProductDto {
private Integer id;
private @NotNull String name;
private @NotNull String imageURL;
private @NotNull double price;
private @NotNull String description;
private @NotNull Integer categoryId;

public ProductDto(Product product) {
this.setId(product.getId());
this.setName(product.getName());
this.setImageURL(product.getImageURL());
this.setDescription(product.getDescription());
this.setPrice(product.getPrice());
this.setCategoryId(product.getCategory().getId());
}

public ProductDto(@NotNull String name, @NotNull String imageURL, @NotNull double price, @NotNull String description, @NotNull Integer categoryId) {
this.name = name;
this.imageURL = imageURL;
this.price = price;
this.description = description;
this.categoryId = categoryId;
}

public ProductDto() {
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getImageURL() {
return imageURL;
}

public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public Integer getCategoryId() {
return categoryId;
}

public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.yen.ShoppingCart.service;

import com.yen.ShoppingCart.model.Category;
import com.yen.ShoppingCart.model.Product;
import com.yen.ShoppingCart.model.dto.ProductDto;
import com.yen.ShoppingCart.repository.ProductRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
Expand All @@ -9,4 +12,23 @@ public class ProductService {

@Autowired
private ProductRepository productRepository;

public void addProduct(ProductDto productDto, Category category) {
Product product = getProductFromDto(productDto, category);
productRepository.save(product);

}

// local method
public static Product getProductFromDto(ProductDto productDto, Category category) {
Product product = new Product();
product.setCategory(category);
product.setDescription(productDto.getDescription());
product.setImageURL(productDto.getImageURL());
product.setPrice(productDto.getPrice());
product.setName(productDto.getName());
return product;
}

}

5 changes: 4 additions & 1 deletion ShoppingCart/doc/progress.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Progress

# 20231126
- https://dev.to/webtutsplus/let-s-develop-an-e-commerce-application-from-scratch-using-java-1gap
- https://dev.to/webtutsplus/let-s-develop-an-e-commerce-application-from-scratch-using-java-1gap

# 20231127
- https://dev.to/nilmadhabmondal/creating-an-ecommerce-frontend-with-vue-js-59o

0 comments on commit 04145c1

Please sign in to comment.