-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
630 additions
and
60 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
...ver/springboot/src/main/java/com/lcaohoanq/shoppe/base/exception/OutOfStockException.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,12 @@ | ||
package com.lcaohoanq.shoppe.base.exception; | ||
|
||
import lombok.NoArgsConstructor; | ||
|
||
@NoArgsConstructor | ||
public class OutOfStockException extends RuntimeException{ | ||
|
||
public OutOfStockException(String message) { | ||
super(message); | ||
} | ||
|
||
} |
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
10 changes: 10 additions & 0 deletions
10
SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/cart/AddNewCartDTO.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,10 @@ | ||
package com.lcaohoanq.shoppe.domain.cart; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record AddNewCartDTO( | ||
@JsonProperty("user_id") | ||
@NotNull(message = "User id is required when creating a new cart") | ||
Long userId | ||
) {} |
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
69 changes: 69 additions & 0 deletions
69
SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/cart/CartController.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,69 @@ | ||
package com.lcaohoanq.shoppe.domain.cart; | ||
|
||
import com.lcaohoanq.shoppe.api.ApiResponse; | ||
import com.lcaohoanq.shoppe.domain.user.IUserService; | ||
import com.lcaohoanq.shoppe.domain.user.User; | ||
import com.lcaohoanq.shoppe.exception.MethodArgumentNotValidException; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.security.access.prepost.PreAuthorize; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
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.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("${api.prefix}/carts") | ||
@Slf4j | ||
@RequiredArgsConstructor | ||
public class CartController { | ||
|
||
private final ICartItemService cartItemService; | ||
private final IUserService userService; | ||
|
||
@GetMapping("") | ||
public ResponseEntity<ApiResponse<CartResponse>> getAll( | ||
@RequestParam(value = "page", defaultValue = "0") int page, | ||
@RequestParam(value = "limit", defaultValue = "10") int limit | ||
) { | ||
return ResponseEntity.ok( | ||
ApiResponse.<CartResponse>builder() | ||
.data(null) | ||
.build() | ||
); | ||
} | ||
|
||
@PostMapping("/item") | ||
@PreAuthorize("hasRole('ROLE_MEMBER')") | ||
public ResponseEntity<ApiResponse<CartItemResponse>> addItem( | ||
@Valid @RequestBody CartItemDTO cartItemDTO, | ||
BindingResult result | ||
) { | ||
|
||
if (result.hasErrors()) { | ||
throw new MethodArgumentNotValidException(result); | ||
} | ||
|
||
UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext() | ||
.getAuthentication().getPrincipal(); | ||
User user = userService.findByUsername(userDetails.getUsername()); | ||
|
||
return ResponseEntity.ok( | ||
ApiResponse.<CartItemResponse>builder() | ||
.message("Add item to cart successfully") | ||
.statusCode(HttpStatus.OK.value()) | ||
.isSuccess(true) | ||
.data(cartItemService.createCartItem(user.getId(),cartItemDTO)) | ||
.build() | ||
); | ||
} | ||
|
||
} |
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
9 changes: 7 additions & 2 deletions
9
SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/cart/CartItemDTO.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 |
---|---|---|
@@ -1,10 +1,15 @@ | ||
package com.lcaohoanq.shoppe.domain.cart; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotNull; | ||
|
||
public record CartItemDTO( | ||
@JsonProperty("product_id") | ||
Long productId, | ||
@Min(value = 1, message = "Product id must be greater than 0") | ||
@NotNull(message = "Product id is required") Long productId, | ||
|
||
@JsonProperty("quantity") | ||
Integer quantity | ||
@Min(value = 1, message = "Quantity must be greater than 0") | ||
@NotNull(message = "Quantity is required") Integer quantity | ||
) {} |
13 changes: 13 additions & 0 deletions
13
SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/cart/CartItemRepository.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,13 @@ | ||
package com.lcaohoanq.shoppe.domain.cart; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface CartItemRepository extends JpaRepository<CartItem, Long> { | ||
|
||
List<CartItem> findByCartId(Long cartId); | ||
|
||
Optional<CartItem> findByCartIdAndProductId(long cartId, long productId); | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
SPCServer/springboot/src/main/java/com/lcaohoanq/shoppe/domain/cart/CartItemResponse.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,26 @@ | ||
package com.lcaohoanq.shoppe.domain.cart; | ||
|
||
import com.fasterxml.jackson.annotation.JsonFormat; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.lcaohoanq.shoppe.domain.product.Product; | ||
import com.lcaohoanq.shoppe.domain.product.ProductResponse; | ||
import java.time.LocalDateTime; | ||
|
||
public record CartItemResponse( | ||
@JsonProperty("id") | ||
Long id, | ||
@JsonProperty("cart_id") | ||
Long cartId, | ||
@JsonProperty("product") | ||
ProductResponse product, | ||
@JsonProperty("quantity") | ||
int quantity, | ||
|
||
@JsonProperty("created_at") | ||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS") | ||
LocalDateTime createdAt, | ||
|
||
@JsonProperty("updated_at") | ||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSSSSS") | ||
LocalDateTime updatedAt | ||
) {} |
Oops, something went wrong.