Skip to content

shyndaliu/capybook

Repository files navigation

Capybook API

a Golang project | Letterboxd analog for books

Deployed on Render.com

Use this https://capybook.onrender.com domain to send requests

Run Locally

Clone the project

  git clone -b mastter https://github.com/shyndaliu/capybook

Go to the project directory

  cd capybook

Up database service

  docker compose up -d go_db

Build and up capybook service

  docker compose build capybook
  docker compose up capybook

To run this project locally, you will need to add the following environment variables to your .env file.

I used Mailtrap for catching emails, you can use any analog

CAPYBOOK_DB_USER 
CAPYBOOK_DB_NAME
CAPYBOOK_DB_PASSWORD
CAPYBOOK_DB_DSN
SMTP_HOST 
SMTP_USERNAME
SMTP_PASSWORD 
JWT_SECRET

API Reference

Healthcheck

  GET /api/v1/healthcheck

Register new user

  POST /api/v1/users
Body parameter Type Description
username string Required. unique username
email string Required. valid email
password string Required. user's password

Activate via email

not supported in deployed version

  PUT /api/v1/users/activated
Body parameter Type Description
code string Required. verification code sent to user's email

Get user by username

  GET /api/v1/users/${username}
Path parameter Type Description
username string Required. username

Requirements : activated user

Change password

  PATCH /api/v1/users/${username}
Path parameter Type Description
username string Required. username
Body parameter Type Description
password string Required. new password
Requirements : activated user who own given username

Delete user

  DELETE /api/v1/users/${username}
Path parameter Type Description
username string Required. username

Requirements : activated user who own given username

Ban user

  POST /api/v1/users/${id}/ban
Path parameter Type Description
id int Required. user's id

Requirements : admin role

Get access and refresh token

  GET /api/v1/token
Body parameter Type Description
username string Required. unique username
email string Required. valid email
password string Required. user's password

Exchange refresh token to access token

  GET /api/v1/token/refresh
Authorization Scope Description
bearer refresh Required. valid refresh token

Create a new book

  POST /api/v1/books
Body parameter Type Description
title string Required. title of the book
author string Required. author of the book
year string Required. published year
description string Required. smth about a book
genres string[] Required. genres of the new book

Requirements : admin role

List books

  GET /api/v1/books
Query parameter Type Description
title string full match the title
author string full match the author
limit int how many items in one page
page int which page to get considering the limit size
sort string sort by author, title, year, rating or add "-" to sort in descending order

Get a book by id

  GET /api/v1/books/${id}
Path parameter Type Description
id int Required. Id of item to fetch

Update book by id

  PATCH /api/v1/books/${id}
Path parameter Type Description
id int Required. Id of item to update
Body parameter Type Description
title string title of the book
author string author of the book
year string published year
description string smth about a book
genres string[] genres of the new book

Requirements : admin role

Delete a book by id

  DELETE /api/v1/books/${id}
Path parameter Type Description
id int Required. Id of item to delete

Requirements : admin role

Post new review

  POST /api/v1/books/${id}/reviews
Path parameter Type Description
id int Required. id of book
Body parameter Type Description
content string Required. review for a book
rating int Required. number between 1 and 5

Requirements : activated user without an active ban

Get reviews under specific book

  GET /api/v1/books/${id}/reviews
Path parameter Type Description
id int Required. id of book
Query parameter Type Description
limit int how many items in one page
page int which page to get considering the limit size
sort string sort by created_at, rating or add "-" to sort in descending order

Update review

  PATCH /api/v1/books/${id}/reviews
Path parameter Type Description
id int Required. id of book
Body parameter Type Description
content string Required. new review for a book
rating int Required. number between 1 and 5

Requirements : activated user without an active ban

Delete review

  DELETE /api/v1/books/${id}/reviews
Path parameter Type Description
id int Required. Id of the book

Requirements : activated user

Delete specific review

  DELETE /api/v1/reviews/${id}
Path parameter Type Description
id int Required. Id of the review

Requirements : admin role

DB structure

Table books {
  id bigserial [primary key]
  title text [not null]
  author text [not null]
  year int [not null]
  description text [not null]
  genres text[] [not null]
}

Table users {
  id bigserial [primary key]
  username citext [not null, unique]
  email citext [not null, unique]
  password bytea [not null]
  token_hash text [not null]
  activated bool [not null, default: false]
}

// one-to-one
Table admins {
  id bigserial [primary key]
  user_id bigserial [not null, unique]
}
Ref: admins.user_id < users.id

// one-to-one
Table bans {
  id bigserial [primary key]
  user_id bigserial [not null, unique]
  expiry timestamp
}
Ref: bans.user_id < users.id


// many-to-many
Table reviews {
  id bigserial [primary key]
  created_at timestamp [not null, default: `now()`]
  user_id bigserial [not null]
  book_id bigserial [not null]
  content text
  rating integer
}

Ref: reviews.book_id < books.id
Ref: reviews.user_id < users.id

//one-to-one
Table verifications{
  code bytea [primary key]
  user_id bigint
  expiry timestamp
}
Ref: verifications.user_id < users.id

Authors

About

API project on Go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published