Skip to content

Commit

Permalink
Modified hibernate query
Browse files Browse the repository at this point in the history
  • Loading branch information
tuannm151 committed Dec 1, 2021
1 parent a3912ab commit 3a39b02
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 53 deletions.
22 changes: 22 additions & 0 deletions src/main/java/com/example/DOTSAPI/configuration/CorsConfigure.java
Original file line number Diff line number Diff line change
@@ -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("*");
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public ResponseEntity<List<OrderDto>> getUserOrders(Authentication authenticatio
return ResponseEntity.ok().body(orderServices.getUserOrders(user));
}

@GetMapping("/admin/list")
public ResponseEntity<List<OrderDto>> findAllOrders() {
return ResponseEntity.ok().body(orderServices.findAllOrders());
}

@GetMapping("/admin/{id}")
public ResponseEntity<OrderDto> getOrder(@PathVariable Long id) {
return ResponseEntity.ok().body(orderServices.getOrder(id));
Expand All @@ -58,4 +63,5 @@ public ResponseEntity<OrderDto> changePaymentStatus(@RequestBody @Valid ChangePa
orderServices.changePaymentStatus(changePaymentStatusDto.getOrderId(), changePaymentStatusDto.getStatus());
return ResponseEntity.ok().build();
}

}
12 changes: 2 additions & 10 deletions src/main/java/com/example/DOTSAPI/dto/cart/UpdateCartItemDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/example/DOTSAPI/dto/order/OrderDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/example/DOTSAPI/dto/product/ProductDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/example/DOTSAPI/model/Brand.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
@Setter
@NoArgsConstructor
public class Brand {
@JsonIgnore
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/example/DOTSAPI/model/CartItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/example/DOTSAPI/model/CartSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -42,6 +42,7 @@ public class CartSession {
private User user;

public CartSession(User user) {
this.createdAt = new Date();
this.user = user;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/example/DOTSAPI/model/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/example/DOTSAPI/model/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/example/DOTSAPI/model/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
public interface OrderRepo extends JpaRepository<Order, Long> {
List<Order> findAllByUserOrderByCreatedAtDesc(User user);
Order findByIdAndUser(Long id, User user);
List<Order> findAllByOrderByCreatedAtDesc();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,4 +17,5 @@ public interface OrderServices {
OrderDto getOrder(Long orderId);
void changeOrderStatus(Long orderId, OrderStatus status);
void changePaymentStatus(Long orderId, PaymentStatus status);
List<OrderDto> findAllOrders();
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ public void placeOrder(RequestOrderDto requestOrderDto, User user) throws Operat
cartSessionServices.deleteUserAllCartItems(user);
}

@Override
public List<OrderDto> findAllOrders() {
List<OrderDto> orderDtos = new ArrayList<>();
List<Order> 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<OrderDto> getUserOrders(User user) {
List<OrderDto> orderDtos = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public List<ResponseProductDto> findAllByCategoryName(String categoryName) {
List<Product> products = productRepo.findAllByCategory(category);
List<ResponseProductDto> 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;
}
Expand All @@ -58,7 +60,9 @@ public List<ResponseProductDto> searchByKeyword(String keyword) {
List<Product> products = productRepo.searchByKeyword(keyword.toLowerCase());;
List<ResponseProductDto> 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;
}
Expand All @@ -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
Expand Down
27 changes: 13 additions & 14 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 3a39b02

Please sign in to comment.