-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from Dedakup/feature/tasks-ud
feat(tasks. commit before refactoring): Created functions for editing…
- Loading branch information
Showing
11 changed files
with
1,433 additions
and
202 deletions.
There are no files selected for viewing
This file contains 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 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 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 |
---|---|---|
@@ -1,17 +1,58 @@ | ||
const db = require('../../utils/db'); | ||
const db = require("../../utils/db"); | ||
|
||
module.exports.deleteTask = async (event) => { | ||
const { taskId } = event.pathParameters; | ||
//get task id from url | ||
const taskId = event.pathParameters?.taskId; | ||
|
||
//validate task id | ||
if (!taskId) { | ||
return { | ||
statusCode: 400, | ||
headers: { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Credentials": true, | ||
}, | ||
body: JSON.stringify({ error: "Missing path parameter: taskId" }), | ||
}; | ||
} | ||
|
||
const userId = event.queryStringParameters?.userId; | ||
|
||
if (!userId) { | ||
return { | ||
statusCode: 400, | ||
headers: { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Credentials": true, | ||
}, | ||
body: JSON.stringify({ error: "Missing query parameter: userId" }), | ||
}; | ||
} | ||
|
||
const params = { | ||
TableName: process.env.DYNAMODB_TASKS_TABLE, | ||
Key: { userId: event.requestContext.authorizer.userId, taskId }, | ||
}; | ||
|
||
await db.delete(params); | ||
Key: { userId, taskId }, | ||
} | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: 'Task deleted successfully' }), | ||
}; | ||
try { | ||
await db.delete(params); | ||
return { | ||
statusCode: 200, | ||
headers: { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Credentials": true, | ||
}, | ||
body: JSON.stringify({ message: "Task deleted successfully", taskId }), | ||
}; | ||
} catch (error) { | ||
console.error("DynamoDB Delete Error:", error); | ||
return { | ||
statusCode: 500, | ||
headers: { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Credentials": true, | ||
}, | ||
body: JSON.stringify({ error: error.message }), | ||
}; | ||
} | ||
}; |
This file contains 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 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 |
---|---|---|
@@ -1,20 +1,70 @@ | ||
const db = require('../../utils/db'); | ||
|
||
module.exports.updateTask = async (event) => { | ||
const { taskId } = event.pathParameters; | ||
const { title, description, dueDate, status } = JSON.parse(event.body); | ||
const { taskId, userId, ...updatedFields } = JSON.parse(event.body); | ||
|
||
if (!taskId || !userId || Object.keys(updatedFields).length === 0) { | ||
return { | ||
statusCode: 400, | ||
headers: { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Credentials": true, | ||
}, | ||
body: JSON.stringify({ | ||
error: "Missing required parameters or body", | ||
}), | ||
}; | ||
} | ||
|
||
const updateExpressions = []; | ||
const expressionAttributeNames = {}; | ||
const expressionAttributeValues = {}; | ||
|
||
Object.keys(updatedFields).forEach((field, index) => { | ||
const attributeName = `#field${index}`; | ||
const attributeValue = `:value${index}`; | ||
|
||
updateExpressions.push(`${attributeName} = ${attributeValue}`); | ||
expressionAttributeNames[attributeName] = field; | ||
expressionAttributeValues[attributeValue] = updatedFields[field]; | ||
}); | ||
|
||
const params = { | ||
TableName: process.env.DYNAMODB_TASKS_TABLE, | ||
Key: { userId: event.requestContext.authorizer.userId, taskId }, | ||
UpdateExpression: 'set title = :title, description = :description, dueDate = :dueDate, status = :status', | ||
ExpressionAttributeValues: { ':title': title, ':description': description, ':dueDate': dueDate, ':status': status }, | ||
Key: { | ||
userId, | ||
taskId, | ||
}, | ||
UpdateExpression: `SET ${updateExpressions.join(', ')}`, | ||
ExpressionAttributeNames: expressionAttributeNames, | ||
ExpressionAttributeValues: expressionAttributeValues, | ||
ReturnValues: "UPDATED_NEW", | ||
}; | ||
|
||
await db.update(params); | ||
|
||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ message: 'Task updated successfully' }), | ||
}; | ||
try { | ||
const result = await db.update(params); | ||
return { | ||
statusCode: 200, | ||
headers: { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Credentials": true, | ||
}, | ||
body: JSON.stringify({ | ||
message: "Task updated successfully", | ||
updatedTask: result.Attributes, | ||
}), | ||
}; | ||
} catch (error) { | ||
console.error("Error updating task:", error); | ||
return { | ||
statusCode: 500, | ||
headers: { | ||
"Access-Control-Allow-Origin": "*", | ||
"Access-Control-Allow-Credentials": true, | ||
}, | ||
body: JSON.stringify({ | ||
error: "Failed to update task", | ||
}), | ||
}; | ||
} | ||
}; |
This file contains 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,54 @@ | ||
// src/models/taskParams.js | ||
require('dotenv').config(); | ||
|
||
const createTaskParams = ({ userId, taskId, title, description, dueDate }) => ({ | ||
TableName: process.env.DYNAMODB_TASKS_TABLE, | ||
Item: { | ||
userId, | ||
taskId, | ||
title, | ||
description: description || null, | ||
dueDate: dueDate || null, | ||
status: "pending", | ||
createdAt: new Date().toISOString(), | ||
}, | ||
}); | ||
|
||
const getTasksParams = ({ userId }) => ({ | ||
TableName: process.env.DYNAMODB_TASKS_TABLE, | ||
KeyConditionExpression: "userId = :userId", | ||
ExpressionAttributeValues: { | ||
":userId": userId, | ||
}, | ||
}); | ||
|
||
const updateTaskParams = ({ userId, taskId, updates }) => ({ | ||
TableName: process.env.DYNAMODB_TASKS_TABLE, | ||
Key: { userId, taskId }, | ||
UpdateExpression: "SET #title = :title, #description = :description, #dueDate = :dueDate, #status = :status", | ||
ExpressionAttributeNames: { | ||
"#title": "title", | ||
"#description": "description", | ||
"#dueDate": "dueDate", | ||
"#status": "status", | ||
}, | ||
ExpressionAttributeValues: { | ||
":title": updates.title, | ||
":description": updates.description || null, | ||
":dueDate": updates.dueDate || null, | ||
":status": updates.status || "pending", | ||
}, | ||
ReturnValues: "UPDATED_NEW", | ||
}); | ||
|
||
const deleteTaskParams = ({ userId, taskId }) => ({ | ||
TableName: process.env.DYNAMODB_TASKS_TABLE, | ||
Key: { userId, taskId }, | ||
}); | ||
|
||
module.exports = { | ||
createTaskParams, | ||
getTasksParams, | ||
updateTaskParams, | ||
deleteTaskParams, | ||
}; |
Oops, something went wrong.