Add task list demo application
This commit is contained in:
65
examples/task-list/server/index.js
Normal file
65
examples/task-list/server/index.js
Normal 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)}…`));
|
||||
});
|
||||
65
examples/task-list/server/tasks.js
Normal file
65
examples/task-list/server/tasks.js
Normal 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;
|
||||
};
|
||||
Reference in New Issue
Block a user