GithubHelp home page GithubHelp logo

clifftech123 / hng-task-2 Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 137 KB

Api to accept person name us input and perform crud opration with it

TypeScript 94.38% JavaScript 5.62%
api backend expressjs nodejs typescript

hng-task-2's Introduction

CRUD REST API for "Person" Resource

Welcome to the CRUD REST API project for managing a "person" resource. This project allows you to perform basic CRUD (Create, Read, Update, Delete) operations on person data. The API is built with Node.js, Express, and MongoDB.

Technologies Used

  • Node.js
  • Express.js
  • MongoDB
  • TypeScript
  • Yarn

Overview

This project aims to create a robust REST API for managing person data. It includes endpoints for creating, reading, updating, and deleting person records. The API is designed to handle dynamic parameters, making it flexible to perform operations based on provided details.

Features

  • Create a new person record.
  • Retrieve person details by name.
  • Update existing person records by name.
  • Delete person records by name.
  • Dynamic parameter handling for flexible queries.
  • Secure interactions with the database to prevent common vulnerabilities.

Getting Started

  1. Clone this repository to your local machine using git clone or download the ZIP file and extract it to a folder.`
  git clone https://github.com/Clifftech123/hng-rest-crud-api.git
  1. Install the required dependencies using
  yarn install
  1. Create a .env file and configure your environment variables (e.g., database connection details).

  2. Run the API using

  yarn dev

API Endpoints and HTTP Methods

Create a Person

  • Endpoint: POST https://hngtask1-stage-2.onrender.com/api

  • Request Format:

    {
      "name": "Clifford Opoku  Isaiah"
    }
  • Response Format (Success):

{
  "name": "Clifford Opoku  Isaiah ",
  "_id": "6501030f31ce063922818171",
  "__v": 0
}

This endpoint creates a new person record in the database. The name attribute is required and must be a string. The API returns the newly created person record in the response body.

  • Response Format (Error): If an number input is provided, the API returns an error message in the response body.
{
  "error": "[\n  {\n    \"code\": \"invalid_type\",\n    \"expected\": \"string\",\n    \"received\": \"number\",\n    \"path\": [\n      \"name\"\n    ],\n    \"message\": \"Expected string, received number\"\n  }\n]"
}
  • Response Format (Error): If an invalid input is provided, the API returns an error message in the response body.
    {
      "error": "Invalid input data"
    }

Retrieve a Person

  • Endpoint: GET https://hngtask1-stage-2.onrender.com/api/:id
  • Example : GET https://hngtask1-stage-2.onrender.com/api/6501030f31ce063922818171

  • Request Parameters:

    • id (string) - The ID of the person record to retrieve.
  • Response Format (Success):
{
  "_id": "6501030f31ce063922818171",
  "name": "Clifford Opoku  Isaiah ",
  "__v": 0
}
  • Response Format (Error - Person not found):
    {
      "error": "Person not found"
    }

Update a Person

  • Endpoint: PUT https://hngtask1-stage-2.onrender.com/api/:id
  • Example : PUT https://hngtask1-stage-2.onrender.com/api/6501030f31ce063922818171
  • Request Format:
    {
      "name": "Updated Name"
    }
    • Request Parameters:

      • id (string) The ID of the person record to update.
  • Response Format (Success):
{
  "_id": "6501030f31ce063922818171",
  "name": "Updated Name ",
  "__v": 0
}
  • Response Format (Error - Person not found):
    {
      "error": "Person not found"
    }

Delete a Person

  • Endpoint: Delete https://hngtask1-stage-2.onrender.com/api/:id
  • Example : Delete https://hngtask1-stage-2.onrender.com/api/6501030f31ce063922818171
  • Response Format (Success): No content (204)
    • Request Parameters:

      • id (string) The ID of the person record to delete.
  • Response Format (Error - Person not found):
{
  "message": "Person deleted successfully."
}

UML Diagrams

  • UML diagrams representing the API's class structure and entity-relationship design can be found in the docs/UML_diagrams.png folder. UML

POSTMAN API AUTOMATION TESTING

This project includes automated tests for each API endpoint using Postman. The tests are located in the Postman folder in src/Postman and are organized by endpoint. The tests are designed to run against a local instance of the API. To run the tests, follow the steps below.

API Points

  • Endpoint: POST https://hngtask1-stage-2.onrender.com/api
  • Test code
 - *  `Postman Collection name `: Post-Person-Test

pm.test("Create a Person - Status Code is 201", function () {
    pm.response.to.have.status(201);
});

pm.test("Create a Person - Response  Name", function () {
    pm.response.to.have.jsonBody("name");
});
  • Response Format (Pass):
  • Create a Person - Status Code is 201
  • Create a Person - Response Name
  • Endpoint: GET https://hngtask1-stage-2.onrender.com/api/:id
    • Postman Collection name : Get-Person-by-id
  • Test code
pm.test("Response body is valid JSON", function () {
    pm.response.to.be.json;
});
  • Response Format (Pass):
  • Response body is valid JSON
  • Endpoint: PUT https://hngtask1-stage-2.onrender.com/api/:id
    • Postman Collection name : update-person-by-id
  • Test code
pm.test("Update a Person - Status Code is 200", function () {
    pm.response.to.have.status(200);
});
  • Response Format (Pass):
  • Update a Person - Status Code is 200
  • Endpoint: Delete https://hngtask1-stage-2.onrender.com/api/:id
    • Postman Collection name : Delete-Person-by-id
pm.test("Update a Person - Status Code is 404", function () {
    pm.response.to.have.status(404);
});
  • Response Format (Pass):
  • Update a Person - Status Code is 404

hng-task-2's People

Contributors

clifftech123 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.