@@ -2,60 +2,103 @@ import { Request, Response } from "express";
22import Product from "../models/product.model" ;
33
44export const createProduct = async ( req : Request , res : Response ) => {
5- try {
6- const { name, description, price, category, stock } = req . body ;
7-
8- if ( ! name || ! description || ! price ) {
9- return res . status ( 400 ) . json ( { message : "Missing required fields" } ) ;
10- }
11-
12- const product = await Product . create ( { name, description, price, category, stock } ) ;
13- return res . status ( 201 ) . json ( product ) ;
14- } catch ( error ) {
15- console . error ( "Create Product Error:" , error ) ;
16- res . status ( 500 ) . json ( { message : "Server error" } ) ;
5+ try {
6+ const { name, description, price, category, stock } = req . body ;
7+
8+ if ( ! name || ! description || ! price ) {
9+ return res . status ( 400 ) . json ( { message : "Missing required fields" } ) ;
1710 }
11+
12+ const product = await Product . create ( {
13+ name,
14+ description,
15+ price,
16+ category,
17+ stock,
18+ } ) ;
19+ return res . status ( 201 ) . json ( product ) ;
20+ } catch ( error ) {
21+ console . error ( "Create Product Error:" , error ) ;
22+ res . status ( 500 ) . json ( { message : "Server error" } ) ;
23+ }
1824} ;
1925
20- export const getProducts = async ( _req : Request , res : Response ) => {
21- try {
22- const products = await Product . find ( ) . sort ( { createdAt : - 1 } ) ;
23- res . status ( 200 ) . json ( products ) ;
24- } catch ( error ) {
25- console . error ( "Get Products Error:" , error ) ;
26- res . status ( 500 ) . json ( { message : "Server error" } ) ;
26+ export const getProducts = async ( req : Request , res : Response ) => {
27+ try {
28+ const { search, category, minPrice, maxPrice, sort } = req . query ;
29+
30+ const query : any = { } ;
31+
32+ // 🔍 Search by name or description
33+ if ( search ) {
34+ query . $or = [
35+ { name : { $regex : search , $options : "i" } } ,
36+ { description : { $regex : search , $options : "i" } } ,
37+ ] ;
38+ }
39+
40+ // 🏷️ Filter by category
41+ if ( category && category !== "all" ) {
42+ query . category = category ;
43+ }
44+
45+ // 💰 Filter by price range
46+ if ( minPrice || maxPrice ) {
47+ query . price = { } ;
48+ if ( minPrice ) query . price . $gte = Number ( minPrice ) ;
49+ if ( maxPrice ) query . price . $lte = Number ( maxPrice ) ;
2750 }
51+
52+ // 🧾 Build base query
53+ let mongoQuery = Product . find ( query ) ;
54+
55+ // 📊 Sorting
56+ if ( sort === "asc" || sort === "lowToHigh" )
57+ mongoQuery = mongoQuery . sort ( { price : 1 } ) ;
58+ else if ( sort === "desc" || sort === "highToLow" )
59+ mongoQuery = mongoQuery . sort ( { price : - 1 } ) ;
60+ else mongoQuery = mongoQuery . sort ( { createdAt : - 1 } ) ;
61+
62+ const products = await mongoQuery ;
63+
64+ res . status ( 200 ) . json ( products ) ;
65+ } catch ( error ) {
66+ console . error ( "Get Products Error:" , error ) ;
67+ res . status ( 500 ) . json ( { message : "Server error" } ) ;
68+ }
2869} ;
2970
3071export const getProductById = async ( req : Request , res : Response ) => {
31- try {
32- const product = await Product . findById ( req . params . id ) ;
33- if ( ! product ) return res . status ( 404 ) . json ( { message : "Product not found" } ) ;
34- res . status ( 200 ) . json ( product ) ;
35- } catch ( error ) {
36- console . error ( "Get Product Error:" , error ) ;
37- res . status ( 500 ) . json ( { message : "Server error" } ) ;
38- }
72+ try {
73+ const product = await Product . findById ( req . params . id ) ;
74+ if ( ! product ) return res . status ( 404 ) . json ( { message : "Product not found" } ) ;
75+ res . status ( 200 ) . json ( product ) ;
76+ } catch ( error ) {
77+ console . error ( "Get Product Error:" , error ) ;
78+ res . status ( 500 ) . json ( { message : "Server error" } ) ;
79+ }
3980} ;
4081
4182export const updateProduct = async ( req : Request , res : Response ) => {
42- try {
43- const product = await Product . findByIdAndUpdate ( req . params . id , req . body , { new : true } ) ;
44- if ( ! product ) return res . status ( 404 ) . json ( { message : "Product not found" } ) ;
45- res . status ( 200 ) . json ( product ) ;
46- } catch ( error ) {
47- console . error ( "Update Product Error:" , error ) ;
48- res . status ( 500 ) . json ( { message : "Server error" } ) ;
49- }
83+ try {
84+ const product = await Product . findByIdAndUpdate ( req . params . id , req . body , {
85+ new : true ,
86+ } ) ;
87+ if ( ! product ) return res . status ( 404 ) . json ( { message : "Product not found" } ) ;
88+ res . status ( 200 ) . json ( product ) ;
89+ } catch ( error ) {
90+ console . error ( "Update Product Error:" , error ) ;
91+ res . status ( 500 ) . json ( { message : "Server error" } ) ;
92+ }
5093} ;
5194
5295export const deleteProduct = async ( req : Request , res : Response ) => {
53- try {
54- const product = await Product . findByIdAndDelete ( req . params . id ) ;
55- if ( ! product ) return res . status ( 404 ) . json ( { message : "Product not found" } ) ;
56- res . status ( 200 ) . json ( { message : "Product deleted successfully" } ) ;
57- } catch ( error ) {
58- console . error ( "Delete Product Error:" , error ) ;
59- res . status ( 500 ) . json ( { message : "Server error" } ) ;
60- }
96+ try {
97+ const product = await Product . findByIdAndDelete ( req . params . id ) ;
98+ if ( ! product ) return res . status ( 404 ) . json ( { message : "Product not found" } ) ;
99+ res . status ( 200 ) . json ( { message : "Product deleted successfully" } ) ;
100+ } catch ( error ) {
101+ console . error ( "Delete Product Error:" , error ) ;
102+ res . status ( 500 ) . json ( { message : "Server error" } ) ;
103+ }
61104} ;
0 commit comments