Skip to content

Commit

Permalink
Database Cart
Browse files Browse the repository at this point in the history
  • Loading branch information
caunhach committed Mar 23, 2024
1 parent 4f59424 commit 0ac1c70
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
package com.kampus.kbazaar.cart;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1")
public class CartController {

@Autowired
private final CartService cartService;

public CartController(CartService cartService) {
this.cartService = cartService;
}

@GetMapping("/carts")
public ResponseEntity getCart() { // NOSONAR
return ResponseEntity.ok().build();
}


@PostMapping("/carts/{username}/promotions")
public ResponseEntity createCartPromotions(@PathVariable String username) {

cartService.createCartPromotions30UpTo200();
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package com.kampus.kbazaar.cart;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface CartRepository {
@Query("SELECT c FROM Cart c WHERE c.shopper.id = :userId")
List<Cart> findByUserId(@Param("userId") Long userId);
}
18 changes: 18 additions & 0 deletions kbazaar/src/main/java/com/kampus/kbazaar/cart/CartService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package com.kampus.kbazaar.cart;

import com.kampus.kbazaar.exceptions.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartService {
@Autowired
private final CartRepository cartRepository;

public CartService(CartRepository cartRepository) {
this.cartRepository = cartRepository;
}

public void createCartPromotions30UpTo200() {
List<Cart> carts = cartRepository.findByUserId(1L);
if (cartRepository.findByUserId(1L).isEmpty())
throw new NotFoundException("Cart not found");
}
}
6 changes: 3 additions & 3 deletions kbazaar/src/main/resources/sql/schema/cart.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE cart (
cart_id SERIAL PRIMARY KEY,
quantity INT NOT NULL,
(user_id) REFERENCES shopper(id)
(product_id) REFERENCES product(id)
(promotion_id) REFERENCES promotion(promotion_id)
user_id INT NOT NULL,
product_id INT NOT NULL,
promotion_id INT NOT NULL
)

0 comments on commit 0ac1c70

Please sign in to comment.