mirror of
https://github.com/KevinMidboe/leifsbackend.git
synced 2026-01-13 04:45:31 +00:00
Working backend setup with endpoints and database setup for adventures.
This commit is contained in:
67
src/controllers/adventure.js
Normal file
67
src/controllers/adventure.js
Normal file
@@ -0,0 +1,67 @@
|
||||
const Adventure = require('../db/models').adventure;
|
||||
const Location = require('../db/models').location;
|
||||
const Image = require('../db/models').image;
|
||||
|
||||
// const { Adventure, Image, Location } = require('../db/sequelize')
|
||||
|
||||
const Op = require('sequelize').Op;
|
||||
|
||||
// module.exports = {
|
||||
|
||||
function location(req, res) {
|
||||
return Adventure
|
||||
.findAll({
|
||||
where: {
|
||||
'locationName': req.query.name
|
||||
},
|
||||
attributes: ['id', 'title', 'subtext', 'dateStart', 'dateEnd', 'locationName']
|
||||
// order: [['title', 'ASC']]
|
||||
})
|
||||
.then(adventure => res.status(200).send(adventure))
|
||||
.catch(error => res.status(400).send(error))
|
||||
}
|
||||
|
||||
function list(req, res) {
|
||||
return Adventure
|
||||
.findAll({
|
||||
attributes: ['id', 'title', 'subtext', 'dateStart', 'dateEnd', 'locationName']
|
||||
})
|
||||
.then(adventure => res.status(200).send(adventure))
|
||||
.catch(error => res.status(400).send(error))
|
||||
}
|
||||
|
||||
function get(req, res) {
|
||||
return Adventure
|
||||
.findByPk(req.params.id, {
|
||||
attributes: ['id', 'title', 'subtext', 'dateStart', 'dateEnd', 'locationName']
|
||||
})
|
||||
.then(adventure => res.status(200).send(adventure))
|
||||
.catch(error => res.status(400).send(error));
|
||||
}
|
||||
|
||||
function createLocation(req, res) {
|
||||
return Location
|
||||
.create({
|
||||
name: req.body.locationName,
|
||||
geoposition: req.body.geoposition
|
||||
})
|
||||
}
|
||||
|
||||
// get form elements
|
||||
function create(req, res) {
|
||||
console.log('adventure', Adventure)
|
||||
return createLocation(req, res)
|
||||
.then(() => Adventure
|
||||
.create({
|
||||
title: req.body.title,
|
||||
subtext: req.body.subtext,
|
||||
dateStart: req.body.dateStart,
|
||||
dateEnd: req.body.dateEnd,
|
||||
locationName: req.body.locationName,
|
||||
}, {}))
|
||||
.then(adventure => res.status(201).send(adventure))
|
||||
.catch(error => res.status(400).send(error))
|
||||
}
|
||||
|
||||
|
||||
module.exports = { location, list, get, create };
|
||||
47
src/controllers/calendar.js
Normal file
47
src/controllers/calendar.js
Normal file
@@ -0,0 +1,47 @@
|
||||
const Adventure = require('../db/models').adventure;
|
||||
// const { Adventure } = require('../db/sequelize')
|
||||
|
||||
const Op = require('sequelize').Op;
|
||||
|
||||
module.exports = {
|
||||
|
||||
location(req, res) {
|
||||
return Adventure
|
||||
.findAll({
|
||||
where: { [Op.not]: { locationName: null } }
|
||||
})
|
||||
.then(adventure => res.status(200).send(adventure))
|
||||
.catch(error => res.status(400).send(error))
|
||||
},
|
||||
|
||||
list(req, res) {
|
||||
return Adventure
|
||||
.findAll({
|
||||
attributes: ['id', 'title', 'subtext', 'dateStart', 'dateEnd', 'locationName']
|
||||
})
|
||||
.then(adventure => res.status(200).send(adventure))
|
||||
.catch(error => res.status(400).send(error))
|
||||
},
|
||||
|
||||
get(req, res) {
|
||||
return Adventure
|
||||
.findById(req.params.id)
|
||||
.then(adventure => res.status(200).send(adventure))
|
||||
.catch(error => res.status(400).send(error));
|
||||
},
|
||||
|
||||
// get form elements
|
||||
create(req, res) {
|
||||
console.log('adventure', Adventure)
|
||||
return Adventure
|
||||
.create({
|
||||
title: req.body.title,
|
||||
subtext: req.body.subtext,
|
||||
dateStart: req.body.dateStart,
|
||||
dateEnd: req.body.dateEnd,
|
||||
locationName: req.body.locationName
|
||||
})
|
||||
.then(adventure => res.status(201).send(adventure))
|
||||
.catch(error => res.status(400).send(error))
|
||||
}
|
||||
};
|
||||
23
src/controllers/image.js
Normal file
23
src/controllers/image.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const Image = require('../db/models').image;
|
||||
|
||||
// const { Image } = require('../db/sequelize')
|
||||
|
||||
module.exports = {
|
||||
|
||||
all(req, res) {
|
||||
console.log('here')
|
||||
return Image
|
||||
.findAll()
|
||||
.then(images => res.status(200).send(images))
|
||||
.catch(error => res.status(400).send(error))
|
||||
},
|
||||
|
||||
list(req, res) {
|
||||
return Image
|
||||
.findAll({
|
||||
where: { 'adventure_id': req.params.adventureId }
|
||||
})
|
||||
.then(images => res.status(200).send(images))
|
||||
.catch(error => res.status(400).send(error))
|
||||
},
|
||||
};
|
||||
9
src/controllers/index.js
Normal file
9
src/controllers/index.js
Normal file
@@ -0,0 +1,9 @@
|
||||
const Adventure = require('./adventure');
|
||||
const Location = require('./location');
|
||||
const Image = require('./image');
|
||||
|
||||
module.exports = {
|
||||
Adventure,
|
||||
Location,
|
||||
Image
|
||||
}
|
||||
22
src/controllers/location.js
Normal file
22
src/controllers/location.js
Normal file
@@ -0,0 +1,22 @@
|
||||
const Location = require('../db/models').location;
|
||||
const Adventure = require('../db/models').adventure;
|
||||
|
||||
// const { Adventure, Location } = require('../db/sequelize')
|
||||
|
||||
const Op = require('sequelize').Op;
|
||||
|
||||
module.exports = {
|
||||
|
||||
list(req, res) {
|
||||
return Location
|
||||
.findAll({
|
||||
attributes: ['name', 'geoposition', 'mapboxData']
|
||||
})
|
||||
// .then(Location => Adventure.findAll({
|
||||
// where: { 'locationName': Location.name },
|
||||
// attributes: [ 'title', 'subtext', 'dateStart', 'dateEnd', 'location.geoposition' ]
|
||||
// }))
|
||||
.then(Location => res.status(200).send(Location))
|
||||
.catch(error => res.status(400).send(error))
|
||||
},
|
||||
};
|
||||
Reference in New Issue
Block a user