Skip to content

Commit

Permalink
Added productDto
Browse files Browse the repository at this point in the history
  • Loading branch information
tuannm151 committed Nov 30, 2021
1 parent 099067d commit a3912ab
Show file tree
Hide file tree
Showing 24 changed files with 370 additions and 109 deletions.
14 changes: 7 additions & 7 deletions src/main/java/com/example/DOTSAPI/DotsApiApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public Argon2PasswordEncoder passwordEncoder() {
@Bean
CommandLineRunner run(AppUserServices appUserServices, ProductServices productServices) {
return args -> {
appUserServices.saveRole(new Role( "ROLE_USER"));
appUserServices.saveRole(new Role( "ROLE_ADMIN"));

appUserServices.saveAppUser(new User( "Tuan", "Nguyen", "tuanxsokoh", "[email protected]",
"123456", new HashSet<>()));

appUserServices.addRoleToAppUser("tuanxsokoh", "ROLE_ADMIN");
// appUserServices.saveRole(new Role( "ROLE_USER"));
// appUserServices.saveRole(new Role( "ROLE_ADMIN"));
//
// appUserServices.saveAppUser(new User( "Tuan", "Nguyen", "tuanxsokoh", "[email protected]",
// "123456", new HashSet<>()));
//
// appUserServices.addRoleToAppUser("tuanxsokoh", "ROLE_ADMIN");
};
}

Expand Down
40 changes: 30 additions & 10 deletions src/main/java/com/example/DOTSAPI/controller/ProductController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.example.DOTSAPI.controller;

import com.example.DOTSAPI.dto.product.ProductDto;
import com.example.DOTSAPI.dto.product.ResponseProductDto;
import com.example.DOTSAPI.dto.product.RateProductDto;
import com.example.DOTSAPI.model.Brand;
import com.example.DOTSAPI.model.Category;
import com.example.DOTSAPI.model.Product;
import com.example.DOTSAPI.services.product.ProductServices;
Expand All @@ -18,43 +22,59 @@ public class ProductController {
private final ProductServices productServices;

@GetMapping(value = "", params = "id")
public ResponseEntity<Product> getProductById(@RequestParam Long id) {
public ResponseEntity<ResponseProductDto> getProductById(@RequestParam Long id) {
return ResponseEntity.ok().body(productServices.getProductById(id));
}
@GetMapping("/category")
public ResponseEntity<List<Category>> getAllCategories() {
public ResponseEntity<List<Category>> getAllCategoriesName() {
return ResponseEntity.ok().body(productServices.getAllCategories());
}

@GetMapping("/brand")
public ResponseEntity<List<Brand>> getAllBrandsName() {
return ResponseEntity.ok().body(productServices.getAllBrands());
}

@GetMapping(value = "/list", params="category")
public ResponseEntity<List<Product>> findAllByCategoryName(@RequestParam String category) {
public ResponseEntity<List<ResponseProductDto>> findAllByCategoryName(@RequestParam String category) {
return ResponseEntity.ok().body(productServices.findAllByCategoryName(category));
}

@GetMapping(value = "/list/search", params="keyword")
public ResponseEntity<List<Product>> findAllByName(@RequestParam String keyword) {
public ResponseEntity<List<ResponseProductDto>> findAllByName(@RequestParam String keyword) {
if(keyword.length() > 40) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
return ResponseEntity.ok().body(productServices.searchByKeyword(keyword));
}

@GetMapping("/admin/list")
public ResponseEntity<List<Product>> getAllProducts() {
@GetMapping("/list")
public ResponseEntity<List<ResponseProductDto>> getAllProducts() {
return ResponseEntity.ok().body(productServices.getAllProducts());
}
@PostMapping("/admin/category/save")
public ResponseEntity<Category> saveCategory(@RequestBody @Valid Category category) {
return ResponseEntity.status(HttpStatus.CREATED).body(productServices.saveCategory(category));
}
@PostMapping("/admin/brand/save")
public ResponseEntity<Brand> saveBrand(@RequestBody @Valid Brand brand) {
return ResponseEntity.status(HttpStatus.CREATED).body(productServices.saveBrand(brand));
}
@PostMapping("/admin/save")
public ResponseEntity<Product> saveProduct(@RequestBody @Valid Product product) {
return ResponseEntity.status(HttpStatus.CREATED).body(productServices.saveProduct(product));
public ResponseEntity<ResponseProductDto> saveProduct(@RequestBody @Valid ProductDto productDto) {
productServices.saveProduct(productDto);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@PostMapping("/admin/rating")
public ResponseEntity<?> setRatingProduct(@RequestBody @Valid RateProductDto rateProductDto) {
productServices.rateProduct(rateProductDto);
return ResponseEntity.ok().build();
}

@PutMapping("/admin/update/{id}")
public ResponseEntity<Product> updateProduct(@PathVariable Long id, @RequestBody @Valid Product product) {
return ResponseEntity.ok().body(productServices.updateProductById(id, product));
public ResponseEntity<ResponseProductDto> updateProduct(@PathVariable Long id, @RequestBody @Valid ProductDto productDto) {
return ResponseEntity.ok().body(productServices.updateProductById(id, productDto));
}

@DeleteMapping( "/admin/delete/{id}")
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/com/example/DOTSAPI/dto/cart/CartItemDto.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.example.DOTSAPI.dto.cart;

import com.example.DOTSAPI.dto.product.ResponseProductDto;
import com.example.DOTSAPI.model.CartItem;
import com.example.DOTSAPI.model.Product;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.Setter;

Expand All @@ -11,25 +13,19 @@
@Getter
@Setter
public class CartItemDto {
@NotNull
private Long cartItemId;

@NotNull
private Long quantity;

@NotNull
private Integer size;

@NotBlank
private String color;
private ResponseProductDto responseProductDto;

@NotNull
@JsonIgnore
private Product product;

public CartItemDto(CartItem cartItem) {
this.cartItemId = cartItem.getId();
this.size = cartItem.getSize();
this.product = cartItem.getProduct();
this.responseProductDto = new ResponseProductDto(cartItem.getProduct());
this.color = cartItem.getColor();
this.quantity = cartItem.getQuantity();
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/com/example/DOTSAPI/dto/order/OrderItemDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@
@NoArgsConstructor

public class OrderItemDto {
private Long id;
private Long productId;
private String category;
private String imageUrl;
private String productName;
private Long quantity;
private Integer size;
private double unitPrice;
private String color;
private String brand;

public OrderItemDto(Product product) {
this.imageUrl = product.getImageUrl();
this.productName = product.getName();
this.id = product.getId();
this.productId = product.getId();
this.category = product.getCategory().getName();
this.brand = product.getBrand().getName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import javax.validation.constraints.NotNull;

@Getter
@Setter
@NoArgsConstructor
public class RequestOrderDto {
@NotNull
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/example/DOTSAPI/dto/product/ProductDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.example.DOTSAPI.dto.product;

import lombok.Getter;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.Set;

@Getter
public class ProductDto {
@NotBlank(message = "NAME_NOT_VALID")
private String productName;

@NotBlank(message = "DESCRIPTION_NOT_VALID")
private String description;

@NotBlank(message = "CATEGORY_NOT_VALID")
private String category;

@NotBlank(message = "IMAGEURL_NOT_VALID")
private String imageUrl;

@NotNull(message = "PRICE_MISSING")
@Min(value = 0, message = "PRICE_NOT_VALID")
private double unitPrice;

@NotNull(message = "STOCK_MISSING")
@Min(value = 0,message = "STOCK_NOT_VALID")
private Long stock;

@NotBlank(message = "BRAND_NOT_VALID")
private String brand;

@NotNull(message = "OVERALL_RATING_NOT_VALID")
private float overallRating;

@NotNull(message = "TOTAL_RATING_NOT_VALID")
@Min(value = 0, message = "TOTAL_RATING_NOT_VALID")
private Integer totalRating;

@NotEmpty(message = "SIZE_NOT_VALID")
private Set<Integer> size;
@NotEmpty(message = "COLOR_NOT_VALID")
private Set<String> color;
}
19 changes: 19 additions & 0 deletions src/main/java/com/example/DOTSAPI/dto/product/RateProductDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.example.DOTSAPI.dto.product;

import lombok.Getter;
import lombok.Setter;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

@Getter
public class RateProductDto {
@NotNull(message = "PRODUCT_ID_MISSING")
private Long productId;

@NotNull(message = "RATING_NOT_VALID")
@Min(value = 0, message = "RATING_NOT_VALID")
@Max(value = 5, message = "RATING_NOT_VALID")
private Integer rating;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.example.DOTSAPI.dto.product;

import com.example.DOTSAPI.model.Product;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;
import java.util.List;
import java.util.Set;


@Getter
@Setter
public class ResponseProductDto {
private Long productId;
private String productName;
private String category;
private String imageUrl;
private double unitPrice;
private Long stock;
private String brand;
private float overallRating;
private Integer totalRating;
private Set<Integer> size;
private Set<String> color;

@Temporal(TemporalType.DATE)
private Date createdAt;

@Temporal(TemporalType.DATE)
private Date modifiedAt;

public ResponseProductDto(Product product) {
this.productId = product.getId();
this.productName = product.getName();
this.category = product.getCategory().getName();
this.imageUrl = product.getImageUrl();
this.stock = product.getStock();
this.size = product.getSize();
this.unitPrice = product.getPrice();
this.color = product.getColor();
this.brand = product.getBrand().getName();
this.overallRating = product.getOverallRating();
this.totalRating = product.getTotalRating();
this.setCreatedAt(product.getCreatedAt());
this.setModifiedAt(product.getModifiedAt());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ public final ResponseEntity<Object> handleAlreadyExistsException(AlreadyExistsEx

@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleAllExceptions(Exception ex, HttpServletRequest request) {
// ex.getMessage() for debug purpose, change it to "UNEXPECTED_ERROR" for production
return ExceptionUtils.buildResponseEntity(
"An unexpected error occurred",
ex.getMessage(),
null,
HttpStatus.BAD_REQUEST,
request);
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/com/example/DOTSAPI/model/Brand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.example.DOTSAPI.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.util.HashSet;
import java.util.Set;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class Brand {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotBlank(message = "Brand name is missing")
@Size(max = 15, message="Brand must be smaller than 15 characters long")
@Column(unique = true, length = 60)
private String name;

public Brand(String name) {
this.name = name;
}

@JsonIgnore
@OneToMany(mappedBy = "brand", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private Set<Product> products = new HashSet<>();
}
1 change: 1 addition & 0 deletions src/main/java/com/example/DOTSAPI/model/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@Setter
@NoArgsConstructor
public class Category {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down
Loading

0 comments on commit a3912ab

Please sign in to comment.