Add task list demo application

This commit is contained in:
Steve Kinney
2024-09-17 15:38:26 -06:00
parent b13029bffc
commit 4971bda81b
40 changed files with 2545 additions and 773 deletions

View File

@@ -0,0 +1,65 @@
import express from 'express';
import bodyParser from 'body-parser';
import chalk from 'chalk';
import {
getTasks,
getTask,
createTask,
updateTask,
deleteTask,
} from './tasks.js';
const app = express();
app.use(bodyParser.json());
app.get('/api/tasks', (req, res) => {
const tasks = getTasks();
res.json(tasks);
});
app.post('/api/tasks', (req, res) => {
const { title } = req.body;
if (!title) {
return res.status(400).json({ message: 'A title is required' });
}
const task = createTask(title);
res.status(201).json(task);
});
app.get('/api/tasks/:id', (req, res) => {
const task = getTask(req.params.id);
if (!task) {
return res.status(404).json({ message: 'Task not found' });
}
res.json(task);
});
app.patch('/api/tasks/:id', (req, res) => {
const { title, completed } = req.body;
const task = updateTask(req.params.id, { title, completed });
if (!task) {
return res.status(404).json({ message: 'Task not found' });
}
res.sendStatus(204);
});
app.delete('/api/tasks/:id', (req, res) => {
const task = deleteTask(req.params.id);
if (!task) {
return res.status(404).json({ message: 'Task not found' });
}
res.status(204).send(); // No content to send back
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(chalk.magenta(`Server is running on port ${chalk.green(PORT)}`));
});

View File

@@ -0,0 +1,65 @@
import { v4 as id } from 'uuid';
/** @typedef {import('../types').Task} Task */
/** @type {Task[]} tasks - An array to store tasks. */
let tasks = [];
/**
* Get all tasks.
* @returns {Task[]} An array of tasks.
*/
export const getTasks = () => tasks;
/**
* Create a new task.
* @param {string} title - The title of the task.
* @returns {Task} The newly created task.
*/
export const createTask = (title) => {
/** @type {Task} */
const task = {
id: id(),
title,
completed: false,
createdAt: new Date(),
lastModified: new Date(),
};
tasks.push(task);
return task;
};
/**
* Find a task by ID.
* @param {string} id - The ID of the task to find.
* @returns {Task | undefined} The found task or undefined if not found.
*/
export const getTask = (id) => tasks.find((task) => task.id === id);
/**
* Update a task by ID.
* @param {string} id - The ID of the task to update.
* @param {Partial<Pick<Task, 'title' | 'description'>>} updates - The updates to apply to the task.
* @returns {Task | undefined} The updated task or undefined if not found.
*/
export const updateTask = (id, updates) => {
const task = getTask(id);
if (!task) return undefined;
Object.assign(task, updates, { lastModified: new Date() });
return task;
};
/**
* Delete a task by ID.
* @param {string} id - The ID of the task to delete.
* @returns {boolean} `true` if the task was deleted, `false` if not found.
*/
export const deleteTask = (id) => {
const index = tasks.findIndex((task) => task.id === id);
if (index === -1) return false;
tasks.splice(index, 1);
return true;
};