This repository contains a Node.js project that implements an API for managing fruits. It provides endpoints to create fruits, retrieve all fruits, and get a specific fruit by its ID. The project uses the Express.js framework for building the API and Prisma as the ORM (Object-Relational Mapping) to interact with the database.
The API provides the following endpoints:
- POST /fruits: Creates a new fruit.
- GET /fruits: Returns all fruits.
- GET /fruits/:id: Returns a specific fruit based on its ID.
-
Clone the repository:
git clone <repository_url> cd <repository_name>
-
Install the dependencies:
npm install
-
Set up your database configuration in the .env file. Ensure you have a running database instance (compatible with Prisma) with the correct credentials.
To start the server, run:
npm start
By default, the server will listen on port 3000. You can modify this in the index.ts file if needed.
The API handles errors for various scenarios:
- httpStatus.BAD_REQUEST: This status is returned when an invalid fruit ID is provided.
- httpStatus.NOT_FOUND: This status is returned when a requested fruit is not found.
- httpStatus.CONFLICT: This status is returned when trying to create a fruit that is already registered.
The project includes a set of test cases to ensure the API endpoints function correctly. The testing framework used is not specified in the provided code. The tests cover the following scenarios:
- POST /fruits:
- Should return 201 when inserting a fruit.
- Should return 409 when inserting a fruit that is already registered.
- Should return 422 when inserting a fruit with missing data.
- GET /fruits:
- Should return 404 when trying to get a fruit that doesn't exist.
- Should return 400 when an invalid ID parameter is provided.
- Should return a fruit given an ID.
- Should return all fruits.
To run the tests, use the following command:
npm test
Ensure you have the appropriate testing framework and environment set up to execute the tests successfully.
That's it! You now have an overview of the fruit management API project and how to set it up and run it. Happy fruit management!