Skip to content

Commit

Permalink
Creating Product function
Browse files Browse the repository at this point in the history
  • Loading branch information
sevelinCa committed May 22, 2024
1 parent dc10604 commit b4a485a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/controllers/product.controller.ts
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})

}

}
10 changes: 10 additions & 0 deletions src/services/productService.ts
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
}
}

0 comments on commit b4a485a

Please sign in to comment.