-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add product service, controller, dto, update progress
- Loading branch information
Showing
5 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/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,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); | ||
} | ||
|
||
} |
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
82 changes: 82 additions & 0 deletions
82
ShoppingCart/Backend/src/main/java/com/yen/ShoppingCart/model/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,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; | ||
} | ||
|
||
} |
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
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 |