Skip to content

Commit

Permalink
add count down in product and cart list
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinuy committed Nov 19, 2024
1 parent 0d9f11c commit 74ee005
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 13 deletions.
9 changes: 9 additions & 0 deletions backend/src/main/java/springweb/backend/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ public record Client(
String id,
List<GroceryProduct> shoppingList
) {
Client(
List<GroceryProduct> shoppingList
){
this(null, shoppingList);
}

public Client withId(String id){
return new Client(id,shoppingList);
}
}
4 changes: 2 additions & 2 deletions backend/src/main/java/springweb/backend/ClientController.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public Client updateTask(@PathVariable String id, @RequestBody Client clientDto)
return clientService.updateClient(id, clientDto);
}
@PutMapping("{idClient}/shoppingList/{idProduct}")
public void changeProductCountByIdFromClientById(@PathVariable String idClient, @PathVariable String idProduct) {
clientService.changeProductByIdFromClientById(idClient,idProduct);
public Client changeProductCountByIdFromClientById(@PathVariable String idClient, @PathVariable String idProduct, @RequestBody GroceryProduct productDto) {
return clientService.changeProductByIdFromClientById(idClient,idProduct,productDto);
}

@DeleteMapping({"{id}"})
Expand Down
32 changes: 30 additions & 2 deletions backend/src/main/java/springweb/backend/ClientService.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ public void deleteProductByIdFromClientById(String idClient, String idProduct) {
}


public void changeProductByIdFromClientById(String idClient, String idProduct) {
}
public Client changeProductByIdFromClientById(String idClient, String idProduct, GroceryProduct productToUpdate) {
if (clientRepository.existsById(idClient)) {
Client client = clientRepository.findById(idClient).orElseThrow(
() -> new NoSuchElementException("No Client found with Id: " + idClient)
);

boolean productExists = client.shoppingList().stream()
.anyMatch(product -> product.id().equals(idProduct));

if (!productExists) {
throw new NoSuchElementException("No Product found with Id: " + idProduct);
}

List<GroceryProduct> updatedList = client.shoppingList().stream()
.map(product -> {
if (product.id().equals(idProduct)) {
return productToUpdate.withId(product.id());
}
return product;
})
.toList();

client.shoppingList().clear();
client.shoppingList().addAll(updatedList);

return clientRepository.save(client);
} else {
throw new NoSuchElementException("No Client found with Id: " + idClient);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package springweb.backend;


import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import java.util.List;

Expand All @@ -21,9 +23,9 @@ public List<GroceryProduct> getAllGroceryProducts() {
return groceryService.getAllGroceryProducts();
}

@PutMapping("/{idProduct}")
public GroceryProduct updateProduct(@PathVariable String id, @RequestBody GroceryProduct productDto) {
return groceryService.updateProduct(id, productDto);
@PutMapping(path="/{idProduct}")
public GroceryProduct updateProduct(@PathVariable String idProduct, @RequestBody GroceryProduct productDto) {
return groceryService.updateProduct(idProduct, productDto);
}

}
14 changes: 14 additions & 0 deletions backend/src/main/java/springweb/backend/GroceryProduct.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,18 @@ public record GroceryProduct(
String status,
String image
) {
GroceryProduct(
String category,
String name,
double price,
Integer count,
String status,
String image
){
this(null, category, name,price,count,status,image);
}

public GroceryProduct withId(String id){
return new GroceryProduct(id,category, name,price,count,status,image);
}
}
4 changes: 2 additions & 2 deletions backend/src/main/java/springweb/backend/GroceryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public List<GroceryProduct> getAllGroceryProducts() {
}


public GroceryProduct updateProduct(String id, GroceryProduct productDto) {
public GroceryProduct updateProduct(String id, GroceryProduct product) {
if (groceryRepository.existsById(id)) {
GroceryProduct updatedProduct = new GroceryProduct(id, productDto.category(),productDto.name(),productDto.price(),productDto.count(),productDto.status(),productDto.image());
GroceryProduct updatedProduct = product.withId(product.id());
return groceryRepository.save(updatedProduct);
} else {
throw new NoSuchElementException("No Product found with Id:" + id);
Expand Down
32 changes: 31 additions & 1 deletion frontend/src/components/ProductCard.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
import "./ProductCard.css"
import {Product} from "../Product.ts";
import axios from "axios";
import {useState} from "react";
//import {useState} from "react";

type Props = {
product: Product;
update: ()=>void;
}

export default function ProductCard(props : Props) {

//const [product, setProduct] = useState<Product>();
const [count, setCount] = useState(props.product.count)

function addProductToCart() {
console.log(props.product.name + " added")



let newCount: number = count - 1;
if(newCount<0){
newCount = 0;
}

setCount(newCount)

console.log(props.product.name + " added" + newCount)

axios.put("/api/store/products/"+props.product.id, {
...props.product,
count: newCount,
} as Product)
.then(response => {
console.log("Product count in store updated successfully:", response.data);
})
.then(props.update)
.catch(error => {
console.error("Error updating product count:", error);
});
}




return (
<div className="productCard-container" onClick={addProductToCart}>

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/ProductView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default function ProductView() {
<SearchBar onSearch={filterProducts}/> {/* Add the SearchBar component */}
<div className="productList-container">
{filteredProducts.map((product) => {
return <ProductCard product={product} key={product.id}/>;
return <ProductCard product={product} key={product.id} update={fetchAllProducts}/>;
})}
</div>
</div>
Expand Down
26 changes: 25 additions & 1 deletion frontend/src/components/ShoppingListCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import "./ShoppingListCard.css"
//import {Client} from "../Client.ts";
import {Product} from "../Product.ts";
import axios from "axios";
import {useState} from "react";


type Props = {
product: Product
update: ()=>void;
}

export function ShoppingListCard(props: Props) {

const [count, setCount] = useState(props.product.count)

function deleteProductFromCart() {
console.log(props.product.name + " deleted")
axios({
/* axios({
method: "PUT",
url: `api/store/clients/1/shoppingList/${props.product.id}`,
data: {
Expand All @@ -22,9 +26,29 @@ export function ShoppingListCard(props: Props) {
.then(response => {
console.log("Product count updated successfully:", response.data);
})
.catch(error => {
console.error("Error updating product count:", error);
});*/

let newCount: number = count - 1;
if(newCount<0){
newCount = 0;
}

setCount(newCount)

axios.put("/api/store/clients/1/shoppingList/"+props.product.id, {
...props.product,
count: newCount,
} as Product)
.then(response => {
console.log("Product count in cart updated successfully:", response.data);
})
.then(props.update)
.catch(error => {
console.error("Error updating product count:", error);
});

}

return (
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/ShoppingListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default function ShoppingListView() {
{
client.shoppingList.map((product) => {
return (
<ShoppingListCard key={product.id} product={product}/>
<ShoppingListCard key={product.id} product={product} update={fetchClients}/>
)
})
}
Expand Down

0 comments on commit 74ee005

Please sign in to comment.