{
  "openapi": "3.0.1",
  "info": {
    "title": "API Service",
    "version": "1.0.0",
    "description": "API Service endpoints",
    "contact": {
      "email": "sidhant@panda.fyi",
      "name": "Sidhant Panda"
    }
  },
  "servers": [
    {
      "url": "/"
    }
  ],
  "tags": [
    {
      "name": "book",
      "description": "Books APIs"
    }
  ],
  "paths": {
    "/book/add": {
      "post": {
        "tags": [
          "book"
        ],
        "summary": "Add a book to the database",
        "operationId": "book-add",
        "requestBody": {
          "description": "New user signing up",
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/DraftBookSchema"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "Book was added",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BookSchema"
                }
              }
            }
          },
          "400": {
            "description": "Bad request",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BadRequestSchema"
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ServerErrorSchema"
                }
              }
            }
          }
        }
      }
    },
    "/book/all": {
      "get": {
        "tags": [
          "book"
        ],
        "summary": "Get all books in database",
        "operationId": "book-get-all",
        "responses": {
          "200": {
            "description": "Search results",
            "content": {
              "application/json": {
                "schema": {
                  "properties": {
                    "books": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/BookSchema"
                      }
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ServerErrorSchema"
                }
              }
            }
          }
        }
      }
    },
    "/book/id/{bookId}": {
      "get": {
        "parameters": [
          {
            "in": "path",
            "name": "bookId",
            "schema": {
              "type": "string"
            },
            "required": true,
            "description": "ID of book to retrieve"
          }
        ],
        "tags": [
          "book"
        ],
        "summary": "Retrieve a book by ID",
        "operationId": "book-get",
        "responses": {
          "200": {
            "description": "Book details",
            "content": {
              "application/json": {
                "schema": {
                  "properties": {
                    "book": {
                      "$ref": "#/components/schemas/BookSchema"
                    }
                  }
                }
              }
            }
          },
          "404": {
            "description": "Book not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/NotFoundErrorSchema"
                }
              }
            }
          }
        }
      },
      "delete": {
        "parameters": [
          {
            "in": "path",
            "name": "bookId",
            "schema": {
              "type": "string"
            },
            "required": true,
            "description": "ID of book to remove"
          }
        ],
        "tags": [
          "book"
        ],
        "summary": "Remove a book by ID",
        "operationId": "book-remove",
        "responses": {
          "204": {
            "description": "Book removed"
          },
          "404": {
            "description": "Book not found",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/NotFoundErrorSchema"
                }
              }
            }
          }
        }
      }
    },
    "/book/search": {
      "get": {
        "tags": [
          "book"
        ],
        "summary": "Search books by name or author",
        "operationId": "book-search",
        "parameters": [
          {
            "in": "query",
            "name": "name",
            "schema": {
              "type": "string",
              "default": "hitchhiker"
            },
            "description": "String containing full name or partial name of the book"
          },
          {
            "in": "query",
            "name": "author",
            "schema": {
              "type": "string",
              "default": "douglas"
            },
            "description": "String containing full name or partial name of the author"
          }
        ],
        "responses": {
          "200": {
            "description": "Search results",
            "content": {
              "application/json": {
                "schema": {
                  "properties": {
                    "books": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/BookSchema"
                      }
                    }
                  }
                }
              }
            }
          },
          "500": {
            "description": "Internal Server Error",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ServerErrorSchema"
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "BadRequestSchema": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string",
            "description": "Error message",
            "example": "PATH: [author] ;; MESSAGE: \"author\" is required"
          }
        },
        "required": [
          "error"
        ]
      },
      "ServerErrorSchema": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string",
            "description": "Error message",
            "example": "Internal server error"
          }
        },
        "required": [
          "error"
        ]
      },
      "NotFoundErrorSchema": {
        "type": "object",
        "properties": {
          "error": {
            "type": "string",
            "description": "Error message",
            "example": "Item not found"
          }
        },
        "required": [
          "error"
        ]
      },
      "DraftBookSchema": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string",
            "description": "Name of the book",
            "example": "The Hitchhiker's Guide to the Galaxy"
          },
          "author": {
            "type": "string",
            "description": "Name of the author",
            "example": "Douglas Adams"
          }
        },
        "required": [
          "name",
          "author"
        ]
      },
      "BookSchema": {
        "allOf": [
          {
            "$ref": "#/components/schemas/DraftBookSchema"
          }
        ],
        "type": "object",
        "properties": {
          "_id": {
            "type": "string",
            "description": "MongoDB id of the document",
            "example": "5d7224d3b40b1a1a046697dd"
          },
          "createdAt": {
            "type": "number",
            "description": "Created at timestamp in miliseconds",
            "example": 1616158942428
          },
          "updatedAt": {
            "type": "number",
            "description": "Updated at timestamp in miliseconds",
            "example": 1616158942428
          }
        },
        "required": [
          "_id",
          "createdAt",
          "updatedAt"
        ]
      }
    }
  }
}