Skip to main content
Add a new comment to an existing task. This automatically increments the task’s amount_of_comments counter.

Endpoint

POST /api/addCommentToTask

Request Body

task_id
number
required
The ID of the task to add the comment to.
comment
string
required
The text content of the comment.

Response

success
string
Returns "Successful!" when the comment is added successfully.
error
string
Returns "Invalid!" if the operation fails (e.g., task not found).

Example Request

curl -X POST http://localhost:5173/api/addCommentToTask \
  -H "Content-Type: application/json" \
  -d '{
    "task_id": 42,
    "comment": "Client requested to move the shoot to 2pm"
  }'

Response Example

"Successful!"

Error Response

"Invalid!"

Behavior Notes

The created_at timestamp is automatically set to the current date and time when the comment is created.
Adding a comment increments the task’s amount_of_comments field by 1. This counter is useful for displaying comment counts without querying all comments.
After successfully adding a comment, emit a database-change event via Socket.io to notify other clients. See Real-time Updates for details.

Database Operations

The endpoint performs two Prisma operations:
  1. Create the comment:
await prisma.task_comments.create({
  data: {
    task_id: task_id,
    comment: comment,
    created_at: new Date()
  }
});
  1. Increment the comment counter:
await prisma.tasks.update({
  where: { id: task_id },
  data: {
    amount_of_comments: { increment: 1 }
  }
});

Comment Schema

Comments are stored with the following structure:
model task_comments {
  id         Int      @id @default(autoincrement())
  created_at DateTime @default(now())
  task       Tasks    @relation(fields: [task_id], references: [id], onDelete: Cascade)
  task_id    Int
  comment    String
}

Common Use Cases

Add Status Update

async function addStatusUpdate(taskId, status) {
  const comment = `Status updated: ${status}`;
  await addComment(taskId, comment);
}

addStatusUpdate(42, 'Photos delivered to client');

Add Client Feedback

async function recordClientFeedback(taskId, feedback) {
  const timestamp = new Date().toLocaleString();
  const comment = `[${timestamp}] Client feedback: ${feedback}`;
  await addComment(taskId, comment);
}

recordClientFeedback(42, 'Loves the outdoor shots, wants more close-ups');

Add Multiple Comments

async function addBulkComments(taskId, comments) {
  const results = await Promise.all(
    comments.map(text => addComment(taskId, text))
  );
  
  const successCount = results.filter(r => r === 'Successful!').length;
  console.log(`${successCount} of ${comments.length} comments added`);
}

addBulkComments(42, [
  'Initial contact made with client',
  'Deposit received',
  'Location scouted'
]);

Comment Display

To retrieve and display comments, you’ll need to query the database directly or extend the API. Comments are related to tasks via the task_id foreign key:
// Example: Get task with all comments
const taskWithComments = await prisma.tasks.findUnique({
  where: { id: taskId },
  include: {
    task_comments: {
      orderBy: { created_at: 'desc' }
    }
  }
});

Source Reference

Implementation: src/routes/api/(comments)/addCommentToTask/+server.ts:4

Build docs developers (and LLMs) love