From 30fac3ff13a51ce166f2825bef2ab504a87a3049 Mon Sep 17 00:00:00 2001 From: Ben Date: Tue, 25 Sep 2018 13:59:58 +0100 Subject: [PATCH] Worked on the note endpoint and helpers, ready for response and testing --- src/controllers/noteController.js | 19 +++++++++++++++++-- src/models/api/API.js | 1 + src/models/api/noteResponse.js | 5 +++++ src/models/notes/groups.js | 10 +++++++++- src/models/notes/notes.js | 10 +++++----- 5 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 src/models/api/noteResponse.js diff --git a/src/controllers/noteController.js b/src/controllers/noteController.js index 1f5254b..b01d910 100644 --- a/src/controllers/noteController.js +++ b/src/controllers/noteController.js @@ -11,7 +11,7 @@ export class NoteController extends ControllerHandler { let content = req.body.text || null; let creatorid = req.user.id || undefined; - let group = req.body.parentgroup || null; + let group = req.body.parentgroup || undefined; let order = req.body.order || undefined; let user = req.user || undefined; @@ -32,10 +32,25 @@ export class NoteController extends ControllerHandler { let id = await Notes.genID(); - Notes.newNote(id, content, creatorid, order); + let success; + + if (!group) { + await Notes.newNote(id, content, creatorid, order); + } else { + let doesExist = await Notes.doesGroupExist(); + if (!doesExist) { + errors.addError(422, 'Unprocessable entity', 'The group you are trying to create this note in does not exist'); + errors.endpoint(); + next(); + return; + } + await Notes.newGroupedNote(id, content, creatorid, order, parentgroup); + } next(); } + + static async } // id: id, diff --git a/src/models/api/API.js b/src/models/api/API.js index 1f68ead..e856b48 100644 --- a/src/models/api/API.js +++ b/src/models/api/API.js @@ -9,3 +9,4 @@ export class API extends BaseAPI { API.errors = require('./APIErrors').APIErrors; API.user = require('./userResponses').UserAPI; API.permalink = require('./permaLinkResponse').PermaLinkAPI; +API.note = require('./noteResponse').NoteAPI; diff --git a/src/models/api/noteResponse.js b/src/models/api/noteResponse.js new file mode 100644 index 0000000..34e1367 --- /dev/null +++ b/src/models/api/noteResponse.js @@ -0,0 +1,5 @@ +import {API} from './API'; + +export class NoteAPI extends API { + +} diff --git a/src/models/notes/groups.js b/src/models/notes/groups.js index 4369ccf..ec95a6f 100644 --- a/src/models/notes/groups.js +++ b/src/models/notes/groups.js @@ -1,3 +1,11 @@ +import {Database} from '../database/database'; + export class Groups { - constructor() { } + static async newGroup() { + + } + + static async doesGroupExist() { + + } } diff --git a/src/models/notes/notes.js b/src/models/notes/notes.js index 6f6284d..6ff29e4 100644 --- a/src/models/notes/notes.js +++ b/src/models/notes/notes.js @@ -3,7 +3,11 @@ import {Database} from '../database/database'; export class Notes extends Groups { static async newNote(id, content, creatorid, order) { - Database.note.newNote(id, content, creatorid, order, null); + return await Database.note.newNote(id, content, creatorid, order, null); + } + + static async newGroupedNote(id, content, creatorid, order, parentgroup) { + return await Database.note.newNote(id, content, creatorid, order, parentgroup); } static async genID() { @@ -34,10 +38,6 @@ export class Notes extends Groups { } - static async newGroupedNote(id, content, creatorid, order, parentgroup) { - - } - static async reorderNote(id, newPosition) { }