From 1b40b1bed9f96e640c38ea2133bb684635f7965c Mon Sep 17 00:00:00 2001 From: Kalevich Ivan Date: Tue, 14 Apr 2026 15:58:12 +0300 Subject: [PATCH] feat: add priority field to todos (low/medium/high) --- src/models/todo.js | 4 ++-- src/routes/todos.js | 9 +++++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/models/todo.js b/src/models/todo.js index bc96713..44e44d1 100644 --- a/src/models/todo.js +++ b/src/models/todo.js @@ -9,8 +9,8 @@ function getById(id) { return todos.find(t => t.id === id) || null; } -function create(title) { - const todo = { id: nextId++, title, done: false }; +function create(title, priority) { + const todo = { id: nextId++, title, done: false, priority: priority || 'medium' }; todos.push(todo); return todo; } diff --git a/src/routes/todos.js b/src/routes/todos.js index e28def5..db7c95a 100644 --- a/src/routes/todos.js +++ b/src/routes/todos.js @@ -2,6 +2,8 @@ const express = require('express'); const router = express.Router(); const Todo = require('../models/todo'); +const VALID_PRIORITIES = ['low', 'medium', 'high']; + // GET /todos router.get('/', (req, res) => { res.json(Todo.getAll()); @@ -16,9 +18,12 @@ router.get('/:id', (req, res) => { // POST /todos router.post('/', (req, res) => { - const { title } = req.body; + const { title, priority } = req.body; if (!title) return res.status(400).json({ error: 'title is required' }); - res.status(201).json(Todo.create(title)); + if (priority && !VALID_PRIORITIES.includes(priority)) { + return res.status(400).json({ error: 'priority must be low, medium or high' }); + } + res.status(201).json(Todo.create(title, priority)); }); // PATCH /todos/:id