From 3a39b02886da1dc7b39e75d8b134f3de42d3d620 Mon Sep 17 00:00:00 2001 From: tuanxsokoh Date: Wed, 1 Dec 2021 23:43:08 +0700 Subject: [PATCH] Modified hibernate query --- .../DOTSAPI/configuration/CorsConfigure.java | 22 +++++++++++++++ .../DOTSAPI/controller/OrderController.java | 6 +++++ .../DOTSAPI/dto/cart/UpdateCartItemDto.java | 12 ++------- .../example/DOTSAPI/dto/order/OrderDto.java | 4 +-- .../DOTSAPI/dto/product/ProductDto.java | 6 ++--- .../dto/product/ResponseProductDto.java | 3 +++ .../java/com/example/DOTSAPI/model/Brand.java | 1 - .../com/example/DOTSAPI/model/CartItem.java | 4 +-- .../example/DOTSAPI/model/CartSession.java | 5 ++-- .../com/example/DOTSAPI/model/Category.java | 8 ++++-- .../java/com/example/DOTSAPI/model/Order.java | 4 +-- .../com/example/DOTSAPI/model/Product.java | 6 ++--- .../example/DOTSAPI/repository/OrderRepo.java | 1 + .../cart/CartSessionServicesImpl.java | 8 ------ .../DOTSAPI/services/order/OrderServices.java | 2 ++ .../services/order/OrderServicesImpl.java | 14 ++++++++++ .../services/product/ProductServicesImpl.java | 12 ++++++--- src/main/resources/application.properties | 27 +++++++++---------- 18 files changed, 92 insertions(+), 53 deletions(-) create mode 100644 src/main/java/com/example/DOTSAPI/configuration/CorsConfigure.java diff --git a/src/main/java/com/example/DOTSAPI/configuration/CorsConfigure.java b/src/main/java/com/example/DOTSAPI/configuration/CorsConfigure.java new file mode 100644 index 0000000..725c136 --- /dev/null +++ b/src/main/java/com/example/DOTSAPI/configuration/CorsConfigure.java @@ -0,0 +1,22 @@ +package com.example.DOTSAPI.configuration; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.CorsRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class CorsConfigure { + @Bean + public WebMvcConfigurer corsConfigurer() { + return new WebMvcConfigurer() { + @Override + public void addCorsMappings(CorsRegistry registry) { + registry.addMapping("/**") + .allowedMethods("GET", "POST", "PUT", "DELETE") + .allowedHeaders("*") + .allowedOrigins("*"); + } + }; + } +} diff --git a/src/main/java/com/example/DOTSAPI/controller/OrderController.java b/src/main/java/com/example/DOTSAPI/controller/OrderController.java index f8d148f..b058eb0 100644 --- a/src/main/java/com/example/DOTSAPI/controller/OrderController.java +++ b/src/main/java/com/example/DOTSAPI/controller/OrderController.java @@ -43,6 +43,11 @@ public ResponseEntity> getUserOrders(Authentication authenticatio return ResponseEntity.ok().body(orderServices.getUserOrders(user)); } + @GetMapping("/admin/list") + public ResponseEntity> findAllOrders() { + return ResponseEntity.ok().body(orderServices.findAllOrders()); + } + @GetMapping("/admin/{id}") public ResponseEntity getOrder(@PathVariable Long id) { return ResponseEntity.ok().body(orderServices.getOrder(id)); @@ -58,4 +63,5 @@ public ResponseEntity changePaymentStatus(@RequestBody @Valid ChangePa orderServices.changePaymentStatus(changePaymentStatusDto.getOrderId(), changePaymentStatusDto.getStatus()); return ResponseEntity.ok().build(); } + } diff --git a/src/main/java/com/example/DOTSAPI/dto/cart/UpdateCartItemDto.java b/src/main/java/com/example/DOTSAPI/dto/cart/UpdateCartItemDto.java index b9b492f..fbbafdb 100644 --- a/src/main/java/com/example/DOTSAPI/dto/cart/UpdateCartItemDto.java +++ b/src/main/java/com/example/DOTSAPI/dto/cart/UpdateCartItemDto.java @@ -14,20 +14,12 @@ public class UpdateCartItemDto { private Long cartItemId; - @NotNull(message = "Quantity is missing") + @NotNull(message = "QUANTITY_MISSING") @Min(value = 1, message = "Quantity at least 1") private Long quantity; - @NotNull(message = "Size is missing") - private Integer size; - @NotBlank(message = "Color is missing") - private String color; - - - public UpdateCartItemDto(Long cartItemId, Long quantity, Integer size,String color) { + public UpdateCartItemDto(Long cartItemId, Long quantity) { this.cartItemId = cartItemId; - this.size = size; this.quantity = quantity; - this.color = color; } } diff --git a/src/main/java/com/example/DOTSAPI/dto/order/OrderDto.java b/src/main/java/com/example/DOTSAPI/dto/order/OrderDto.java index a592257..201b5f9 100644 --- a/src/main/java/com/example/DOTSAPI/dto/order/OrderDto.java +++ b/src/main/java/com/example/DOTSAPI/dto/order/OrderDto.java @@ -23,10 +23,10 @@ @NoArgsConstructor public class OrderDto { private Long orderId; - @Temporal(TemporalType.DATE) + @Temporal(TemporalType.TIMESTAMP) private Date createdAt; - @Temporal(TemporalType.DATE) + @Temporal(TemporalType.TIMESTAMP) private Date modifiedAt; private double totalPrice; diff --git a/src/main/java/com/example/DOTSAPI/dto/product/ProductDto.java b/src/main/java/com/example/DOTSAPI/dto/product/ProductDto.java index 1513bb6..cdafdc9 100644 --- a/src/main/java/com/example/DOTSAPI/dto/product/ProductDto.java +++ b/src/main/java/com/example/DOTSAPI/dto/product/ProductDto.java @@ -4,10 +4,7 @@ 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 javax.validation.constraints.*; import java.util.Date; import java.util.Set; @@ -23,6 +20,7 @@ public class ProductDto { private String category; @NotBlank(message = "IMAGEURL_NOT_VALID") + @Size(max = 700, message = "IMAGEURL_TOO_LONG") private String imageUrl; @NotNull(message = "PRICE_MISSING") diff --git a/src/main/java/com/example/DOTSAPI/dto/product/ResponseProductDto.java b/src/main/java/com/example/DOTSAPI/dto/product/ResponseProductDto.java index 2f64793..cb89f86 100644 --- a/src/main/java/com/example/DOTSAPI/dto/product/ResponseProductDto.java +++ b/src/main/java/com/example/DOTSAPI/dto/product/ResponseProductDto.java @@ -15,9 +15,11 @@ @Getter @Setter +@JsonInclude(JsonInclude.Include.NON_NULL) public class ResponseProductDto { private Long productId; private String productName; + private String description; private String category; private String imageUrl; private double unitPrice; @@ -46,6 +48,7 @@ public ResponseProductDto(Product product) { this.brand = product.getBrand().getName(); this.overallRating = product.getOverallRating(); this.totalRating = product.getTotalRating(); + this.description = product.getDescription(); this.setCreatedAt(product.getCreatedAt()); this.setModifiedAt(product.getModifiedAt()); } diff --git a/src/main/java/com/example/DOTSAPI/model/Brand.java b/src/main/java/com/example/DOTSAPI/model/Brand.java index c1c74d7..669bbf7 100644 --- a/src/main/java/com/example/DOTSAPI/model/Brand.java +++ b/src/main/java/com/example/DOTSAPI/model/Brand.java @@ -16,7 +16,6 @@ @Setter @NoArgsConstructor public class Brand { - @JsonIgnore @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; diff --git a/src/main/java/com/example/DOTSAPI/model/CartItem.java b/src/main/java/com/example/DOTSAPI/model/CartItem.java index b75804c..4c7048c 100644 --- a/src/main/java/com/example/DOTSAPI/model/CartItem.java +++ b/src/main/java/com/example/DOTSAPI/model/CartItem.java @@ -23,9 +23,9 @@ public class CartItem { @Column private Long id; - @Temporal(TemporalType.DATE) + @Temporal(TemporalType.TIMESTAMP) private Date createdAt; - @Temporal(TemporalType.DATE) + @Temporal(TemporalType.TIMESTAMP) private Date modifiedAt; @NotNull(message = "Quantity is missing") diff --git a/src/main/java/com/example/DOTSAPI/model/CartSession.java b/src/main/java/com/example/DOTSAPI/model/CartSession.java index cd07924..84d670f 100644 --- a/src/main/java/com/example/DOTSAPI/model/CartSession.java +++ b/src/main/java/com/example/DOTSAPI/model/CartSession.java @@ -21,9 +21,9 @@ public class CartSession { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @Temporal(TemporalType.DATE) + @Temporal(TemporalType.TIMESTAMP) private Date createdAt; - @Temporal(TemporalType.DATE) + @Temporal(TemporalType.TIMESTAMP) private Date modifiedAt; @Transient @@ -42,6 +42,7 @@ public class CartSession { private User user; public CartSession(User user) { + this.createdAt = new Date(); this.user = user; } } diff --git a/src/main/java/com/example/DOTSAPI/model/Category.java b/src/main/java/com/example/DOTSAPI/model/Category.java index 3fc5c2e..e559fdc 100644 --- a/src/main/java/com/example/DOTSAPI/model/Category.java +++ b/src/main/java/com/example/DOTSAPI/model/Category.java @@ -17,12 +17,16 @@ @Setter @NoArgsConstructor public class Category { - @JsonIgnore @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @NotBlank(message = "Category name is missing") + @Column(nullable = false, length = 700) + @Size(max = 700, message="Image url must be smaller than 60 characters long") + @NotBlank(message = "IMAGEURL_MISSING") + private String imageUrl; + + @NotBlank(message = "CATEGORY_MISSING") @Size(max = 60, message="Category must be smaller than 60 characters long") @Column(unique = true, length = 60) private String name; diff --git a/src/main/java/com/example/DOTSAPI/model/Order.java b/src/main/java/com/example/DOTSAPI/model/Order.java index 289c8b7..39fd411 100644 --- a/src/main/java/com/example/DOTSAPI/model/Order.java +++ b/src/main/java/com/example/DOTSAPI/model/Order.java @@ -22,10 +22,10 @@ public class Order { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - @Temporal(TemporalType.DATE) + @Temporal(TemporalType.TIMESTAMP) private Date createdAt; - @Temporal(TemporalType.DATE) + @Temporal(TemporalType.TIMESTAMP) private Date modifiedAt; @Min(value = 0, message = "Total can't be negative") diff --git a/src/main/java/com/example/DOTSAPI/model/Product.java b/src/main/java/com/example/DOTSAPI/model/Product.java index 59de0ce..7a253a6 100644 --- a/src/main/java/com/example/DOTSAPI/model/Product.java +++ b/src/main/java/com/example/DOTSAPI/model/Product.java @@ -20,10 +20,10 @@ public class Product { @Column private Long id; - @Temporal(TemporalType.DATE) + @Temporal(TemporalType.TIMESTAMP) private Date createdAt; - @Temporal(TemporalType.DATE) + @Temporal(TemporalType.TIMESTAMP) private Date modifiedAt; @Column(nullable = false) @@ -32,7 +32,7 @@ public class Product { @Column(nullable = false,length = 1000) private String description; - @Column(nullable = false) + @Column(nullable = false, length = 700) private String imageUrl; @Column(nullable = false) diff --git a/src/main/java/com/example/DOTSAPI/repository/OrderRepo.java b/src/main/java/com/example/DOTSAPI/repository/OrderRepo.java index a345574..d938cbd 100644 --- a/src/main/java/com/example/DOTSAPI/repository/OrderRepo.java +++ b/src/main/java/com/example/DOTSAPI/repository/OrderRepo.java @@ -10,4 +10,5 @@ public interface OrderRepo extends JpaRepository { List findAllByUserOrderByCreatedAtDesc(User user); Order findByIdAndUser(Long id, User user); + List findAllByOrderByCreatedAtDesc(); } diff --git a/src/main/java/com/example/DOTSAPI/services/cart/CartSessionServicesImpl.java b/src/main/java/com/example/DOTSAPI/services/cart/CartSessionServicesImpl.java index 43a07ef..e96db9b 100644 --- a/src/main/java/com/example/DOTSAPI/services/cart/CartSessionServicesImpl.java +++ b/src/main/java/com/example/DOTSAPI/services/cart/CartSessionServicesImpl.java @@ -93,15 +93,7 @@ public void updateCartItem(UpdateCartItemDto updateCartItemDto, User user) throw if(updateCartItemDto.getQuantity() > product.getStock()) { throw new OperationNotSupportedException("QUANTITY_EXCEEDED_STOCK"); } - if(!product.getSize().contains(updateCartItemDto.getSize())) { - throw new NotFoundException("SIZE_NOT_AVAILABLE"); - } - if(!product.getColor().contains(updateCartItemDto.getColor())) { - throw new NotFoundException("COLOR_NOT_AVAILABLE"); - } - cartItem.setSize(updateCartItemDto.getSize()); - cartItem.setColor(updateCartItemDto.getColor()); cartItem.setQuantity(updateCartItemDto.getQuantity()); cartItemRepo.save(cartItem); } diff --git a/src/main/java/com/example/DOTSAPI/services/order/OrderServices.java b/src/main/java/com/example/DOTSAPI/services/order/OrderServices.java index b9ef76e..ab6e802 100644 --- a/src/main/java/com/example/DOTSAPI/services/order/OrderServices.java +++ b/src/main/java/com/example/DOTSAPI/services/order/OrderServices.java @@ -4,6 +4,7 @@ import com.example.DOTSAPI.dto.order.RequestOrderDto; import com.example.DOTSAPI.enums.OrderStatus; import com.example.DOTSAPI.enums.PaymentStatus; +import com.example.DOTSAPI.model.Order; import com.example.DOTSAPI.model.User; import javax.naming.OperationNotSupportedException; @@ -16,4 +17,5 @@ public interface OrderServices { OrderDto getOrder(Long orderId); void changeOrderStatus(Long orderId, OrderStatus status); void changePaymentStatus(Long orderId, PaymentStatus status); + List findAllOrders(); } diff --git a/src/main/java/com/example/DOTSAPI/services/order/OrderServicesImpl.java b/src/main/java/com/example/DOTSAPI/services/order/OrderServicesImpl.java index b6dba98..b400af5 100644 --- a/src/main/java/com/example/DOTSAPI/services/order/OrderServicesImpl.java +++ b/src/main/java/com/example/DOTSAPI/services/order/OrderServicesImpl.java @@ -66,6 +66,20 @@ public void placeOrder(RequestOrderDto requestOrderDto, User user) throws Operat cartSessionServices.deleteUserAllCartItems(user); } + @Override + public List findAllOrders() { + List orderDtos = new ArrayList<>(); + List orders = orderRepo.findAllByOrderByCreatedAtDesc(); + if(orders.isEmpty()) { + throw new NotFoundException("ORDER_EMPTY"); + } + for(Order order : orders) { + OrderDto orderDto = convertOrderToDto(order); + orderDtos.add(orderDto); + } + return orderDtos; + } + @Override public List getUserOrders(User user) { List orderDtos = new ArrayList<>(); diff --git a/src/main/java/com/example/DOTSAPI/services/product/ProductServicesImpl.java b/src/main/java/com/example/DOTSAPI/services/product/ProductServicesImpl.java index 16ccc22..f3faf01 100644 --- a/src/main/java/com/example/DOTSAPI/services/product/ProductServicesImpl.java +++ b/src/main/java/com/example/DOTSAPI/services/product/ProductServicesImpl.java @@ -48,7 +48,9 @@ public List findAllByCategoryName(String categoryName) { List products = productRepo.findAllByCategory(category); List responseProductDtos = new ArrayList<>(); for(Product product : products) { - responseProductDtos.add(new ResponseProductDto(product)); + ResponseProductDto responseProductDto = new ResponseProductDto(product); + responseProductDto.setDescription(null); + responseProductDtos.add(responseProductDto); } return responseProductDtos; } @@ -58,7 +60,9 @@ public List searchByKeyword(String keyword) { List products = productRepo.searchByKeyword(keyword.toLowerCase());; List responseProductDtos = new ArrayList<>(); for(Product product : products) { - responseProductDtos.add(new ResponseProductDto(product)); + ResponseProductDto responseProductDto = new ResponseProductDto(product); + responseProductDto.setDescription(null); + responseProductDtos.add(responseProductDto); } return responseProductDtos; } @@ -82,7 +86,9 @@ public ResponseProductDto updateProductById(Long id, ProductDto productDto) { Product product = productRepo.getById(id); Product updatedProduct = convertToEntity(productDto, product); updatedProduct.setModifiedAt(new Date()); - return new ResponseProductDto(productRepo.save(updatedProduct)); + ResponseProductDto responseProductDto = new ResponseProductDto(productRepo.save(updatedProduct)); + responseProductDto.setDescription(null); + return responseProductDto; } @Override diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 9b293f0..58343dd 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,23 +1,22 @@ -#server.port=8080 -#logging.level.org.springframework=ERROR +server.port=8080 +logging.level.org.springframework=ERROR -#spring.datasource.url=jdbc:postgresql://localhost:5432/postgres -#spring.datasource.username=postgres -#spring.datasource.password=okoh1234 +spring.datasource.url=jdbc:postgresql://localhost:5432/postgres +spring.datasource.username=postgres +spring.datasource.password=okoh1234 -spring.datasource.url=jdbc:postgresql://ec2-35-169-49-157.compute-1.amazonaws.com:5432/dc4jgg92i0llqv -spring.datasource.username=vpehjzvmglvfwe -spring.datasource.password=a33a2302349e07f7ade7b611b47d156c546d18f1cc78d15b56b0c74b75bf0ade - -#spring.datasource.hikari.connection-timeout=20000 -#spring.datasource.hikari.maximum-pool-size=5 +#spring.datasource.url=jdbc:postgresql://ec2-35-169-49-157.compute-1.amazonaws.com:5432/dc4jgg92i0llqv +#spring.datasource.username=vpehjzvmglvfwe +#spring.datasource.password=a33a2302349e07f7ade7b611b47d156c546d18f1cc78d15b56b0c74b75bf0ade +spring.datasource.hikari.connection-timeout=20000 +spring.datasource.hikari.maximum-pool-size=5 spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true -#server.ssl.key-store=classpath:localhost.p12 -#server.ssl.key-store-type=PKCS12 -#server.ssl.key-store-password=changeit +server.ssl.key-store=classpath:localhost.p12 +server.ssl.key-store-type=PKCS12 +server.ssl.key-store-password=changeit #drop n create table again, good for testing, comment this in production spring.jpa.hibernate.ddl-auto=update