RESTful API for a library with basic functionalities of CRUD
Download mysite
repository and inside the repository open the terminal. Make sure Django and Django Rest Framework are pre-installed on the machine, if not
please do so by typing these command:-
pip install Django
pip install djangorestframework
Now, in the terminal type python manage.py runserver
.
The server will become live. Our server is hosted at http://127.0.0.1:8000/
Although, I have already added a few sample books to the database, you can also add books by the following method:-
In the terminal open Django's interactive shell by running python manage.py shell
command. Within the shell, manually create instances of the model (Library) and save them to the database using Django's ORM. Here is how:-
>>> from myapp.models import Library
>>> Library.objects.create(name='12 Rules For Life', description='An antidote to chaos', author='Jorden Peterson', genre='Non-fiction')
Replace name, description, author and genre as per your book.
Endpoint: GET /libraryapi/
Endpoint: POST /libraryapi/
Request Body: JSON object representing the new book. For example:-
{
"name": "The Silent Patient",
"description": "Shocking psychological thriller of a woman's act of violence against her husband—and of the therapist obsessed with uncovering her motive",
"author": "Alex Michaelides",
"genre": "Thriller"
}
Note: Please don't miss the '/' at the end of endpoint because it is necessary for POST request
Endpoint: PUT /libraryapi/{book_id}/
Request Body: JSON object with updated book details. For example:-
{
"name": "The Silent Patient",
"description": "Shocking psychological thriller of a woman's act of violence against her husband—and of the therapist obsessed with uncovering her motive",
"author": "Alex Michaelides",
"genre": "Thriller/Crime"
}
Note: Please don't miss the '/' at the end of endpoint because it is necessary for PUT request
For final clarification here are some images while I was testing the API:-
![Screenshot 2023-12-07 at 12 25 13 PM](https://private-user-images.githubusercontent.com/78219532/288661780-10025d18-ee6b-49c4-a8bc-0ab2d3e572c3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzNzU2OTAsIm5iZiI6MTczOTM3NTM5MCwicGF0aCI6Ii83ODIxOTUzMi8yODg2NjE3ODAtMTAwMjVkMTgtZWU2Yi00OWM0LWE4YmMtMGFiMmQzZTU3MmMzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEyVDE1NDk1MFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTJiMDNkZDVhNzE2ZjJhNTQ1MjJhOTM5YTRiY2E1MzU0MTkwZjQ0YzY3YzgzOWQyYTEwMDFhNDAyMTE0ZmJhYTMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.ak3ueD5S0yW2F0IQAZ_pGuWUuz7hD_A1S-I0ajw4kJU)
![Screenshot 2023-12-07 at 12 26 04 PM](https://private-user-images.githubusercontent.com/78219532/288662030-7378cfaa-70cc-455b-913a-e325bf728438.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzNzU2OTAsIm5iZiI6MTczOTM3NTM5MCwicGF0aCI6Ii83ODIxOTUzMi8yODg2NjIwMzAtNzM3OGNmYWEtNzBjYy00NTViLTkxM2EtZTMyNWJmNzI4NDM4LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEyVDE1NDk1MFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTQwOThlNzA4NGI4ODVjMWUxOGQ5MGVjN2YxMTEzYzljN2E3NTI0MTllY2IzMDU4MzljZGE1N2U1YjM5ZDMwNWQmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.5AZCFoFFZudME4r3tdvfADOA3yYHy_AooBJOcM7MWSQ)