Skip to content

A simple Node.js and Express REST API for managing a food list with full CRUD operations, built using Prisma ORM and PostgreSQL.

Notifications You must be signed in to change notification settings

ferrnd/food-management-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Food Management API

This is a RESTful API designed to manage food records, built with Node.js, Express.js for handling routes, and Prisma as the Object-Relational Mapper (ORM) for database interactions.

The code adheres to a simple Model-View-Controller (MVC) pattern, separating concerns into:

  • Router: Defines API endpoints.
  • Controller: Implements business logic and handles HTTP requests/responses.
  • Model: Manages direct database access using Prisma Client.

API Endpoints

The API base path (e.g., /foods) provides the following endpoints for CRUD (Create, Read, Update, Delete) operations:

Method Endpoint Description Controller Function
GET / Retrieves a list of all food items. listarTodos
GET /:id Retrieves a specific food item by its ID. listarUm
POST / Creates a new food item. criar
PUT /:id Updates an existing food item by its ID. atualizar
DELETE /:id Deletes a food item by its ID. deletar

➕ Create Food Item (POST /)

Request Body (JSON):

Field Type Required Description
nome string Yes The name of the food item.
descricao string Yes A description of the food item.

Success Response: HTTP 201 Created

Technical Details

Model Layer (prisma.food)

The Model layer is responsible for data persistence using Prisma:

  • encontreTodos(): Equivalent to SELECT * FROM food ORDER BY id ASC.
  • encontreUm(id): Fetches a single record by ID.
  • criar(data): Inserts a new food record.
  • deletar(id): Deletes a food record.
  • atualizar(id, data): Updates fields on a food record conditionally.

Controller Layer (foodController)

The Controller handles:

  • Input Validation: Ensures required fields (nome, descricao) are present for creation.
  • Existence Checks: Validates if a food item exists before attempting an update or delete.
  • Error Handling: Provides appropriate HTTP status codes and detailed error messages.

About

A simple Node.js and Express REST API for managing a food list with full CRUD operations, built using Prisma ORM and PostgreSQL.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published