Skip to content
/ sten Public

A simple API made to my school for teaching how REST API's works.

Notifications You must be signed in to change notification settings

Floriian/sten

Repository files navigation

STEN

Basic concept

The app name is anagram of Nest. The basic concept is create a free API for learning purposes. In my school we learn Python, and this created specially for my school. But I think, it's very useful for other schools for teaching how RESTAPI-s works, or everyone else :) It uses NestJS under the hood. Supports basic HTTP methods (GET, POST, PATCH, DELETE). I will add Put method in the future.

How can I install locally?

  1. With Docker-Compose
  2. Without Docker-Compose

With Docker-Compose

You will need:

  1. Register to visualcrossing, and you will get an api key. Copy the given API key.
  2. After you installed Docker, you need to clone this repo.
    With HTTPS:
    git clone https://github.com/Floriian/sten.git
    With SSH:
    git clone https://github.com/Floriian/sten.git
  3. Navigate to git repo's folder, and edit the docker-compose file:
    The app is listening on default port 3000. If the port is busy, it runs on port 5400. If theese ports, is not good for you, change (or add a new port) the first value of ports, and write an another expose children with the choosed port.
  4. Start the app:
    docker compose up -d, or docker-compose up -d
  5. Verify the app is running. docker ps it shows something similar to this:
  6. Swagger: open browser, and type (with default port): http://localhost:3000

Without Docker-Compose

You will need:

  1. Get the Weather API key, and save somewhere.
  2. Install NodeJS & PostgreSQL on your computer.
  3. Create a database in the Postgres server.
  4. Clone the git repo.
  5. In terminal, navigate to Gitrepos folder, and type:
npm install
  1. Open .env file, and change WeatherAPI key to your API key.
  2. Modify your connection string, with your PostgreSQL authentication info.
DATABASE_URL="postgresql://<POSTGRES_USERNAME>:<POSTGRES_PASSWORD>@localhost:<POSTGRES_PORT>/<POSTGRES_DATABASE>?schema=public"
  1. After you changed the .env file, enter the following command:
npm run build
  1. After the application is builded, start the server.
npm run start:prod
  1. The port is in the console, where is the app listening.

Endpoints

Animals

GET

GET /animal

It returns all animals in the db.

Option Value
Params None
Request body Required

Example responses

  • Response (Code 200):
    [
      {
        "id": 0,
        "name": "The animal's name",
        "specie": "The animal's specie",
        "age": 10
      },
      {
        "id": 1,
        "name": "The animal's name",
        "specie": "The animal's specie",
        "age": 11
      }
    ]
  • Response (Code 404): No animals in the database.

Get by ID.

GET /animal/:id

It returns one animal, with a given ID.

Examples:

GET /animal/1

Example responses

  • Response (Code 200):

    {
      "id": 1,
      "name": "Lessie",
      "specie": "Fox",
      "age": 18
    }
  • Response (Code 404):
    No animal found in database.

  • Response (Code 400):
    This response happens, when the param not int, It will return a body


POST

POST /animal
Option Value
Params None
Request body Required

Request body options:

Key Type Required?
name string
specie string
age int

Example request:

POST /animal
Content-Type: application/json

{
   "name": "Radír",
   "specie": "Mopsz",
   "age": 12
}

Example responses

  • Response (Code 201):
    The animal has been created, and returns the created animal data.
    {
      "id": 1,
      "name": "Radír",
      "specie": "Mopsz",
      "age": 12
    }
  • Response (Code 400)
    This response happens, when the body is failed the validation test.

PATCH

PATCH /animal/:id

It will update animal by id, and returns the animal with updated data.

Option Value
Params Required
Request body Required

Request body options:

Key Type Required?
name string
specie string
age int

Example request:

PATCH /animal/1
Content-Type: application/json

{
   "name": "Lessie"
}

Example responses

  • Response (Code 202)

    {
      "name": "Lessie",
      "specie": "Fox",
      "age": 10
    }
  • Response (Code 404):
    No animal found with this ID.

  • Response (Code 400):
    This response happens, when the body is failed the validation test.

DELETE

DELETE /animal/:id

It deletes animal by id, and return the deleted animal's data.

Option Value
Params Required
Request body None

Example request

DELETE /animal/1

Example responses

  • Response (Code 200)
    {
      "id": 1,
      "name": "Radír",
      "specie": "Mopsz",
      "age": 10
    }
  • Response (Code 404)
    No animal fond with the given ID.
  • Response (Code 400)
    This response happens, when the param not int, It will return a body

About

A simple API made to my school for teaching how REST API's works.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages