Coder Social home page Coder Social logo

library-example's Introduction

Library CRUD Example App

Alt text

This is an example CRUD (Create, Read, Update, Delete) book application built with FastAPI and Pydantic. It allows you to manage books via a simple to use API. This application also includes test coverage using Pytest.

Features

  • Create Book: Add a new book to the collection.
  • Read Books: Retrieve a list of all books or a specific book by its ID.
  • Update Book: Modify details of an existing book.
  • Delete Book: Remove a book from the collection.

Requirements

  • FastAPI
  • Pydantic
  • Uvicorn (for running the server)

Installation

pip install -r requirements.txt

Running the Application

uvicorn main:app --reload

API Endpoints

  • POST /books/ Add a new book. Requires a JSON body with title, author, and year.
  • GET /books/ Retrieve all books. Optional query parameter limit to limit the number of books returned.
  • GET /books/{book_id} Retrieve a book by its ID.
  • PUT /books/{book_id} Update a book by its ID. Requires a JSON body with updated title, author, and year.
  • DELETE /books/{book_id} Delete a book by its ID.

Data Model

Book

  • title: string
  • author: string
  • year: integer

Error Handling

  • Returns a 404 error if a book with the specified ID is not found.

Curl Examples

Adding a book

curl -X 'POST' \
  'http://127.0.0.1:8000/books/' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "title": "The Great Gatsby",
  "author": "F. Scott Fitzgerald",
  "year": 1925
}'

Retrieving Books

curl -X 'GET' \
  'http://127.0.0.1:8000/books/' \
  -H 'accept: application/json'

With a limit

curl -X 'GET' \
  'http://127.0.0.1:8000/books/?limit=2' \
  -H 'accept: application/json'

Retrieving Book By ID

curl -X 'GET' \
  'http://127.0.0.1:8000/books/{book_id}' \
  -H 'accept: application/json'

Updating a Book

curl -X 'PUT' \
  'http://127.0.0.1:8000/books/{book_id}' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "title": "New Title",
  "author": "New Author",
  "year": 1926
}'

Deleting a Book

curl -X 'DELETE' \
  'http://127.0.0.1:8000/books/{book_id}' \
  -H 'accept: application/json'

library-example's People

Contributors

brettskipy avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.