This repository was archived by the owner on Nov 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
[WIP] challenges and dits #66
Open
agatatalita
wants to merge
6
commits into
master
Choose a base branch
from
challenges-dits
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2ae5bcd
write common tests for ideas and challenges CRUD and make them work
agatatalita a58f580
fix tests - missing ditType data parameter
agatatalita 0bf9488
fix tests - missing ditType data parameter
agatatalita 1258cb9
write common tests for ideas and challenges lists functionalities and…
agatatalita ea9dc63
add comments tests for challenges
agatatalita c8b2146
create tests for dit-tags and make them pass
agatatalita File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,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 }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
votes.toshould include'challenges'here in this file.(i can't comment directly there)