-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
900 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
const models = require('../models'); | ||
const msg = require('../utils/messages'); | ||
|
||
exports.create = async (req, res) => { | ||
try { | ||
const { | ||
dimensionId, | ||
subdivisionId, | ||
needsAndChallenges, | ||
proposal, | ||
inWords | ||
} = req.body; | ||
|
||
// create a new Challenge entry | ||
const blogEntry = await models.Challenge.create({ | ||
dimensionId, | ||
subdivisionId, | ||
needsAndChallenges, | ||
proposal, | ||
inWords | ||
}); | ||
|
||
return res.status(201).json(blogEntry); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ message: msg.error.default }); | ||
} | ||
} | ||
|
||
exports.stats = async (req, res) => { | ||
try { | ||
const cityId = parseInt(req.query.cityId) || null; | ||
// count how many challenges per dimension | ||
const countChallengesPerDimension = await models.Challenge.count({ | ||
attributes: ['dimensionId'], | ||
include: [ | ||
{ | ||
model: models.Subdivision, | ||
as: 'subdivision', | ||
where: cityId ? { cityId: cityId } : {} | ||
} | ||
], | ||
group: ['dimensionId'] | ||
}); | ||
|
||
// count how many challenges per city | ||
const countChallengesInCity = await models.Challenge.count({ | ||
attributes: ['subdivision.cityId'], | ||
include: [ | ||
{ | ||
model: models.Subdivision, | ||
as: 'subdivision', | ||
where: cityId ? { cityId: cityId } : {} | ||
} | ||
], | ||
group: ['subdivision.cityId'], | ||
}); | ||
|
||
// { | ||
// "countChallengesPerDimension": [ | ||
// { | ||
// "dimensionId": 1, | ||
// "count": 2 | ||
// }, | ||
// { | ||
// "dimensionId": 4, | ||
// "count": 2 | ||
// }, | ||
// { | ||
// "dimensionId": 2, | ||
// "count": 1 | ||
// }, | ||
// { | ||
// "dimensionId": 3, | ||
// "count": 1 | ||
// }, | ||
// { | ||
// "dimensionId": 6, | ||
// "count": 1 | ||
// }, | ||
// { | ||
// "dimensionId": 5, | ||
// "count": 1 | ||
// }, | ||
// { | ||
// "dimensionId": 7, | ||
// "count": 1 | ||
// } | ||
// ], | ||
// "countChallengesInCity": [ | ||
// { | ||
// "cityId": 1, | ||
// "count": 5 | ||
// }, | ||
// { | ||
// "cityId": 2, | ||
// "count": 4 | ||
// } | ||
// ] | ||
// } | ||
|
||
return res.status(200).json({ | ||
countChallengesPerDimension, | ||
countChallengesInCity | ||
}); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ message: msg.error.default }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
const models = require('../models'); | ||
|
||
exports.getAll = async (req, res) => { | ||
try { | ||
const faqs = await models.Faq.findAll({ | ||
order: [['order', 'ASC']] | ||
}); | ||
return res.status(200).json(faqs); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ message: 'An error occurred' }); | ||
} | ||
} | ||
|
||
exports.getOne = async (req, res) => { | ||
try { | ||
const id = req.params.id; | ||
const faq = await models.Faq.findOne({ where: { id } }); | ||
return res.status(200).json(faq); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ message: 'An error occurred' }); | ||
} | ||
} | ||
|
||
exports.create = async (req, res) => { | ||
try { | ||
const { order, question, answer } = req.body; | ||
const faq = await models.Faq.create({ order, question, answer }); | ||
return res.status(201).json(faq); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ message: 'An error occurred' }); | ||
} | ||
} | ||
|
||
exports.edit = async (req, res) => { | ||
try { | ||
const id = req.params.id; | ||
const { order, question, answer } = req.body; | ||
const faq = await models.Faq.findOne({ where: { id } }); | ||
if (!faq) { | ||
return res.status(404).json({ message: 'FAQ not found' }); | ||
} | ||
faq.order = order; | ||
faq.question = question; | ||
faq.answer = answer; | ||
await faq.save(); | ||
return res.status(200).json(faq); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ message: 'An error occurred' }); | ||
} | ||
} | ||
|
||
exports.delete = async (req, res) => { | ||
try { | ||
const id = req.params.id; | ||
const faq = await models.Faq.findOne({ where: { id } }); | ||
if (!faq) { | ||
return res.status(404).json({ message: 'FAQ not found' }); | ||
} | ||
await faq.destroy(); | ||
return res.status(200).json({ message: 'FAQ deleted' }); | ||
} catch (error) { | ||
console.error(error); | ||
return res.status(500).json({ message: 'An error occurred' }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const models = require('../models'); | ||
|
||
exports.getConfigs = async (keys) => { | ||
try { | ||
const configs = await models.Config.findAll({ | ||
where: keys ? { key: keys } : null, | ||
}); | ||
|
||
// for every config, check the type and parse the value | ||
configs.forEach(config => { | ||
switch (config.type) { | ||
case 'string': | ||
config.value = config.value; | ||
break; | ||
case 'number': | ||
config.value = parseInt(config.value, 10); | ||
break; | ||
case 'boolean': | ||
config.value = config.value === 'true'; | ||
break; | ||
case 'json': | ||
config.value = JSON.parse(config.value); | ||
break; | ||
default: | ||
config.value = config.value; | ||
break; | ||
} | ||
}); | ||
|
||
const formattedConfigs = {}; | ||
configs.forEach(config => { | ||
formattedConfigs[config.key] = config.value; | ||
}); | ||
|
||
return formattedConfigs; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} | ||
|
||
exports.saveConfig = async (key, value) => { | ||
try { | ||
const config = await models.Config.findOne({ | ||
where: { key: key } | ||
}); | ||
|
||
if (!config) { | ||
throw new Error('Config not found'); | ||
} | ||
|
||
// make sure to save the value as a string | ||
config.value = value.toString(); | ||
await config.save(); | ||
|
||
return config; | ||
} catch (error) { | ||
throw error; | ||
} | ||
} |
Oops, something went wrong.