CodingNic

Categories and Summary

Add a Category

Categories and Summary 15 min read

Add a Category

Create a POST /api/categories route and a small API helper.

Trim the name and treat case-insensitive duplicates as the same category.

javascript
const existing = db.categories.find(
  (category) => category.name.toLowerCase() === name.toLowerCase()
);

Test it

Add Travel, then try travel. The second attempt should reuse the existing category instead of creating a duplicate.

Checkpoint

Category creation is persisted and duplicate names are handled consistently.