From 58b62f64dcdf65dc4104165e9f0e313f57e696cb Mon Sep 17 00:00:00 2001 From: Roland Bernard <rolbernard@unibz.it> Date: Tue, 4 May 2021 13:33:19 +0200 Subject: [PATCH] Cleand up some API requests --- server/src/v1/comment.ts | 4 ++-- server/src/v1/project.ts | 22 ++++++++++++---------- server/src/v1/task.ts | 26 ++++++++++++++------------ server/src/v1/user.ts | 36 ++++++++++++++++++++---------------- server/src/v1/work.ts | 8 +++++++- 5 files changed, 55 insertions(+), 41 deletions(-) diff --git a/server/src/v1/comment.ts b/server/src/v1/comment.ts index f7739b9..66fef84 100644 --- a/server/src/v1/comment.ts +++ b/server/src/v1/comment.ts @@ -70,12 +70,12 @@ comment.post('/', async (req, res) => { }); interface UpdateCommentBody { - text?: string; + text: string; token: Token; } comment.put('/:uuid', async (req, res) => { - if (isOfType<UpdateCommentBody>(req.body, [])) { + if (isOfType<UpdateCommentBody>(req.body, [['text', 'string']])) { try { const comment_id = req.params.uuid; if (validate(comment_id)) { diff --git a/server/src/v1/project.ts b/server/src/v1/project.ts index 77240ab..e93d40e 100644 --- a/server/src/v1/project.ts +++ b/server/src/v1/project.ts @@ -338,16 +338,18 @@ project.put('/:uuid', async (req, res) => { }); if (projects.length >= 1) { await database.transaction(async transaction => { - await transaction('projects') - .update({ - name: req.body.name, - text: req.body.text, - color: req.body.color, - status: req.body.status, - deadline: req.body.deadline, - }).where({ - id: id, - }); + if (req.body.name || req.body.text || req.body.color || req.body.status || req.body.deadline) { + await transaction('projects') + .update({ + name: req.body.name, + text: req.body.text, + color: req.body.color, + status: req.body.status, + deadline: req.body.deadline, + }).where({ + id: id, + }); + } if (remove_team_ids.length !== 0) { await transaction('team_projects') .delete() diff --git a/server/src/v1/task.ts b/server/src/v1/task.ts index 3662b6e..e00c3ce 100644 --- a/server/src/v1/task.ts +++ b/server/src/v1/task.ts @@ -546,18 +546,20 @@ task.put('/:uuid', async (req, res) => { }); if (task.length >= 1) { await database.transaction(async transaction => { - await transaction('tasks') - .update({ - name: req.body.name, - text: req.body.text, - icon: req.body.icon, - priority: req.body.priority, - status: req.body.status, - edited: new Date(), - }) - .where({ - 'tasks.id': task_id, - }); + if (req.body.name || req.body.text || req.body.icon || req.body.priority || req.body.status) { + await transaction('tasks') + .update({ + name: req.body.name, + text: req.body.text, + icon: req.body.icon, + priority: req.body.priority, + status: req.body.status, + edited: new Date(), + }) + .where({ + 'tasks.id': task_id, + }); + } if (remove_requirement_ids.length !== 0) { await transaction('task_requirements') .delete() diff --git a/server/src/v1/user.ts b/server/src/v1/user.ts index d4ffdbb..2fa51c7 100644 --- a/server/src/v1/user.ts +++ b/server/src/v1/user.ts @@ -145,25 +145,29 @@ interface UserUpdateBody { } user.put('/', async (req, res) => { - if ( - isOfType<UserUpdateBody>(req.body, [['realname', 'string']]) - || isOfType<UserUpdateBody>(req.body, [['email', 'string']]) - ) { + if (isOfType<UserUpdateBody>(req.body, [])) { try { - const updated = await database('users') - .update({ - email: req.body.email, - real_name: req.body.realname, - }) - .where({ id: req.body.token.id }); - if (updated >= 1) { + if (req.body.realname || req.body.email) { + const updated = await database('users') + .update({ + email: req.body.email, + real_name: req.body.realname, + }) + .where({ id: req.body.token.id }); + if (updated >= 1) { + res.status(200).json({ + status: 'success', + }); + } else { + res.status(404).json({ + status: 'error', + message: 'user not found', + }); + } + } else { res.status(200).json({ status: 'success', - }); - } else { - res.status(404).json({ - status: 'error', - message: 'user not found', + message: 'nothing to do', }); } } catch (e) { diff --git a/server/src/v1/work.ts b/server/src/v1/work.ts index 680e3e7..65e3223 100644 --- a/server/src/v1/work.ts +++ b/server/src/v1/work.ts @@ -109,7 +109,13 @@ work.put('/finish', async (req, res) => { work.get('/', async (req, res) => { try { const work = await database('workhours') - .select() + .select({ + id: 'workhours.id', + task: 'workhours.task_id', + user: 'workhours.user_id', + started: 'workhours.started', + finished: 'workhours.finished', + }) .where({ user_id: req.body.token.id, finished: null, -- GitLab