Rest API CRUD for books
Manipulate books via API
Instalation
- Copy
.env.example
file to create .env
file
- Run docker container with postgres
docker-compose up -d
- Apply migrations
migrate -path ./database/migrations/ -database 'postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable' up
- Build
go build
- Execute program
./junior-test
Usage
API url: localhost:8000
Endpoints for books observation:
GET /books - Get list of all books
GET /book/:id - Get single book by id
Guest user can only view list of books or single book.
Authentication is needed to create/update/delete
books
Available actions for authentication:
POST /auth/sign-up - Registration
POST /auth/sigh-in - Login
Registration and authentication example body:
{
"username": "test",
"password": "123"
}
After registration you can sign in to receive jwt token
Then place your token to Authentication header
Authentication: Bearer <your_token_here>
If you set valid token, you are able to create/update/delete
books
Endpoints for books manipulations:
POST /book - Create book
PUT /book/:id - Update book
DELETE /book/:id - Delete book
User can update/delete
only books he created.
Books example body:
{
"title": "Ktulhu",
"author": "Lovecraft",
"description": "Very scary book",
"genre": "horror",
"page_count": 546,
"year": 1910,
"price": 5000
}