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

Routes for hostel added #1

Open
wants to merge 1 commit into
base: main
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
2 changes: 2 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const footerRouter = require("./routes/footer");
const aboutRouter = require("./routes/about");
const testimonialRouter = require("./routes/testimonial");
const clubRouter = require("./routes/club");
const hostelRouter = require("./routes/hostel");

const bodyParser = require("body-parser");
//----------------------------------->
Expand Down Expand Up @@ -49,6 +50,7 @@ app.use("/footer", footerRouter);
app.use("/about", aboutRouter);
app.use("/testimonial", testimonialRouter);
app.use("/club", clubRouter);
app.use("/hostel", hostelRouter);

//Export----------------------------->
module.exports = app;
Expand Down
57 changes: 57 additions & 0 deletions controllers/hostel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const hostel = require("./../models/hostel");
//----------------------------------->

//----------------------------------------------------------------------->
exports.addHostel = async (req, res) => {
const Hostel = new hostel(req.body);
Hostel.save()
.then(() => res.status(201).json(Hostel))
.catch((err) => res.status(500).json("Error: " + err));
};

//----------------------------------------------------------------------->
exports.getHostel = async (req, res) => {
hostel
.find({ show: true, _id: req.params.id })
.then((hostel) => res.status(200).json(hostel))
.catch((err) => res.status(404).json("Error: " + err));
};

//----------------------------------------------------------------------->
exports.deleteHostel = async (req, res) => {
hostel
.findByIdAndUpdate(
req.params.id,
{
$set: {
show: false,
},
},
{ useFindAndModify: false }
)
.then((hostel) => res.status(200).json(hostel))
.catch((err) => res.status(400).json("Error: " + err));
};

//----------------------------------------------------------------------->
exports.getHostelall = async (req, res) => {
if (req.body.showDeleted) {
hostel
.find()
.then((hostel) => res.status(200).json(hostel))
.catch((err) => res.status(404).json("Error: " + err));
} else {
hostel
.find({ show: true })
.then((hostel) => res.status(200).json(hostel))
.catch((err) => res.status(404).json("Error: " + err));
}
};

//----------------------------------------------------------------------->
exports.updateHostel = async (req, res) => {
hostel
.findByIdAndUpdate(req.params.id, req.body, { useFindAndModify: false })
.then(() => res.status(200).json("hostel Updated Successfully!"))
.catch((err) => res.status(404).json("Error: " + err));
};
79 changes: 79 additions & 0 deletions models/hostel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const mongoose = require("mongoose");
//----------------------------------->

//Schema---------------------------->
const Schema = new mongoose.Schema(
{
messageText: {
type: String,
default: "",
},
name: {
type: String,
default: "",
},
designation: {
type: String,
default: "",
},
hostel: {
type: String,
default: "",
},
profileLink: {
type: String,
default: "",
},
qualification: {
type: Object,
default: {
degree: "",
university: "",
year: "",
},
},
image: {
type: String,
default: "",
},

sourceOfInfo: {
type: Object,
default: {
name: null,
email: null,
designation: null,
department: null,
},
},
contact: {
type: Object,
default: {
email: null,
telNo: null,
EPABX: null,
Extn: null,
fax: null,
},
},
show: { type: Boolean, default: true },
},
{
timestamps: true,
}
);

//Model---------------------------->
const Model = mongoose.model("Hostel", Schema);

//Export----------------------------->
module.exports = Model;

// format of input in api calls

// Format of form:
// 1. it is has the above fields create input feild for all of them
// 2. for the source of info and contact create seperate fields in the form using the default object keys
// 3. keep the table below the input field to view what has been added
// 4. keep an update and delete button in the table
// 5. update pre filled vaues should come along with the update button in the new form
16 changes: 16 additions & 0 deletions routes/hostel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const express = require('express');
const hostelController = require('../controllers/hostel');
//----------------------------------->

//Router
const Router = express.Router();

Router.route('/').post(hostelController.addHostel).get(hostelController.getHostel);

Router.route('/get/all').get(hostelController.getHostelall);

Router.route('/:id').patch(hostelController.updateHostel).get(hostelController.getHostel).post(hostelController.deleteHostel);


//Export----------------------------->
module.exports = Router;