Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend nav bar controller #6

Open
wants to merge 4 commits into
base: backend
Choose a base branch
from
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
146 changes: 146 additions & 0 deletions server/controllers/navbarController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import {NextFunction, Request, Response} from 'express';
import mongoose from 'mongoose';
// import model here once done named NavBarTitle

const create = (req: Request, res: Response, next: NextFunction) => {
let {category, index, parentCategory} = req.body;

const navBarheading = new NavBarTitle({
_id: new mongoose.Types.ObjectId(),
category, // name of the heading
index, // decides ordering
parentCategory // if subHeading, it can list the name of the parent category from category
});

return navBarheading
.save()
.then((newNavBarheading) => {
return res.status(201).json({blog: newNavBarheading});
})
.catch((error) => {
return res.status(500).json({
message: error.message
});
});
};

const read = (req: Request, res: Response, next: NextFunction) => {
const _id = req.params.pageID;

NavBarTitle.findById(_id)
.exec()
.then((title) => {
if (title) {
return res.status(200).json({title});
} else {
return res.status(404).json({
error: 'Page not found.'
});
}
})
.catch((error) => {
return res.status(500).json({
error: error.message
});
});
};

const readByCategory = (req: Request, res: Response, next: NextFunction) => {
const category = req.params.category;

NavBarTitle.find({category: category})
.exec()
.then((titles) => {
if (titles) {
return res.status(200).json({
count: titles.length,
titles: titles
});
} else {
return res.status(404).json({
error: 'Pages not found.'
});
}
})
.catch((error) => {
return res.status(500).json({
error: error.message
});
});
};

const readAll = (req: Request, res: Response, next: NextFunction) => {
const category = req.params.category;

NavBarTitle.find()
.exec()
.then((titles) => {
if (titles) {
return res.status(200).json({
count: titles.length,
titles: titles
});
} else {
return res.status(404).json({
error: 'Pages not found.'
});
}
})
.catch((error) => {
return res.status(500).json({
error: error.message
});
});
};

const edit = (req: Request, res: Response, next: NextFunction) => {
const _id = req.params.pageID;

NavBarTitle.findById(_id)
.exec()
.then((title) => {
if (title) {
title.set(req.body);
tile.save()
.then((savedTitle) => {

return res.status(201).json({
title: savedTitle
});
})
.catch((error) => {

return res.status(500).json({
message: error.message
});
});
} else {
return res.status(401).json({
message: 'NOT FOUND'
});
}
})
.catch((error) => {
return res.status(500).json({
message: error.message
});
});
};

const deletePage = (req: Request, res: Response, next: NextFunction) => {
const _id = req.params.blogID;
NavBarTitle.findByIdAndDelete(_id)
.exec()
.then(() => {
return res.status(201).json({
message: 'Page deleted'
});
})
.catch((error) => {
return res.status(500).json({
message: error.message
});
});
};

export default {create, read, readByCategory, edit, deletePage};
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@types/express": "^4.17.17",
"@types/node": "^18.15.5",
"express": "^4.18.2",
"mongoose": "^7.0.4",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0",
"typescript": "^5.0.2"
Expand Down
Loading