olurocks / stage2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

stage2

Person API

This API allows you to perform CRUD (Create, Read, Update, Delete) operations on Person objects. You can interact with this API to manage information about individuals.

Getting Started

Before you begin, make sure you have the following prerequisites:

  • Node.js and npm installed on your machine.
  • A database configured and connected. This API uses Sequelize to interact with the database, so make sure to set up your database accordingly.

Installation

  1. Clone the repository to your local machine.
git clone https://github.com/olurocks/stage2.git
  1. Navigate to the project directory.
cd stage2
  1. Install the required dependencies.
npm install
  1. Configure your database connection by editing the config/config.json file to match your database credentials.

  2. Create the database tables by running the Sequelize migration:

npx sequelize db:migrate
  1. Start the server.
npm run dev

Endpoints

Create a Person

  • Endpoint: POST /
  • Description: Create a new person with a name and a value.
  • Request Body:
{
  "name": "John Doe",
  "value": "42"
}
  • Response:
{
  "id": 1,
  "name": "John Doe",
  "value": "42",
  "createdAt": "2023-09-11T00:00:00.000Z",
  "updatedAt": "2023-09-11T00:00:00.000Z"
}

Get a Person by ID

  • Endpoint: GET /:id
  • Description: Retrieve a person by their ID.
  • Response:
{
  "id": 1,
  "name": "John Doe",
  "value": "42",
  "createdAt": "2023-09-11T00:00:00.000Z",
  "updatedAt": "2023-09-11T00:00:00.000Z"
}

Update a Person by ID

  • Endpoint: PUT /:id
  • Description: Update a person's name and value by their ID.
  • Request Body:
{
  "name": "Updated Name",
  "value": "43"
}
  • Response:
{
  "message": "Person updated successfully"
}

Delete a Person by ID

  • Endpoint: DELETE /:id
  • Description: Delete a person by their ID.
  • Response:
{
  "message": "User deleted successfully",
  "deleted_user": {
    "id": 1,
    "name": "John Doe",
    "value": "42",
    "createdAt": "2023-09-11T00:00:00.000Z",
    "updatedAt": "2023-09-11T00:00:00.000Z"
  }
}

Error Handling

  • If the provided ID does not exist, you will receive a 404 Not Found response.

  • If the request body does not contain valid name and value fields, you will receive a 400 Bad Request response.

  • If there is an internal server error, you will receive a 500 Internal Server Error response.

    Test

  • To run tests run the command in your root directory

npm run test

About


Languages

Language:JavaScript 100.0%