diff --git a/backend/Controller/sweetCategoriesController.js b/backend/Controller/sweetCategoriesController.js new file mode 100644 index 00000000..14c2f8c5 --- /dev/null +++ b/backend/Controller/sweetCategoriesController.js @@ -0,0 +1,36 @@ +// Handling the buisness logic and interacting with the User model +import SweetCategories from "../Model/sweetCategoriesModel.js" + +export const get_categories = async(req, res)=>{ + try{ + const sweet_categories = await SweetCategories.find(); + res.status(200); + return res.json(sweet_categories); + } catch(error) { + res.status(500).json({error:"Internal Server error."}) + } +} + +export const create_category = async(req, res)=>{ + try{ + // Validation + if (req.body.average_price <= 0) { + throw new Error("Average price can not be 0 or below."); + } + const sweet_category = await SweetCategories.create({ + image_url: req.body.image_url, + description: req.body.description, + product_type: req.body.product_type, + average_price: req.body.average_price + }); + console.log(`Saved new sweet category.`); + res.status(201).json({ + success: true, + message: 'Sweet category created successfully', + category: sweet_category + }); + } catch(error) { + console.log(error); + res.status(500).json({error:"Internal Server error."}) + } +} \ No newline at end of file diff --git a/backend/Model/sweetCategoriesModel.js b/backend/Model/sweetCategoriesModel.js new file mode 100644 index 00000000..dcdd2fb0 --- /dev/null +++ b/backend/Model/sweetCategoriesModel.js @@ -0,0 +1,22 @@ +import mongoose from "mongoose"; + +const sweetCategoriesSchema = new mongoose.Schema({ + image_url: { // Stores image url. + type:String, + required: true + }, + description: { // Description of this sweet category. + type:String, + required: true + }, + product_type: { // Description of this sweet category. + type:String, + required: true + }, + average_price:{ + type:Number, + required: true + } +}) + +export default mongoose.model("sweetCategories", sweetCategoriesSchema); \ No newline at end of file diff --git a/backend/Route/sweetCategoriesRoute.js b/backend/Route/sweetCategoriesRoute.js new file mode 100644 index 00000000..55b93db6 --- /dev/null +++ b/backend/Route/sweetCategoriesRoute.js @@ -0,0 +1,11 @@ +import express from 'express'; +import { create_category, get_categories } from '../Controller/sweetCategoriesController.js'; + +const router = express.Router(); + +// GET +router.get('/get_categories', get_categories); +// POST +router.post('/create_category', create_category); + +export default router; \ No newline at end of file diff --git a/backend/index.js b/backend/index.js index 185d6dee..867c9aa0 100644 --- a/backend/index.js +++ b/backend/index.js @@ -9,6 +9,7 @@ import cors from "cors"; import cookieParser from "cookie-parser"; import { v4 as uuidv4 } from "uuid"; import exampleRoutes from './Route/exampleRoute.js'; +import sweetCategoriesRoutes from './Route/sweetCategoriesRoute.js'; const app = express(); @@ -188,6 +189,8 @@ app.get('/api/test-user', (req, res) => { // Call the exampleRoutes API here url: http://localhost:8000/api/{all api's are added here} app.use('/api', exampleRoutes); +// Sweet Categroies API +app.use('/api', sweetCategoriesRoutes); // Connect to MongoDB mongoose.connect(MONGOURL).then(async () => {