Skip to content

Commit

Permalink
getCard
Browse files Browse the repository at this point in the history
  • Loading branch information
Pachj committed Jan 5, 2024
1 parent 7b2f00d commit 8e78ca7
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/login")
@CrossOrigin(origins = ["http://localhost:3000"])
class AuthController(
private val authenticationService: AuthenticationService
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.bbw.shopibackend.controller

import ch.bbw.shopibackend.SimpleProduct
import ch.bbw.shopibackend.model.Cart
import ch.bbw.shopibackend.service.CartService
import org.springframework.web.bind.annotation.*

Expand All @@ -11,8 +12,8 @@ class CartController(
) {

@GetMapping
fun getCart() {

fun getCart(@RequestHeader("Authorization") bearerToken: String): Cart? {
return cartService.getCart(bearerToken)
}

@PostMapping
Expand Down
13 changes: 13 additions & 0 deletions shopi-backend/src/main/kotlin/ch/bbw/shopibackend/model/Cart.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package ch.bbw.shopibackend.model

import ch.bbw.shopibackend.SimpleProduct

data class Cart(
val items: List<CartItem>
)

data class CartItem(
val product: SimpleProduct,
val amount: Int,
)

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ch.bbw.shopibackend.repository

import ch.bbw.shopibackend.SimpleProduct
import ch.bbw.shopibackend.model.Cart
import ch.bbw.shopibackend.model.CartItem
import org.jooq.DSLContext
import org.jooq.generated.public_.Tables.CART
import org.jooq.generated.public_.Tables.CART_PRODUCT
import org.jooq.generated.public_.Tables.*
import org.springframework.stereotype.Repository

@Repository
Expand Down Expand Up @@ -40,6 +42,38 @@ class CartRepository(
}
}

fun getCart(userId: Int) {
fun getCart(userId: Int): Cart? {
val cartItems = context.select(
PRODUCT.ID.`as`("id"),
PRODUCT.NAME.`as`("name"),
PRODUCT.DESCRIPTION.`as`("description"),
PRODUCT.PRICE.`as`("price"),
PRODUCT.STOCK.`as`("stock"),
PRODUCT.IMAGE.`as`("imageLink"),
CART_PRODUCT.COUNT.`as`("amount"),
)
.from(CART)
.leftJoin(CART_PRODUCT).on(CART_PRODUCT.CART_FK.eq(CART.ID))
.leftJoin(PRODUCT).on(PRODUCT.ID.eq(CART_PRODUCT.PRODUCT_FK))
.where(CART.USER_FK.eq(userId))
.fetch()

if (cartItems.isEmpty()) {
return null
}

return Cart(cartItems.map {
CartItem(
product = SimpleProduct(
id = it.get("id", Int::class.java),
name = it.get("name", String::class.java),
description = it.get("description", String::class.java),
price = it.get("price", String::class.java),
stock = it.get("stock", Int::class.java),
imageLink = it.get("imageLink", String::class.java),
),
amount = it.get("amount", Int::class.java),
)
})
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package ch.bbw.shopibackend.service

import ch.bbw.shopibackend.SimpleProduct
import ch.bbw.shopibackend.model.Cart
import ch.bbw.shopibackend.repository.CartRepository
import org.springframework.stereotype.Service

@Service
class CartService(
private val cartRepository: CartRepository,
private val tokenService: TokenService,
private val userService: UserService,
) {

Expand All @@ -20,8 +20,9 @@ class CartService(
cartRepository.add(product.id, userId)
}

fun getCart(userId: Int) {
cartRepository.getCart(userId)
fun getCart(bearerToken: String): Cart? {
val userId = userService.getUserIdFromToken(bearerToken) ?: throw Exception("User not valid")
return cartRepository.getCart(userId)
}

}
9 changes: 8 additions & 1 deletion shopi-frontend/src/components/ShoppingCart/ShoppingCart.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import React, { useEffect, useState } from "react";
import { Box, Modal, Table, TableBody, TableContainer, Typography } from "@mui/material";
import { getCart } from "../../helpers/cartHelpers";

const boxStyle = {
position: "absolute",
Expand All @@ -13,6 +14,12 @@ const boxStyle = {
};

const ShoppingCart = ({ open, setOpen }: { open: boolean; setOpen: (b: boolean) => void }) => {
const [shoppingCart, setShoppingCart] = useState(null);

useEffect(() => {
getCart().then((cart) => setShoppingCart(cart));
}, [open]);

return (
<Modal open={open} onClose={() => setOpen(false)}>
<Box sx={boxStyle}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Button, Card, CardActions, CardContent } from "@mui/material";
import { Product } from "../ProductOverview/ProductOverview";
import style from "./SimpleProduct.module.css";
import { addToCart } from "../../helpers/cartHelpers";

const SimpleProduct = ({
product,
Expand All @@ -18,7 +19,7 @@ const SimpleProduct = ({
</CardContent>
<CardActions sx={{ display: "flex", justifyContent: "center" }}>
<Button onClick={() => setSelectedProduct(product)}>Details</Button>
<Button>Warenkorb</Button>
<Button onClick={() => addToCart(product)}>Warenkorb</Button>
</CardActions>
</Card>
);
Expand Down
12 changes: 11 additions & 1 deletion shopi-frontend/src/helpers/cartHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,17 @@ export const addToCart = async (product: Product) => {
},
body: JSON.stringify(product),
});
};

export const getCart = async () => {
const response = await fetch("http://localhost:8080/cart", {
method: "GET",
headers: {
"Access-control-allow-origin": "*",
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("accessToken")}`,
},
});
const data = await response.json();
console.log(data);
return data;
};

0 comments on commit 8e78ca7

Please sign in to comment.