/crudhng

Primary LanguagePython

CRUD_SERVER

Table Of Contents

CRUD SERVER API-ENDPOINT DOCUMENTATION




Base Uri



Hosted for live testing on https://godand.pythonanywhere.com ....

Set up the server (local)

Clone The Repository

$ git clone https://github.com/Godhanded/crudhng.git

$ cd crudhng

Install Dependencies

# create Virtual Environment
$ python3 -m venv venv

# Activate Virtual Env
$ source venv/bin/activate

# Install Dependencies
$ pip install -r requirements.txt

Run the Server

$ python3 run.py 

Run API TESTs

Note: ensure you are connected to the internet before running tests and are in crudhng directory

# install test suite and http requests library
$ pip install requests pytest

# Run the tests in test_crud.py
$ pytest test_crud.py -v

click for test_crud.py file

DataBase Info

  • Sqlite file database is used
  • SqlAlchemy ORM was used for db interactions
  • Sqlite file.db file is created in /instance directory when the server is run for the first time.
  • DataBase Models click here

Error Handling



Errors are returned as JSON objects in the following format with their error code

{
  "error": "error name",
  "message": "error description"
}

The API will return 2 error types, with diffreent descriptions when requests fail;

  • 404: resource not found
  • 500: Internal server error

EndPoints




Create

POST '/api'

  • Creates a new user and adds them to DataBase
  • Request Body: JSON object containing
{
  "name":"name of person",
}
  • Returns: JSON, Id and Name of created user
 {
    "message": "Success",
    "id": 1,
    "name": "name of person"
 }

status code: 201



Read

GET '/api/${id}'

  • Gets a person from the database using user id
  • Path Parameter: id- integer id of person to retrieve
  • Returns: JSON, message and person object containing name id and date created
 {
    "message": "Success",
    "person": {
        "id":1,
        "name":"name of user",
        "date_created":"Mon, 11 Sep 2023 01:04:27 GMT"
    }
 }

status code: 200



Update

PATCH '/api/${id}'

  • Update or change name of person record
  • Path Parameter: id- integer id of person to update
  • Request Body: Json object containing
 {
    "name":"new name"
 }
  • Returns: JSON, message and person object containing name id and date created
 {
    "message": "name updated",
    "id": 0,
    "name":"new name"
 }

status code: 200


PUT '/api/${id}'

  • Update or change name of person record
  • Path Parameter: id- integer id of person to update
  • Request Body: Json object containing
 {
    "name":"new name"
 }
  • Returns: JSON, message and person object containing name id and date created
 {
    "message": "name updated",
    "id": 0,
    "name":"new name"
 }

status code: 200



Delete

DELETE '/api/${id}'

  • Delete record of Person from database

  • Path Parameter: id- integer id of person to delete

  • Returns: JSON, message and id of deleted record

 {
    "message": "deleted",
    "id": 3
 }

status code: 200



UML CLASS DIAGRAM


Assumptions/Limitations

  • Multiple records or persons can have the same name, an ID will make them unique
  • Crud operations will be made using a unique identifier id which is provided upon create operation

Sample Usage


Create Operation

Read Operation

Update Operation

Delete Operation

Authors