-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and display products and categories
- Loading branch information
1 parent
35e81ab
commit 64211a7
Showing
43 changed files
with
1,004 additions
and
246 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"use server"; | ||
import Category from "@/lib/schemas/Category"; | ||
import checkRole from "@/utils/checkRole"; | ||
import dbConnect from "@/utils/dbConnect"; | ||
|
||
dbConnect(); | ||
|
||
export const createCategory = async (payload) => { | ||
//* Validate the admin first to protect tbis action | ||
if (!checkRole("admin")) { | ||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Unauthorized", | ||
status: 401, | ||
}) | ||
); | ||
} | ||
|
||
const categories = await Category.create(payload); | ||
const data = JSON.stringify(categories); | ||
|
||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Product Created Successfully", | ||
status: 200, | ||
data: data, | ||
}) | ||
); | ||
}; | ||
export const updateCategory = async (id, payload) => { | ||
//* Validate the admin first to protect tbis action | ||
if (!checkRole("admin")) { | ||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Unauthorized", | ||
status: 401, | ||
}) | ||
); | ||
} | ||
|
||
const categories = await Category.findByIdAndUpdate(id, payload); | ||
|
||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Category Created Successfully", | ||
status: 200, | ||
}) | ||
); | ||
}; | ||
|
||
export const deleteCategory = async (id) => { | ||
//* Validate the admin first to protect tbis action | ||
if (!checkRole("admin")) { | ||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Unauthorized", | ||
status: 401, | ||
}) | ||
); | ||
} | ||
|
||
const products = await Category.findByIdAndDelete(id); | ||
const data = JSON.stringify(products); | ||
|
||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Product Deleted Successfully", | ||
status: 200, | ||
data: data, | ||
}) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
"use server"; | ||
import FeaturedProduct from "@/lib/schemas/FeaturedProduct"; | ||
import Product from "@/lib/schemas/Product"; | ||
import checkRole from "@/utils/checkRole"; | ||
import dbConnect from "@/utils/dbConnect"; | ||
|
||
dbConnect(); | ||
|
||
export const createFeaturedProduct = async (payload) => { | ||
//* Validate the admin first to protect tbis action | ||
if (!checkRole("admin")) { | ||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Unauthorized", | ||
status: 401, | ||
}) | ||
); | ||
} | ||
const resp = await Product.findById(payload.productId); | ||
const image = resp.images[0]; | ||
|
||
const res = await FeaturedProduct.create({ ...payload, image }); | ||
|
||
if (res) { | ||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Featured Product Created Successfully", | ||
status: 200, | ||
data: res, | ||
}) | ||
); | ||
} else { | ||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Featured Product Not Created", | ||
status: 400, | ||
}) | ||
); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
"use server"; | ||
import Product from "@/lib/schemas/Product"; | ||
import checkRole from "@/utils/checkRole"; | ||
import dbConnect from "@/utils/dbConnect"; | ||
|
||
dbConnect(); | ||
|
||
export const createProduct = async (payload) => { | ||
//* Validate the admin first to protect tbis action | ||
if (!checkRole("admin")) { | ||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Unauthorized", | ||
status: 401, | ||
}) | ||
); | ||
} | ||
|
||
const products = await Product.create(payload); | ||
const data = JSON.stringify(products); | ||
|
||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Product Created Successfully", | ||
status: 200, | ||
data: data, | ||
}) | ||
); | ||
}; | ||
export const updateProduct = async (id, payload) => { | ||
//* Validate the admin first to protect tbis action | ||
if (!checkRole("admin")) { | ||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Unauthorized", | ||
status: 401, | ||
}) | ||
); | ||
} | ||
|
||
const products = await Product.findByIdAndUpdate(id, payload); | ||
const data = JSON.stringify(products); | ||
|
||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Product Created Successfully", | ||
status: 200, | ||
data: data, | ||
}) | ||
); | ||
}; | ||
export const deleteProduct = async (id) => { | ||
//* Validate the admin first to protect tbis action | ||
if (!checkRole("admin")) { | ||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Unauthorized", | ||
status: 401, | ||
}) | ||
); | ||
} | ||
|
||
const products = await Product.findByIdAndDelete(id); | ||
const data = JSON.stringify(products); | ||
|
||
return JSON.parse( | ||
JSON.stringify({ | ||
message: "Product Created Successfully", | ||
status: 200, | ||
data: data, | ||
}) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"use server"; | ||
import { revalidatePath } from "next/cache"; | ||
const revalidate = async (path) => { | ||
revalidatePath(path); | ||
}; | ||
export default revalidate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 5 additions & 6 deletions
11
app/(root)/_components/home/CategorySideBar/CategoryItem.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.