Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Pachj committed Jan 5, 2024
1 parent 4b37b3c commit 1b8d613
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch.bbw.shopibackend.controller

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

Expand All @@ -21,8 +22,8 @@ class CartController(
cartService.addItemToCart(product, bearerToken)
}

@DeleteMapping
fun deleteProduct() {

@PutMapping
fun changeAmount(@RequestHeader("Authorization") bearerToken: String, @RequestBody cartItem: CartItem): Cart? {
return cartService.changeAmount(cartItem.product, bearerToken, cartItem.amount)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ch.bbw.shopibackend.model
import ch.bbw.shopibackend.SimpleProduct

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

data class CartItem(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ class CartRepository(
private val context: DSLContext
) {

fun delete() {
fun deleteProductFromCart(productId: Int, userId: Int): Cart? {
context.deleteFrom(CART_PRODUCT)
.where(CART_PRODUCT.CART_FK.eq(userId))
.and(CART_PRODUCT.PRODUCT_FK.eq(productId))
.execute()

return getCart(userId)
}

fun add(productId: Int, userId: Int) {
Expand Down Expand Up @@ -59,7 +65,7 @@ class CartRepository(
.fetch()

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

return Cart(cartItems.map {
Expand All @@ -76,4 +82,14 @@ class CartRepository(
)
})
}

fun changeAmount(productId: Int, userId: Int, amount: Int): Cart? {
context.update(CART_PRODUCT)
.set(CART_PRODUCT.COUNT, amount)
.where(CART_PRODUCT.CART_FK.eq(userId))
.and(CART_PRODUCT.PRODUCT_FK.eq(productId))
.execute()

return getCart(userId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ class CartService(
private val cartRepository: CartRepository,
private val userService: UserService,
) {

fun deleteCart() {
cartRepository.delete()
}

fun addItemToCart(product: SimpleProduct, bearerToken: String) {
val userId = userService.getUserIdFromToken(bearerToken) ?: throw Exception("User not valid")
cartRepository.add(product.id, userId)
Expand All @@ -25,4 +20,14 @@ class CartService(
return cartRepository.getCart(userId)
}

fun changeAmount(product: SimpleProduct, bearerToken: String, amount: Int): Cart? {
val userId = userService.getUserIdFromToken(bearerToken) ?: throw Exception("User not valid")

if (amount == 0) {
return cartRepository.deleteProductFromCart(product.id, userId)
}

return cartRepository.changeAmount(product.id, userId, amount)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ CREATE TABLE IF NOT EXISTS cart

CREATE TABLE IF NOT EXISTS cart_product
(
count SMALLINT NOT NULL,
count INT NOT NULL,
cart_fk INT NOT NULL,
CONSTRAINT cart_fk
FOREIGN KEY (cart_fk) REFERENCES cart (id),
Expand Down
1 change: 0 additions & 1 deletion shopi-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"@types/node": "^16.18.59",
"@types/react": "^18.2.33",
"@types/react-dom": "^18.2.14",
"jsonwebtoken": "^9.0.2",
"jwt-decode": "^4.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
72 changes: 62 additions & 10 deletions shopi-frontend/src/components/ShoppingCart/ShoppingCart.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useEffect, useState } from "react";
import { Box, Modal, Table, TableBody, TableContainer, Typography } from "@mui/material";
import { getCart } from "../../helpers/cartHelpers";
import { Box, CircularProgress, Modal, Table, TableBody, TableContainer, TextField, Typography } from "@mui/material";
import { getCart, updateCart } from "../../helpers/cartHelpers";
import { Product } from "../ProductOverview/ProductOverview";

const boxStyle = {
position: "absolute",
Expand All @@ -13,22 +14,73 @@ const boxStyle = {
p: 4,
};

type ShoppingCart = {
items: ShoppingCartItem[];
};

type ShoppingCartItem = {
product: Product;
amount: number;
};

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

useEffect(() => {
getCart().then((cart) => setShoppingCart(cart));
if (open && !shoppingCartIsLoaded) {
getCart().then((cart) => {
setShoppingCart(cart);
setShoppingCartIsLoaded(true);
});
}
}, [open]);

const updateAmount = (product: Product, amount: number) => {
setShoppingCart(null);
setShoppingCartIsLoaded(false);
updateCart(product, amount).then((cart) => {
setShoppingCart(cart);
setShoppingCartIsLoaded(true);
});
};

return (
<Modal open={open} onClose={() => setOpen(false)}>
<Modal
open={open}
onClose={() => {
setOpen(false);
setShoppingCart(null);
setShoppingCartIsLoaded(false);
}}
>
<Box sx={boxStyle}>
<Typography variant={"h4"}>Warenkorb</Typography>
<TableContainer>
<Table>
<TableBody></TableBody>
</Table>
</TableContainer>
{!shoppingCartIsLoaded && <CircularProgress />}
{shoppingCartIsLoaded && shoppingCart && shoppingCart.items.length === 0 && (
<Typography variant={"h5"}>Keine Produkte im Warenkorb</Typography>
)}
{shoppingCartIsLoaded && shoppingCart !== null && (
<TableContainer>
<Table>
<TableBody>
{shoppingCart.items.map((item) => (
<tr key={item.product.id}>
<td>
<TextField
type={"number"}
value={item.amount}
onChange={(e) => updateAmount(item.product, parseInt(e.target.value))}
/>
</td>
<td>{item.product.name}</td>
<td>{item.amount * item.product.price}</td>
</tr>
))}
</TableBody>
</Table>
</TableContainer>
)}
</Box>
</Modal>
);
Expand Down
15 changes: 15 additions & 0 deletions shopi-frontend/src/helpers/cartHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,20 @@ export const getCart = async () => {
},
});
const data = await response.json();

return data;
};

export const updateCart = async (product: Product, amount: number) => {
const response = await fetch("http://localhost:8080/cart", {
method: "PUT",
headers: {
"Access-control-allow-origin": "*",
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("accessToken")}`,
},
body: JSON.stringify({ product: product, amount: amount }),
});

return await response.json();
};

0 comments on commit 1b8d613

Please sign in to comment.