Skip to content

Commit

Permalink
feat: add cart table, adjust create new category
Browse files Browse the repository at this point in the history
  • Loading branch information
lcaohoanq committed Dec 8, 2024
1 parent e7416ec commit 48c6624
Show file tree
Hide file tree
Showing 18 changed files with 202 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public ResponseEntity<ApiResponse<CategoryResponse>> getById(@PathVariable int i

@PostMapping("")
@PreAuthorize("hasRole('ROLE_MANAGER')")
public ResponseEntity<ApiResponse<CategoryResponse>> create(
public ResponseEntity<ApiResponse<List<CategoryResponse>>> create(
@Valid @RequestBody CategoryDTO categoryDTO,
BindingResult result
) {
Expand All @@ -72,7 +72,7 @@ public ResponseEntity<ApiResponse<CategoryResponse>> create(
throw new MethodArgumentNotValidException(result);
}
return ResponseEntity.status(HttpStatus.CREATED).body(
ApiResponse.<CategoryResponse>builder()
ApiResponse.<List<CategoryResponse>>builder()
.message("Create category successfully")
.statusCode(HttpStatus.CREATED.value())
.isSuccess(true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package com.lcaohoanq.shoppe.controllers;

import com.github.javafaker.Faker;
import com.lcaohoanq.shoppe.components.LocalizationUtils;
import com.lcaohoanq.shoppe.dtos.request.ProductDTO;
import com.lcaohoanq.shoppe.dtos.request.ProductImageDTO;
import com.lcaohoanq.shoppe.dtos.responses.ProductResponse;
import com.lcaohoanq.shoppe.dtos.responses.base.ApiResponse;
import com.lcaohoanq.shoppe.dtos.responses.base.PageResponse;
import com.lcaohoanq.shoppe.enums.ProductStatus;
import com.lcaohoanq.shoppe.exceptions.MethodArgumentNotValidException;
import com.lcaohoanq.shoppe.metadata.MediaMeta;
import com.lcaohoanq.shoppe.models.Category;
import com.lcaohoanq.shoppe.models.ProductImage;
import com.lcaohoanq.shoppe.repositories.CategoryRepository;
import com.lcaohoanq.shoppe.services.category.CategoryService;
Expand All @@ -16,6 +19,7 @@
import jakarta.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.PageRequest;
Expand Down Expand Up @@ -155,41 +159,43 @@ public ResponseEntity<ApiResponse<ProductResponse>> deleteProduct(
);
}

// @PostMapping("/generateFakeProducts")
// @PreAuthorize("hasAnyRole('ROLE_MANAGER')")
// public ResponseEntity<String> generateFakeProducts() {
// Faker faker = new Faker();
// Random random = new Random();
//
// List<Category> categoryList = categoryRepository.findAll();
//
// for (int i = 10_001; i <= 1_000_000; i++) {
//// String productName = faker.commerce().productName();
//// if(productService.existsByName(productName)) {
//// continue;
//// }
//
// String randomCategory = categoryList.get(random.nextInt(categoryList.size())).getName();
//
// ProductDTO productDTO = new ProductDTO(
// faker.commerce().productName(),
// faker.lorem().sentence(), //description
// faker.internet().image(), //thumbnail
// randomCategory, //category
// (double)faker.number().numberBetween(10, 90_000_000), //price
// (double)faker.number().numberBetween(10, 90_000_000), //priceBeforeDiscount
// faker.number().numberBetween(1, 10000), //quantity
// faker.number().numberBetween(0, 10000), //sold
// faker.number().numberBetween(0, 10000), //view
// faker.number().randomDouble(1, 0, 5) //rating
// );
// try{
// productService.createProduct(productDTO);
// } catch (Exception e) {
// return ResponseEntity.badRequest().body(e.getMessage());
@PostMapping("/generateFakeProducts")
@PreAuthorize("hasAnyRole('ROLE_MANAGER')")
public ResponseEntity<String> generateFakeProducts() {
Faker faker = new Faker();
Random random = new Random();

List<Category> categoryList = categoryRepository.findAll();

for (int i = 1; i <= 500; i++) {
// String productName = faker.commerce().productName();
// if(productService.existsByName(productName)) {
// continue;
// }
// }
// return ResponseEntity.ok("Fake products generated");
// }

String randomCategory = categoryList.get(random.nextInt(categoryList.size())).getName();

ProductDTO productDTO = new ProductDTO(
faker.commerce().productName(),
faker.lorem().sentence(), //description
randomCategory, //category
3L, //shopOwnerId
(double)faker.number().numberBetween(10, 90_000_000), //price
(double)faker.number().numberBetween(10, 90_000_000), //priceBeforeDiscount
faker.number().numberBetween(1, 10000), //quantity
faker.number().numberBetween(0, 10000), //sold
faker.number().numberBetween(0, 10000), //view
faker.number().randomDouble(1, 0, 5), //rating,
ProductStatus.VERIFIED, //status
true //isActive
);
try{
productService.create(productDTO);
} catch (Exception e) {
return ResponseEntity.badRequest().body(e.getMessage());
}
}
return ResponseEntity.ok("Fake products generated");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public ResponseEntity<ApiResponse<RoleResponse>> getById(@PathVariable Long id)
}

@PostMapping("")
@PreAuthorize("hasRole('ROLE_MANAGER')")
// @PreAuthorize("hasRole('ROLE_MANAGER')")
public ResponseEntity<ApiResponse<RoleResponse>> create(
@Valid @RequestBody RoleDTO roleDTO,
BindingResult bindingResult) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.lcaohoanq.shoppe.dtos.request;

import jakarta.validation.constraints.NotEmpty;
import java.util.List;
import lombok.Builder;

@Builder
public record CategoryDTO(
@NotEmpty(message = "Category name is required") String name
@NotEmpty(message = "Category name is required") List<String> name
) {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ public record ProductDTO(
@NotNull(message = "Product's description must not be null")
String description,

@NotNull(message = "Product's thumbnail must not be null")
String thumbnail,

@NotNull(message = "Product's category must not be null")
String category,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private boolean isPublicEndpoint(String path, HttpServletRequest request) {

// Check roles endpoint
if (path.startsWith(apiPrefix + "/roles")) {
boolean isGet = request.getMethod().equals("GET");
boolean isGet = request.getMethod().equals("GET") || request.getMethod().equals("POST");
log.info("Roles endpoint - Is GET? {}", isGet);
return isGet;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.lcaohoanq.shoppe.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.lcaohoanq.shoppe.models.base.BaseEntity;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "carts")
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
public class Cart extends BaseEntity {

@Id
@SequenceGenerator(name = "carts_seq", sequenceName = "carts_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "carts_seq")
@Column(name="id", unique=true, nullable=false)
@JsonProperty("id")
private Long id;

@Column(name = "total_quantity")
private int totalQuantity;

@Column(name = "total_price")
private double totalPrice;

@OneToOne
@JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false)
private User user;

@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL, orphanRemoval = true)
private List<CartProduct> cartProducts = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.lcaohoanq.shoppe.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.lcaohoanq.shoppe.models.base.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Table(name = "cart_products")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class CartProduct extends BaseEntity {

@Id
@SequenceGenerator(name = "cartproducts_seq", sequenceName = "cartproducts_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "cartproducts_seq")
@Column(name="id", unique=true, nullable=false)
@JsonProperty("id")
private Long id;

@ManyToOne
@JoinColumn(name = "cart_id")
private Cart cart;

@ManyToOne
@JoinColumn(name = "product_id")
private Product product;

@Column(name = "quantity")
private int quantity;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lcaohoanq.shoppe.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.lcaohoanq.shoppe.models.base.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
Expand All @@ -21,7 +22,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Coupon {
public class Coupon extends BaseEntity {

@Id
@SequenceGenerator(name = "coupons_seq", sequenceName = "coupons_id_seq", allocationSize = 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.lcaohoanq.shoppe.models.base.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
Expand All @@ -27,7 +28,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CouponCondition {
public class CouponCondition extends BaseEntity {

@Id
@SequenceGenerator(name = "coupon_conditions_seq", sequenceName = "coupon_conditions_id_seq", allocationSize = 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.lcaohoanq.shoppe.models;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.lcaohoanq.shoppe.enums.ProductStatus;
import com.lcaohoanq.shoppe.models.base.BaseEntity;
Expand All @@ -15,6 +16,7 @@
import jakarta.persistence.OneToMany;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -66,6 +68,9 @@ public class Product extends BaseEntity {
private User shopOwner;

@OneToMany(mappedBy = "product")
private List<ProductImage> images;
private List<ProductImage> images = new ArrayList<>();

@OneToMany(mappedBy = "product")
private List<CartProduct> cartProducts = new ArrayList<>();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.lcaohoanq.shoppe.enums.ShippingCarrierName;
import com.lcaohoanq.shoppe.models.base.BaseEntity;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
Expand All @@ -24,7 +25,7 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class ShippingCarrier {
public class ShippingCarrier extends BaseEntity {

@Id
@SequenceGenerator(name = "orders_seq", sequenceName = "orders_id_seq", allocationSize = 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lcaohoanq.shoppe.models;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.lcaohoanq.shoppe.constants.Regex;
import com.lcaohoanq.shoppe.enums.Country;
import com.lcaohoanq.shoppe.enums.Currency;
import com.lcaohoanq.shoppe.enums.Gender;
Expand All @@ -20,6 +21,7 @@
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -80,11 +82,16 @@ public class User extends BaseEntity implements UserDetails {

private String address;

@Unique
@Column(name="phone_number",nullable = false, length = 100)
@JsonProperty("phone_number")
private String phoneNumber;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name="wallet_id")
@JoinColumn(name = "cart_id")
private Cart cart;

@OneToOne(mappedBy = "user", cascade = CascadeType.ALL)
private Wallet wallet;

@ManyToOne
Expand Down
Loading

0 comments on commit 48c6624

Please sign in to comment.