Skip to content

bme-db-lab/szglab5-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

szglab5-backend

Git repository for laboradmin backend service

Table of Contents

Prerequisites

You will need the following things properly installed on your computer.

Installation

Clone repository

git clone <repository-url>

Install npm packages

cd szglab5-backend
npm install

Install nodemon (automatic server restart)

npm install nodemon -g

Create database (default is "laboradmin")

psql -U postgres
CREATE DATABASE laboradmin;

Config

Available node environments ('dev' | 'prod' | 'test')

Steps to add specific config:

  • Create a new file to config/ with name of config.[env].json
  • Copy the content of config.example.json to the new file
  • Modify or delete rows

Commands

CLI (npm run start cli:"ENV" "command")

seed [filepath]
  • filepath: the json file path that contains the seed data, relative to /db/seedData (default: 'dev.seed.json')
npm run cli:dev seed test1.seed.json

START (npm run start:"ENV")

Starts the API server

npm run start:dev

Endpoints

The endpoints format fit JSON API specification.

Model names in plural (-> modelNamePlural):

  • tests (Test model)
  • languages (Language model)
  • questions (Question model)

Fetching Resources

List (GET /:modelNamePlural)

  • Request: GET /questions HTTP/1.1
  • Response:
{
  "data": [
    {
      "type": "questions",
      "id": 4,
      "attributes": {
        "id": 4,
        "text": "tuturu 2",
        "createdAt": "2017-03-15T17:07:30.284Z",
        "updatedAt": "2017-03-15T17:07:30.284Z",
        "testId": 2
      },
      "relationships": {
        "test": {
          "data": {
            "id": 2,
            "type": "tests"
          }
        }
      }
    },
    {
      "type": "questions",
      "id": 1,
      "attributes": {
        "id": 1,
        "text": "Kérdés 1",
        "createdAt": "2017-03-15T17:07:30.244Z",
        "updatedAt": "2017-03-15T17:07:30.244Z",
        "testId": null
      },
      "relationships": {
        "test": null
      }
    },
    {
      "type": "questions",
      "id": 2,
      "attributes": {
        "id": 2,
        "text": "Kérdés 2",
        "createdAt": "2017-03-15T17:07:30.261Z",
        "updatedAt": "2017-03-15T17:07:30.261Z",
        "testId": null
      },
      "relationships": {
        "test": null
      }
    }
  ]
}

Get (GET /:modelNamePlural/:id)

  • Request: GET /questions/2 HTTP/1.1
  • Response:
{
  "data": {
    "type": "questions",
    "id": 2,
    "attributes": {
      "id": 2,
      "text": "Kérdés 2",
      "createdAt": "2017-03-15T17:07:30.261Z",
      "updatedAt": "2017-03-15T17:07:30.261Z",
      "testId": null
    },
    "relationships": {
      "test": null
    }
  }
}

Get relation (GET /:modelNamePlural/:id/relModelNamePlural)

!Not supported yet!

Creating Resources (POST /:modelNamePlural)

  • Header: Content-Type: application/vnd.api+json
  • Request: POST /questions
  • Body:
{
	"data": {
		"type": "questions",
		"attributes": {
			"text": "New questiion, yupiii blblblblblb"
		},
		"relationships": {
			"test": {
				"data": {
					"type": "tests",
					"id": 3
				}
			}
		}
	}
}

Response:

  {
    "data": {
      "type": "questions",
      "id": 8,
      "attributes": {
        "id": 8,
        "text": "New questiion, yupiii blblblblblb",
        "updatedAt": "2017-03-16T09:12:29.966Z",
        "createdAt": "2017-03-16T09:12:29.956Z",
        "testId": 3
      }
    }
  }

Updating resources (PATCH /:modelNamePlural/:id)

  • Header: Content-Type: application/vnd.api+json
  • Request: PATCH /questions/2
  • Body:
{
  "data": {
    "type": "questions",
    "attributes": {
      "text": "Change this text!"
    },
    "relationships": {
      "test": {
        "data": {
          "type": "tests",
          "id": 3
        }
      }
    }
  }
}

Response: 204 (No Content)

Deleting Resources (DELETE /:modelNamePlural/:id)

  • Request: DELETE /questions/2
  • Response: 204 (No Content)

Useful links