Skip to content

Commit fe52f40

Browse files
committed
basics working fine till now, working on elastic search from here
1 parent c89828e commit fe52f40

File tree

16 files changed

+9445
-19
lines changed

16 files changed

+9445
-19
lines changed

frontend/src/app/cart/page.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,53 @@ import CartItem from '@/components/CartItem'
66
import { formatPrice } from '@/lib/utils'
77

88
export default function CartPage() {
9-
const [cart, setCart] = useState<any>(null)
9+
const [cart, setCart] = useState<any | null>(null)
10+
const [error, setError] = useState<string | null>(null)
1011

1112
async function load() {
12-
const data = await getCart()
13-
setCart(data)
13+
try {
14+
const data = await getCart()
15+
setCart(data)
16+
} catch (err) {
17+
console.error(err)
18+
setError('Failed to load cart')
19+
setCart({ items: [], subtotal: 0 })
20+
}
1421
}
1522

1623
useEffect(() => {
1724
load()
1825
}, [])
1926

20-
if (!cart) return <div>Loading…</div>
27+
if (error) {
28+
return <div className="p-4 text-red-600">{error}</div>
29+
}
30+
31+
if (!cart) {
32+
return <div className="p-4">Loading…</div>
33+
}
34+
35+
const items = cart.items ?? []
2136

2237
return (
2338
<div className="max-w-3xl mx-auto p-4">
2439
<h1 className="text-xl font-semibold mb-4">Your Cart</h1>
2540

26-
{cart.items.length === 0 && <p>Your cart is empty</p>}
41+
{items.length === 0 && <p>Your cart is empty</p>}
2742

28-
{cart.items.map((item: any) => (
43+
{items.map((item: any) => (
2944
<CartItem key={item.variant.id} item={item} onRefresh={load} />
3045
))}
3146

3247
<div className="flex justify-between mt-6 text-lg font-semibold">
3348
<span>Subtotal</span>
34-
<span>{formatPrice(cart.Subtotal)}</span>
49+
<span>{formatPrice(cart.subtotal ?? 0)}</span>
3550
</div>
3651

3752
<button
38-
disabled={cart.items.length === 0}
53+
disabled={items.length === 0}
3954
className="mt-6 w-full bg-black text-white py-3 rounded disabled:opacity-50"
40-
onClick={() => location.href = '/checkout'}
55+
onClick={() => (location.href = '/checkout')}
4156
>
4257
Proceed to Checkout
4358
</button>

frontend/src/app/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,13 +351,13 @@ const [products, setProducts] = useState<ProductCard[]>([])
351351
{Math.round(((product.price + 1000 - product.price) / product.price) * 500)}% OFF
352352
</div>
353353
)}
354-
{!product.in_stock && (
354+
{/* {!product.in_stock && (
355355
<div className="absolute inset-0 bg-black/60 flex items-center justify-center">
356356
<span className="bg-white text-red-600 px-3 py-1.5 rounded font-semibold text-xs shadow">
357357
Out of Stock
358358
</span>
359359
</div>
360-
)}
360+
)} */}
361361
<button className="absolute top-2 right-2 bg-white rounded-full p-1.5 opacity-0 group-hover:opacity-100 transition shadow">
362362
<Heart className="w-4 h-4 text-gray-600 hover:fill-red-500 hover:text-red-500" />
363363
</button>

frontend/src/components/ProductDetailPage.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async function handleAddToCart() {
6464
})
6565

6666
// optional UX
67-
alert("Added to cart")
67+
// alert("Added to cart")
6868

6969
} catch (e: any) {
7070
alert(e.message || "Failed to add to cart")

frontend/src/components/header.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { useCartCount } from "@/lib/use-cart-count"
44

5-
import { Search, ShoppingCart, User, Menu, Heart,MapPin } from "lucide-react"
5+
import { Search, ShoppingCart, User, Menu, Heart,MapPin,UserCog } from "lucide-react"
66
import Link from "next/link"
77
import { useState } from "react"
88

@@ -112,6 +112,12 @@ export default function Header() {
112112
<User className="h-5 w-5 text-gray-700" />
113113
<span className="hidden text-sm font-medium text-gray-700 md:inline">Profile</span>
114114
</button>
115+
<Link href="/admin/products">
116+
<button className="flex items-center gap-2 rounded p-2 hover:bg-gray-100" aria-label="Admin">
117+
<UserCog className="h-5 w-5 text-gray-700" />
118+
<span className="hidden text-sm font-medium text-gray-700 md:inline">Admin</span>
119+
</button>
120+
</Link>
115121
</div>
116122
</div>
117123

services/product/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/go-chi/chi v1.5.5 // indirect
99
github.com/go-chi/chi/v5 v5.2.3 // indirect
1010
github.com/go-chi/cors v1.2.2 // indirect
11+
github.com/google/uuid v1.6.0 // indirect
1112
github.com/jackc/pgpassfile v1.0.0 // indirect
1213
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
1314
github.com/jackc/pgx/v5 v5.7.6 // indirect

services/product/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ github.com/go-chi/chi/v5 v5.2.3 h1:WQIt9uxdsAbgIYgid+BpYc+liqQZGMHRaUwp0JUcvdE=
55
github.com/go-chi/chi/v5 v5.2.3/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
66
github.com/go-chi/cors v1.2.2 h1:Jmey33TE+b+rB7fT8MUy1u0I4L+NARQlK6LhzKPSyQE=
77
github.com/go-chi/cors v1.2.2/go.mod h1:sSbTewc+6wYHBBCW7ytsFSn836hqM7JxpglAy2Vzc58=
8+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
9+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
810
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
911
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
1012
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=

services/product/internal/store/store.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,19 @@ func (s *Store) UpdateProduct(ctx context.Context, id string, p *model.Product)
314314
}
315315

316316
func (s *Store) DeleteProduct(ctx context.Context, id string) error {
317-
_, err := s.db.Exec(ctx, `
318-
UPDATE products
319-
SET deleted_at = NOW()
317+
cmd, err := s.db.Exec(ctx, `
318+
DELETE FROM products
320319
WHERE id = $1
321320
`, id)
321+
if err != nil {
322+
return err
323+
}
322324

323-
return err
325+
if cmd.RowsAffected() == 0 {
326+
return fmt.Errorf("product not found")
327+
}
328+
329+
return nil
324330
}
325331

326332
func (s *Store) CreateVariant(ctx context.Context, productID string, v *model.Variant) (string, error) {

services/product/migrations/001_create_products.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,4 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_product_media_url_unique
7171
ON product_media (url);
7272

7373
ALTER TABLE product_variants
74-
ADD COLUMN IF NOT EXISTS stock_reserved INT NOT NULL DEFAULT 0;
74+
ADD COLUMN IF NOT EXISTS stock_reserved INT NOT NULL DEFAULT 0;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
ALTER TABLE products
2-
ADD COLUMN IF NOT EXISTS min_price INT;
2+
ADD COLUMN IF NOT EXISTS min_price INT;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE product_variants
2+
ADD COLUMN IF NOT EXISTS sku TEXT;

0 commit comments

Comments
 (0)