ParvinEyvazov / book-api

This is a Book - Author relation REST API written with Layered Architecture

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Book REST API

This project is an example REST API that shows how to work with local and DB. As an architecture, Layered Architecture used.

layers

This API contains 2 services: Book, Author


1 - Running project

On the root folder:

go run .

2 - (alternative) Running with DB (using Docker)

You need to have Docker Compose on your machine, then on the root folder:

  • Building images
docker compose build --no-cache
docker compose up

3 - Running tests

  • Coverage

layers

  • Run Tests:
go test -v ./...
  • Calculate coverage
go test -cover ./...
  • Export coverage file as html
go test -coverprofile c.out ./...
go tool cover -html=c.out

4 - Version

API has 2 version.

  • Storing data locally <base_url>/api/v1
  • Storing data in DB <base_url>/api/v2

5 - BOOK Endpoints

  • GET ALL BOOKS
Request: GET /api/v1/books

Response: {
    id:                 string
    title:              string
    description:        string
    publication_date:   string
    authorIDs:          string[]
}[]
  • GET SINGLE BOOK WITH id
Request: GET /api/v1/book/:id

Response: {
    id:                 string
    title:              string
    description:        string
    publication_date:   string
    authorIDs:          string[]
}
  • SEARCH BOOKS (searching for title or description or authorId)
Request: GET /api/v1/book/search/:search_text

Response: {
    id:                 string
    title:              string
    description:        string
    publication_date:   string
    authorIDs:          string[]
    authors             {
                            id:      string
                            name:    string
                            surname: string
                        }[]
}[]
  • CREATE A BOOK
Request: POST /api/v1/book
body {
    title:              string
    description:        string
    publication_date:   string
    authorIDs:          string[]
}

Response: {
    id:                 string
    title:              string
    description:        string
    publication_date:   string
    authorIDs:          string[]
}
  • UPDATE EXISTING BOOK WITH id
Request: PUT /api/v1/book/:id
body {
    title:              string
    description:        string
    publication_date:   string
    authorIDs:          string[]
}

Response: {
    id:                 string
    title:              string
    description:        string
    publication_date:   string
    authorIDs:          string[]
}
  • DELETE EXISTING BOOK WITH id
Request: DELETE /api/v1/book/:id

Response: {
    id:                 string
    title:              string
    description:        string
    publication_date:   string
    authorIDs:          string[]
}

AUTHOR Endpoints

  • GET ALL AUTHORS
Request: GET /api/v1/authors

Response: {
    id:         string
    name:       string
    surname:    string
}[]
  • GET SINGLE AUTHOR WITH id
Request: GET /api/v1/author/:id

Response: {
    id:         string
    name:       string
    surname:    string
}
  • SEARCH AUTHORS (searching for name or surname)
Request: GET /api/v1/author/search/:search_text

Response: {
    id:         string
    name:       string
    surname:    string
    books:      {
                    id                  string
                    title               string
                    description         string
                    publication_date    string
                    authorIDs           string[]
                }[]
}[]
  • CREATE AN AUTHOR
Request: POST /api/v1/author
body {
    name:       string
    surname:    string
}

Response: {
    id:         string
    name:       string
    surname:    string
}
  • UPDATE EXISTING AUTHOR WITH id
Request: PUT /api/v1/author/:id
body {
    name:       string
    surname:    string
}

Response: {
    id:         string
    name:       string
    surname:    string
}
  • DELETE EXISTING AUTHOR WITH id
Request: DELETE /api/v1/author/:id

Response: {
    id:         string
    name:       string
    surname:    string
}

For version 2, DB is not fully implemented, but these 2 endpoints were implemented to show how using MySQL DB using in the code.

  • GET ALL BOOKS
Request: GET /api/v2/books

Response: {
    id:                 string
    title:              string
    description:        string
    publication_date:   string
    authorIDs:          string[]
}[]
  • GET ALL AUTHORS
Request: GET /api/v2/authors

Response: {
    id:         string
    name:       string
    surname:    string
}[]

About

This is a Book - Author relation REST API written with Layered Architecture


Languages

Language:Go 98.0%Language:Dockerfile 1.7%Language:Shell 0.3%