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
1 change: 0 additions & 1 deletion .env

This file was deleted.

3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
221 changes: 97 additions & 124 deletions controllers/temple.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,21 @@ const apiKey =
'Ezl0961tEpx2UxTZ5v2uKFK91qdNAr5npRlMT1zLcE3Mg68Xwaj3N8Dyp1R8IvFenrVwHRllOUxF0Og00l0m9NcaYMtH6Bpgdv7N';

exports.create = (req, res) => {
// Validate request
if (!req.body.name) {
res.status(400).send({ message: 'Content can not be empty!' });
return;
return res.status(400).send({ message: 'Content can not be empty!' });
}

// Create a Temple
const temple = new Temple({
temple_id: req.body.temple_id,
name: req.body.name,
description: req.body.description,
location: req.body.location,
dedicated: req.body.dedicated,
additionalInfo: req.body.additionalInfo,
});
// Save Temple in the database

temple
.save(temple)
.save()
.then((data) => {
res.send(data);
})
Expand All @@ -33,129 +32,103 @@ exports.create = (req, res) => {
};

exports.findAll = (req, res) => {
console.log(req.header('apiKey'));
if (req.header('apiKey') === apiKey) {
Temple.find(
{},
{
temple_id: 1,
name: 1,
location: 1,
dedicated: 1,
additionalInfo: 1,
_id: 0,
}
)
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || 'Some error occurred while retrieving temples.',
});
});
} else {
res.send('Invalid apiKey, please read the documentation.');
if (req.header('apiKey') !== apiKey) {
return res.status(401).send('Invalid apiKey, please read the documentation.');
}

Temple.find(
{},
{
temple_id: 1,
name: 1,
location: 1,
dedicated: 1,
additionalInfo: 1,
_id: 0,
}
)
.then((data) => {
res.send(data);
})
.catch((err) => {
res.status(500).send({
message:
err.message || 'Some error occurred while retrieving temples.',
});
});
};

// Find a single Temple with an id
exports.findOne = (req, res) => {
const temple_id = req.params.temple_id;
if (req.header('apiKey') === apiKey) {
Temple.find({ temple_id: temple_id })
.then((data) => {
if (!data)
res
.status(404)
.send({ message: 'Not found Temple with id ' + temple_id });
else res.send(data[0]);
})
.catch((err) => {
res.status(500).send({
message: 'Error retrieving Temple with temple_id=' + temple_id,
});
const temple_id = Number(req.params.temple_id);

if (req.header('apiKey') !== apiKey) {
return res.status(401).send('Invalid apiKey, please read the documentation.');
}

Temple.findOne({ temple_id })
.then((data) => {
if (!data) {
return res
.status(404)
.send({ message: 'Not found Temple with id ' + temple_id });
}

res.send(data);
})
.catch((err) => {
res.status(500).send({
message: 'Error retrieving Temple with temple_id=' + temple_id,
});
} else {
res.send('Invalid apiKey, please read the documentation.');
});
};

exports.update = (req, res) => {
const temple_id = Number(req.params.temple_id);

if (!req.body || Object.keys(req.body).length === 0) {
return res.status(400).send({
message: 'Data to update can not be empty!',
});
}

Temple.findOneAndUpdate({ temple_id }, req.body, {
new: true,
runValidators: true,
})
.then((data) => {
if (!data) {
return res.status(404).send({
message: 'Cannot update Temple with temple_id=' + temple_id,
});
}

res.send(data);
})
.catch((err) => {
res.status(500).send({
message: 'Error updating Temple with temple_id=' + temple_id,
});
});
};

// // Update a Temple by the id in the request
// exports.update = (req, res) => {
// if (!req.body) {
// return res.status(400).send({
// message: 'Data to update can not be empty!',
// });
// }

// const id = req.params.id;

// Temple.findByIdAndUpdate(id, req.body, { useFindAndModify: false })
// .then((data) => {
// if (!data) {
// res.status(404).send({
// message: `Cannot update Temple with id=${id}. Maybe Temple was not found!`,
// });
// } else res.send({ message: 'Temple was updated successfully.' });
// })
// .catch((err) => {
// res.status(500).send({
// message: 'Error updating Temple with id=' + id,
// });
// });
// };

// // Delete a Temple with the specified id in the request
// exports.delete = (req, res) => {
// const id = req.params.id;

// Temple.findByIdAndRemove(id)
// .then((data) => {
// if (!data) {
// res.status(404).send({
// message: `Cannot delete Temple with id=${id}. Maybe Temple was not found!`,
// });
// } else {
// res.send({
// message: 'Temple was deleted successfully!',
// });
// }
// })
// .catch((err) => {
// res.status(500).send({
// message: 'Could not delete Temple with id=' + id,
// });
// });
// };

// // Delete all Temples from the database.
// exports.deleteAll = (req, res) => {
// Temple.deleteMany({})
// .then((data) => {
// res.send({
// message: `${data.deletedCount} Temples were deleted successfully!`,
// });
// })
// .catch((err) => {
// res.status(500).send({
// message:
// err.message || 'Some error occurred while removing all temple.',
// });
// });
// };

// // Find all published Temples
// exports.findAllPublished = (req, res) => {
// Temple.find({ published: true })
// .then((data) => {
// res.send(data);
// })
// .catch((err) => {
// res.status(500).send({
// message:
// err.message || 'Some error occurred while retrieving temple.',
// });
// });
// };
exports.delete = (req, res) => {
const temple_id = Number(req.params.temple_id);

Temple.findOneAndDelete({ temple_id })
.then((data) => {
if (!data) {
return res.status(404).send({
message: 'Cannot delete Temple with temple_id=' + temple_id,
});
}

res.send({
message: 'Temple was deleted successfully!',
});
})
.catch((err) => {
res.status(500).send({
message: 'Could not delete Temple with temple_id=' + temple_id,
});
});
};
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
const express = require('express');
const cors = require('cors');
const swaggerUi = require('swagger-ui-express');
const swaggerDocument = require('./swagger.json');
const app = express();

app
.use(cors())
.use(express.json())
.use(express.urlencoded({ extended: true }))
.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument))
.use('/', require('./routes'));

const db = require('./models');
Expand Down
24 changes: 11 additions & 13 deletions models/temples.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
module.exports = (mongoose) => {
const Temple = mongoose.model(
'temples',
mongoose.Schema(
{
temple_id: Number,
name: String,
location: String,
dedicated: String,
additionalInfo: Boolean,
},
{ timestamps: true }
)
const schema = new mongoose.Schema(
{
temple_id: Number,
name: String,
description: String,
location: String,
dedicated: String,
additionalInfo: mongoose.Schema.Types.Mixed,
},
{ timestamps: true }
);

return Temple;
return mongoose.model('temples', schema);
};
Loading