Skip to content

Commit 700bda3

Browse files
committed
Add Patch method
1 parent 66dbbda commit 700bda3

File tree

4 files changed

+87
-10
lines changed

4 files changed

+87
-10
lines changed

controllers/tasksController.js

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ const taskQuery = require('../models/tasks')
88
*/
99
const addNewTask = async (req, res) => {
1010
try {
11-
const task = await taskQuery.addTask(req.body)
11+
const task = await taskQuery.updateTask(req.body)
1212
return res.json({
1313
message: 'Task created successfully!',
14-
task: req.body,
14+
task: task.taskDetails,
1515
id: task.taskId
1616
})
1717
} catch (err) {
@@ -29,7 +29,7 @@ const fetchTasks = async (req, res) => {
2929
try {
3030
const allTasks = await taskQuery.fetchTasks()
3131
if (allTasks.length > 0) {
32-
return res.status(200).json({
32+
return res.json({
3333
message: 'Tasks returned successfully!',
3434
tasks: allTasks
3535
})
@@ -41,7 +41,29 @@ const fetchTasks = async (req, res) => {
4141
}
4242
}
4343

44+
/**
45+
* Updates the task
46+
*
47+
* @param req {Object} - Express request object
48+
* @param res {Object} - Express response object
49+
*/
50+
const updateTask = async (req, res) => {
51+
try {
52+
const task = await taskQuery.fetchTask(req.params.id)
53+
if (!task.taskData) {
54+
return res.boom.notFound('Task not found')
55+
}
56+
57+
await taskQuery.updateTask(req.body, req.params.id)
58+
return res.status(204).send()
59+
} catch (err) {
60+
logger.error(`Error while updating user: ${err}`)
61+
return res.boom.serverUnavailable('Something went wrong please contact admin')
62+
}
63+
}
64+
4465
module.exports = {
4566
addNewTask,
46-
fetchTasks
67+
fetchTasks,
68+
updateTask
4769
}

models/tasks.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,25 @@ const firestore = require('../utils/firestore')
22
const tasksModel = firestore.collection('tasks')
33

44
/**
5-
* Adds new task
5+
* Adds and Updates tasks
66
*
77
* @param taskData { Object }: task data object to be stored in DB
8+
* @param taskId { string }: taskid which will be used to update the task in DB
89
* @return {Promise<{taskId: string}>}
910
*/
10-
const addTask = async (taskData) => {
11+
const updateTask = async (taskData, taskId = null) => {
1112
try {
13+
if (taskId) {
14+
const task = await tasksModel.doc(taskId).get()
15+
await tasksModel.doc(taskId).set({
16+
...task.data(),
17+
...taskData
18+
})
19+
return { taskId }
20+
}
1221
const taskInfo = await tasksModel.add(taskData)
13-
return { taskId: taskInfo.id }
22+
const newlyCreatedTaskData = await fetchTask(taskInfo.id)
23+
return { taskId: taskInfo.id, taskDetails: newlyCreatedTaskData.taskData }
1424
} catch (err) {
1525
logger.error('Error in creating task', err)
1626
throw err
@@ -39,7 +49,23 @@ const fetchTasks = async () => {
3949
}
4050
}
4151

52+
/**
53+
* Fetch a task
54+
* @param taskId { string }: taskid which will be used to fetch the task
55+
* @return {Promise<taskData|Object>}
56+
*/
57+
const fetchTask = async (taskId) => {
58+
try {
59+
const task = await tasksModel.doc(taskId).get()
60+
return { taskData: task.data() }
61+
} catch (err) {
62+
logger.error('Error retrieving task data', err)
63+
throw err
64+
}
65+
}
66+
4267
module.exports = {
43-
addTask,
44-
fetchTasks
68+
updateTask,
69+
fetchTasks,
70+
fetchTask
4571
}

routes/tasks.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const tasksController = require('../controllers/tasksController')
55
/**
66
* @swagger
77
* /tasks:
8+
* get:
89
* summary: Used to get all the tasks
910
* responses:
1011
* 200:
@@ -32,6 +33,7 @@ router.get('/', tasksController.fetchTasks)
3233
/**
3334
* @swagger
3435
* /tasks:
36+
* post:
3537
* summary: Used to create new task
3638
* responses:
3739
* 200:
@@ -49,4 +51,27 @@ router.get('/', tasksController.fetchTasks)
4951
*/
5052
router.post('/', tasksController.addNewTask)
5153

54+
/**
55+
* @swagger
56+
* /tasks:
57+
* patch:
58+
* summary: Used to update task details
59+
* responses:
60+
* 204:
61+
* description: no content
62+
* 404:
63+
* description: notFound
64+
* content:
65+
* application/json:
66+
* schema:
67+
* $ref: '#/components/schemas/errors/notFound'
68+
* 500:
69+
* description: badImplementation
70+
* content:
71+
* application/json:
72+
* schema:
73+
* $ref: '#/components/schemas/errors/badImplementation'
74+
*/
75+
router.patch('/:id', tasksController.updateTask)
76+
5277
module.exports = router

utils/swaggerDefinition.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ const swaggerOptions = {
6161
},
6262
links: {
6363
type: 'array',
64-
items: []
64+
items: {
65+
link1: {
66+
type: 'string'
67+
}
68+
}
6569
},
6670
endsOn: {
6771
type: 'string'

0 commit comments

Comments
 (0)