Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions backend/Controller/sweetCategoriesController.js
Original file line number Diff line number Diff line change
@@ -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."})
}
}
22 changes: 22 additions & 0 deletions backend/Model/sweetCategoriesModel.js
Original file line number Diff line number Diff line change
@@ -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);
11 changes: 11 additions & 0 deletions backend/Route/sweetCategoriesRoute.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 3 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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 () => {
Expand Down