Added order by datestart when fecting all Adventures as list. Create now first creates a location and then the adventure. started enpoint for handling uploading of images. These requests will be redirected to another python web server.

This commit is contained in:
2019-03-02 22:42:54 +01:00
parent 036131c1ce
commit 417ec775ff

View File

@@ -2,12 +2,8 @@ const Adventure = require('../db/models').adventure;
const Location = require('../db/models').location; const Location = require('../db/models').location;
const Image = require('../db/models').image; const Image = require('../db/models').image;
// const { Adventure, Image, Location } = require('../db/sequelize')
const Op = require('sequelize').Op; const Op = require('sequelize').Op;
// module.exports = {
function location(req, res) { function location(req, res) {
return Adventure return Adventure
.findAll({ .findAll({
@@ -15,7 +11,6 @@ function location(req, res) {
'locationName': req.query.name 'locationName': req.query.name
}, },
attributes: ['id', 'title', 'subtext', 'dateStart', 'dateEnd', 'locationName'] attributes: ['id', 'title', 'subtext', 'dateStart', 'dateEnd', 'locationName']
// order: [['title', 'ASC']]
}) })
.then(adventure => res.status(200).send(adventure)) .then(adventure => res.status(200).send(adventure))
.catch(error => res.status(400).send(error)) .catch(error => res.status(400).send(error))
@@ -24,7 +19,10 @@ function location(req, res) {
function list(req, res) { function list(req, res) {
return Adventure return Adventure
.findAll({ .findAll({
attributes: ['id', 'title', 'subtext', 'dateStart', 'dateEnd', 'locationName'] attributes: ['id', 'title', 'subtext', 'dateStart', 'dateEnd', 'locationName'],
order: [
['dateStart', 'DESC'],
]
}) })
.then(adventure => res.status(200).send(adventure)) .then(adventure => res.status(200).send(adventure))
.catch(error => res.status(400).send(error)) .catch(error => res.status(400).send(error))
@@ -40,28 +38,41 @@ function get(req, res) {
} }
function createLocation(req, res) { function createLocation(req, res) {
const geoposition = `${req.body.geoposition[0]}, ${req.body.geoposition[1]}`
console.log('creatingLocation', geoposition)
return Location return Location
.create({ .create({
name: req.body.locationName, name: req.body.locationName,
geoposition: req.body.geoposition geoposition: geoposition,
mapboxData: req.body.mapboxData
}) })
.catch((err) => console.error(err))
}
function uploadHandler(req, res) {
console.log('req', req.files)
return res.status(200).send('upload successful')
} }
// get form elements // get form elements
function create(req, res) { function create(req, res) {
console.log('adventure', Adventure) console.log('Received req body for create:', req.body)
return createLocation(req, res)
.then(() => Adventure Adventure.create({
.create({ title: req.body.title,
title: req.body.title, subtext: req.body.subtext,
subtext: req.body.subtext, dateStart: req.body.dateStart,
dateStart: req.body.dateStart, dateEnd: req.body.dateEnd,
dateEnd: req.body.dateEnd, locationName: req.body.locationName,
locationName: req.body.locationName, }, {})
}, {})) .then(() => createLocation(req, res))
.then(adventure => res.status(201).send(adventure)) .then(adventure => res.status(201).send(adventure))
.catch(error => res.status(400).send(error)) .catch(error => {
console.log('Error when adding adventure', error)
res.status(400).send('Something went wrong')
})
} }
module.exports = { location, list, get, create }; module.exports = { location, list, get, create, uploadHandler };