katarinasvedman-ms / agent-test

Testing the AI dev team repo
0 stars 0 forks source link

Developer.Implement chain for #128 #137

Open agent-app-kapeltol[bot] opened 3 months ago

agent-app-kapeltol[bot] commented 3 months ago

Write the code for the API endpoint that allows clients to add a new task to the todo list.

agent-app-kapeltol[bot] commented 3 months ago

Let's create a simple Express.js API endpoint in Node.js to accomplish this task.

Here the bash script:

#!/bin/bash

# Create a new directory for our project
mkdir todoApp && cd todoApp

# Create a new Node.js project
echo '{"name": "todoApp", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC"}' > package.json

# Install Express.js
npm install express body-parser

# Create the main server file
touch index.js

# Write the API endpoint to the server file
echo "const express = require('express');
const bodyParser = require('body-parser');

const app = express();
const tasks = [];

app.use(bodyParser.json());

// POST endpoint to add a new task to the todo list
app.post('/task', (req, res) => {
  // Error handling: Check if task data was provided in the request
  if (!req.body || !req.body.task) {
    return res.status(400).json({ error: 'Task data was not provided in the request.' });
  }

  // Add the new task to the list
  tasks.push(req.body.task);

  // Return the updated task list
  res.json(tasks);
});

// Start the server
app.listen(3000, () => console.log('Todo app listening on port 3000!'));

" > index.js

# Start the server
node index.js

In this script, I'm creating a new Node.js/Express.js project and installing necessary dependencies. Then, I'm writing a simple POST endpoint that allows clients to add a new task to the todo list. The task data should be sent in the 'task' field of the request body in JSON format. If the task data is not provided, the API will return a 400 error.

Run this script on a machine with Node.js and npm installed, and it will create the new project, install the dependencies, create the API endpoint, and start the server.