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.
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 |
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
The Model layer is responsible for data persistence using Prisma:
encontreTodos(): Equivalent toSELECT * 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.
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.