-
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.
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 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,44 @@ | ||
|
||
import { Request, Response } from "express"; | ||
import { saveProduct } from "../services/productService"; | ||
import Vendor from "../database/models/vendor"; | ||
|
||
export const createProduct = async(req:Request,res:Response)=>{ | ||
try { | ||
const tokenData = (req as any).token | ||
let existVendor = await Vendor.findByPk(tokenData.vendorId) | ||
if(existVendor){ | ||
|
||
const {name,image,description,discount,price,quantity,category} = req.body | ||
if(!name || !image || !description || !price || !quantity || !category){ | ||
return res.status(200).json("All Field are required") | ||
} | ||
const data = { | ||
name, | ||
image, | ||
description, | ||
discount: discount ? discount : 0, | ||
price, | ||
quantity, | ||
category, | ||
vendorId: tokenData.id | ||
} | ||
const save = await saveProduct(data) | ||
if(!save){ | ||
return res.status(500).json({error: "Failed to save data"}) | ||
} | ||
return res.status(201).json({message: "Product Created"}) | ||
} | ||
else{ | ||
return res.status(500).json({error: "No vendor found"}) | ||
} | ||
|
||
|
||
|
||
|
||
} catch (error:any) { | ||
res.status(500).json({error:error.message}) | ||
|
||
} | ||
|
||
} |
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,10 @@ | ||
import Product from "../database/models/product" | ||
|
||
export const saveProduct = async (data:any)=>{ | ||
const response = await Product.create(data) | ||
if(response){ | ||
return true | ||
}else{ | ||
return false | ||
} | ||
} |