Skip to content
This repository was archived by the owner on Nov 20, 2025. It is now read-only.
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
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,17 @@ app.use('/messages', require('./routes/messages'));
app.use('/account', require('./routes/account'));
app.use('/contacts', require('./routes/contacts'));
app.use('/ideas', require('./routes/ideas'));
app.use('/challenges', require('./routes/challenges'));
// vote for ideas, ...
app.use('/ideas', require('./routes/votes'));
app.use('/comments', require('./routes/votes'));
app.use('/challenges', require('./routes/votes'));

// following are route factories
// they need to know what is the primary object (i.e. idea, comment, etc.)
app.use('/ideas', require('./routes/primary-comments')('idea'));
app.use('/challenges', require('./routes/primary-comments')('challenge'));

app.use('/comments', require('./routes/comments')(''));
app.use('/comments', require('./routes/primary-comments')('comment'));
app.use('/reactions', require('./routes/comments')('comment'));
Expand Down
17 changes: 17 additions & 0 deletions collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ module.exports = {
type: 'document'
},

challenges: {
type: 'document'
},

ideaTags: {
type: 'edge',
from: ['ideas'],
Expand All @@ -91,6 +95,19 @@ module.exports = {
]
},

challengeTags: {
type: 'edge',
from: ['challenges'],
to: ['tags'],
indexes: [
{
type: 'hash',
fields: ['_from', '_to'],
unique: true
}
]
},

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

votes.to should include 'challenges' here in this file.
(i can't comment directly there)

comments: {
type: 'document',
indexes: [
Expand Down
1 change: 0 additions & 1 deletion controllers/authorize.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// Authorize only logged user
function onlyLogged(req, res, next) {
if (req.auth.logged === true) return next();

return res.status(403).json({ errors: ['Not Authorized'] });
// TODO improve the error
}
Expand Down
139 changes: 139 additions & 0 deletions controllers/dit-tags.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
'use strict';

const path = require('path');

const models = require(path.resolve('./models')),
serialize = require(path.resolve('./serializers')).serialize;

/**
* Controller for POST /dits/:id/tags
* Adds a tag to a dit
*/
async function post(req, res, next) {
let ditType;
try {
// gather data from request
const { tagname } = req.body.tag;
const ditId = req.params.id;
const username = req.auth.username;

ditType = req.baseUrl.slice(1,-1);
// save new dit-tag to database
const newDitTag = await models.ditTag.create(ditType, ditId, tagname, { }, username);
// serialize response body
let responseBody;
switch(ditType){
case 'idea': {
responseBody = serialize.ideaTag(newDitTag);
break;
}
case 'challenge': {
responseBody = serialize.challengeTag(newDitTag);
break;
}
}

// respond
return res.status(201).json(responseBody);
} catch (e) {
// handle errors
switch (e.code) {
// duplicate dit-tag
case 409: {
return res.status(409).end();
}
// missing dit or tag or creator
case 404: {
const errors = e.missing.map(miss => ({ status: 404, detail: `${miss} not found`}));
return res.status(404).json({ errors });
}
// dit creator is not me
case 403: {
return res.status(403).json({ errors: [
{ status: 403, detail: `not logged in as ${ditType} creator` }
]});
}
// unexpected error
default: {
return next(e);
}
}

}
}

/**
* Read list of tags of dit
* GET /dits/:id/tags
*/
async function get(req, res, next) {
let ditType;
try {
// read dit id
const { id } = req.params;
ditType = req.baseUrl.slice(1, -1);

// read ditTags from database
const newDitTags = await models.ditTag.readTagsOfDit(ditType, id);

// serialize response body
let responseBody;
switch(ditType){
case 'idea': {
responseBody = serialize.ideaTag(newDitTags);
break;
}
case 'challenge': {
responseBody = serialize.challengeTag(newDitTags);
break;
}
}

// respond
return res.status(200).json(responseBody);
} catch (e) {
// error when idea doesn't exist
if (e.code === 404) {
return res.status(404).json({ errors: [{
status: 404,
detail: `${ditType} not found`
}] });
}

// handle unexpected error
return next(e);
}
}

/**
* Remove tag from idea
* DELETE /dits/:id/tags/:tagname
*/
async function del(req, res, next) {
let ditType;
try {
const { id, tagname } = req.params;
const { username } = req.auth;
ditType = req.baseUrl.slice(1, -1);

await models.ditTag.remove(ditType, id, tagname, username);

return res.status(204).end();
} catch (e) {
switch (e.code) {
case 404: {
return res.status(404).end();
}
case 403: {
return res.status(403).json({ errors: [
{ status: 403, detail: `not logged in as ${ditType} creator` }
] });
}
default: {
return next(e);
}
}
}
}

module.exports = { post, get, del };
Loading