initial commit
This commit is contained in:
74
controllers/about.controller.js
Normal file
74
controllers/about.controller.js
Normal file
@@ -0,0 +1,74 @@
|
||||
var { About } = require("../models/models");
|
||||
|
||||
async function getSingleAbout(req, res, next) {
|
||||
try {
|
||||
let about = await About.findByPk(parseInt(req.params.id));
|
||||
res.json(about);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function getAllAbouts(req, res, next) {
|
||||
try {
|
||||
let about = await About.findAll();
|
||||
res.json(about);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function createSingleAbout(req, res, next) {
|
||||
try {
|
||||
let about = await About.create({
|
||||
title: req.fields.title,
|
||||
content: req.fields.content
|
||||
});
|
||||
res.json(about);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function updateSingleAbout(req, res, next) {
|
||||
try {
|
||||
let about = await About.findByPk(parseInt(req.params.id));
|
||||
|
||||
if(about) {
|
||||
about.title = req.fields.title;
|
||||
about.content = req.fields.content;
|
||||
about.save();
|
||||
res.json(about);
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteSingleAbout(req, res, next) {
|
||||
try {
|
||||
await About.destroy({
|
||||
where: {
|
||||
id: parseInt(req.params.id)
|
||||
}
|
||||
})
|
||||
res.end();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createSingleAbout,
|
||||
getSingleAbout,
|
||||
getAllAbouts,
|
||||
updateSingleAbout,
|
||||
deleteSingleAbout
|
||||
};
|
||||
76
controllers/adoptsection.controller.js
Normal file
76
controllers/adoptsection.controller.js
Normal file
@@ -0,0 +1,76 @@
|
||||
var { Adoptsection, Asset } = require("../models/models");
|
||||
|
||||
async function getSingleAdoptSection(req, res, next) {
|
||||
try {
|
||||
let adoptsection = await Adoptsection.findByPk(parseInt(req.params.id), { include: [ Asset ] });
|
||||
res.json(adoptsection);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function getAllAdoptSections(req, res, next) {
|
||||
try {
|
||||
let adoptsections = await Adoptsection.findAll({ include: [ Asset ] });
|
||||
res.json(adoptsections);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function createSingleAdoptSection(req, res, next) {
|
||||
try {
|
||||
let adoptsection = await Adoptsection.create({
|
||||
title: req.fields.title,
|
||||
content: req.fields.content,
|
||||
assetId: parseInt(req.fields.assetId)
|
||||
});
|
||||
res.json(adoptsection);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function updateSingleAdoptSection(req, res, next) {
|
||||
try {
|
||||
let adoptsection = await Adoptsection.findByPk(parseInt(req.params.id), { include: [ Asset ] });
|
||||
|
||||
if (adoptsection) {
|
||||
adoptsection.title = req.fields.title;
|
||||
adoptsection.content = req.fields.content;
|
||||
adoptsection.assetId = parseInt(req.fields.assetId);
|
||||
adoptsection.save();
|
||||
res.json(adoptsection);
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteSingleAdoptSection(req, res, next) {
|
||||
try {
|
||||
await Adoptsection.destroy({
|
||||
where: {
|
||||
id: parseInt(req.params.id)
|
||||
}
|
||||
});
|
||||
res.end();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createSingleAdoptSection,
|
||||
getSingleAdoptSection,
|
||||
getAllAdoptSections,
|
||||
updateSingleAdoptSection,
|
||||
deleteSingleAdoptSection
|
||||
};
|
||||
77
controllers/animal.controller.js
Normal file
77
controllers/animal.controller.js
Normal file
@@ -0,0 +1,77 @@
|
||||
var { Animal, Asset } = require("../models/models");
|
||||
|
||||
async function getSingleAnimal(req, res, next) {
|
||||
try {
|
||||
let animal = await Animal.findByPk(parseInt(req.params.id), { include: [ Asset ] });
|
||||
res.json(animal);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function getAllAnimals(req, res, next) {
|
||||
try {
|
||||
let animals = await Animal.findAll({ include: [ Asset ] });
|
||||
res.json(animals);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function createSingleAnimal(req, res, next) {
|
||||
try {
|
||||
let animal = await Animal.create({
|
||||
name: req.fields.name,
|
||||
description: req.fields.description,
|
||||
age: req.fields.age,
|
||||
assetId: parseInt(req.fields.assetId)
|
||||
});
|
||||
res.json(animal);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function updateSingleAnimal(req, res, next) {
|
||||
try {
|
||||
let animal = await Animal.findByPk(parseInt(req.params.id), { include: [ Asset ] });
|
||||
|
||||
if (animal) {
|
||||
animal.name = req.fields.name;
|
||||
animal.description = req.fields.description;
|
||||
animal.assetId = parseInt(req.fields.assetId);
|
||||
animal.save();
|
||||
res.json(animal);
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteSingleAnimal(req, res, next) {
|
||||
try {
|
||||
await Animal.destroy({
|
||||
where: {
|
||||
id: parseInt(req.params.id)
|
||||
}
|
||||
});
|
||||
res.end();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createSingleAnimal,
|
||||
getSingleAnimal,
|
||||
getAllAnimals,
|
||||
updateSingleAnimal,
|
||||
deleteSingleAnimal
|
||||
};
|
||||
41
controllers/asset.controller.js
Normal file
41
controllers/asset.controller.js
Normal file
@@ -0,0 +1,41 @@
|
||||
var { Asset } = require("../models/models");
|
||||
var saveFile = require("../services/asset");
|
||||
|
||||
async function createSingleAsset(req, res, next) {
|
||||
try {
|
||||
let file = saveFile(req.files.file);
|
||||
let asset = await Asset.create({
|
||||
url: "http://localhost:4000/file-bucket/" + file
|
||||
});
|
||||
res.json(asset);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function getAllAssets(req, res, next) {
|
||||
try {
|
||||
let assets = await Asset.findAll();
|
||||
res.json(assets);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function getSingleAsset(req, res, next) {
|
||||
try {
|
||||
let asset = await Asset.findByPk(req.params.id);
|
||||
res.json(asset);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createSingleAsset,
|
||||
getAllAssets,
|
||||
getSingleAsset
|
||||
};
|
||||
44
controllers/subscriber.controller.js
Normal file
44
controllers/subscriber.controller.js
Normal file
@@ -0,0 +1,44 @@
|
||||
var { Subscriber } = require("../models/models");
|
||||
|
||||
async function getAllSubscribers(req, res, next) {
|
||||
try {
|
||||
let subscribers = await Subscriber.findAll();
|
||||
res.json(subscribers);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function createSingleSubscriber(req, res, next) {
|
||||
try {
|
||||
let subscriber = await Subscriber.create({
|
||||
name: req.fields.name,
|
||||
email: req.fields.email
|
||||
});
|
||||
res.json(subscriber);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteSingleSubscriber(req, res, next) {
|
||||
try {
|
||||
await Subscriber.destroy({
|
||||
where: {
|
||||
email: req.params.email
|
||||
}
|
||||
});
|
||||
res.end();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createSingleSubscriber,
|
||||
getAllSubscribers,
|
||||
deleteSingleSubscriber
|
||||
};
|
||||
31
controllers/token.controller.js
Normal file
31
controllers/token.controller.js
Normal file
@@ -0,0 +1,31 @@
|
||||
var { User } = require("../models/models");
|
||||
var { compareSync } = require("bcryptjs");
|
||||
var { sign } = require("jsonwebtoken");
|
||||
|
||||
async function createToken(req, res, next) {
|
||||
try {
|
||||
let user = await User.findOne({ where: { username: req.fields.username } });
|
||||
|
||||
if (!user) return res.status(401).end();
|
||||
|
||||
if (!compareSync(req.fields.password, user.password))
|
||||
return res.status(401).end();
|
||||
|
||||
let token = sign({
|
||||
data: user
|
||||
}, process.env.JWT_SECRET, { expiresIn: "1h" });
|
||||
|
||||
res.json({
|
||||
userId: user.id,
|
||||
token,
|
||||
validUntil: Date.now() + (60*60*1000)
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createToken
|
||||
};
|
||||
34
controllers/user.controller.js
Normal file
34
controllers/user.controller.js
Normal file
@@ -0,0 +1,34 @@
|
||||
var { User, Class } = require("../models/models");
|
||||
var { hashSync } = require("bcryptjs");
|
||||
|
||||
async function getSingleUser(req, res, next) {
|
||||
try {
|
||||
let user = await User.findByPk(parseInt(req.params.id));
|
||||
if (user) {
|
||||
res.json(user);
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function createSingleUser(req, res, next) {
|
||||
try {
|
||||
let user = await User.create({
|
||||
username: req.fields.username,
|
||||
password: hashSync(req.fields.password, 15)
|
||||
});
|
||||
res.json(user);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createSingleUser,
|
||||
getSingleUser
|
||||
};
|
||||
78
controllers/volunteer.controller.js
Normal file
78
controllers/volunteer.controller.js
Normal file
@@ -0,0 +1,78 @@
|
||||
var { Volunteer, Asset } = require("../models/models");
|
||||
|
||||
async function getSingleVolunteer(req, res, next) {
|
||||
try {
|
||||
let volunteer = await Volunteer.findByPk(parseInt(req.params.id), { include: [ Asset ] });
|
||||
res.json(volunteer);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function getAllVolunteers(req, res, next) {
|
||||
try {
|
||||
let volunteers = await Volunteer.findAll({ include: [ Asset ] });
|
||||
res.json(volunteers);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function createSingleVolunteer(req, res, next) {
|
||||
try {
|
||||
let volunteer = await Volunteer.create({
|
||||
title: req.fields.title,
|
||||
content: req.fields.content,
|
||||
extra: req.fields.extra,
|
||||
assetId: parseInt(req.fields.assetId)
|
||||
});
|
||||
res.json(volunteer);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function updateSingleVolunteer(req, res, next) {
|
||||
try {
|
||||
let volunteer = await Volunteer.findByPk(parseInt(req.params.id), { include: [ Asset ] });
|
||||
|
||||
if (volunteer) {
|
||||
volunteer.title = req.fields.title;
|
||||
volunteer.content = req.fields.content;
|
||||
volunteer.extra = req.fields.extra;
|
||||
volunteer.assetId = parseInt(req.fields.assetId);
|
||||
volunteer.save();
|
||||
res.json(volunteer);
|
||||
} else {
|
||||
res.status(404).end();
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteSingleVolunteer(req, res, next) {
|
||||
try {
|
||||
await Volunteer.destroy({
|
||||
where: {
|
||||
id: parseInt(req.params.id)
|
||||
}
|
||||
});
|
||||
res.end();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).end();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createSingleVolunteer,
|
||||
getSingleVolunteer,
|
||||
getAllVolunteers,
|
||||
updateSingleVolunteer,
|
||||
deleteSingleVolunteer
|
||||
};
|
||||
Reference in New Issue
Block a user