NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

from flask import Flask, request, jsonify
from pymongo import MongoClient
from bson import ObjectId
from flask_cors import CORS

app = Flask(__name__)
CORS(app)

# Step-1: Establish the connection to the MongoDB
# Replace your 'your_mongodb_url' with your MongoDB connection string
client = MongoClient("mongodb://localhost:27017")

db = client.BookHive
collection = db.books

# CREATE operation
@app.route('/books',methods=['POST'])
def create_books():
data = request.get_json()
insert_result = collection.insert_one(data)
return jsonify({"message": "Book added successfully", "id": str(insert_result.inserted_id)}), 201

# READ operation(Retrieve all employees)
@app.route('/books',methods=['GET'])
def get_books():
books = list(collection.find())
for book in books:
book['_id'] = str(book['_id']) # Convert ObjectId to string
return jsonify(books), 200


# READ operation(Retrieve a specific employee)
@app.route('/books/<book_id>',methods=['GET'])
def get_book(book_id):
employee = collection.find_one({'_id': ObjectId(employee_id)})
if employee:
employee['_id'] = str(employee['_id']) # Convert ObjectId to string
return jsonify(employee), 200
else:
return jsonify({"message":"Employee Not Found"}), 404


# UPDATE Operation
@app.route('/books/<book_id>', methods=['PUT'])
def update_book(book_id):
data = request.get_json()
update_result = collection.update_one({'_id': ObjectId(book_id)}, {'$set': data})
if update_result.modified_count > 0:
return jsonify({"message": "Book updated successfully"}), 200
else:
return jsonify({"message": "Book Not Found"}), 404


# DELETE Operation
@app.route('/book/<book_id>', methods=['DELETE'])
def delete_book(book_id):
delete_result = collection.delete_one({'_id': ObjectId(book_id)})
if delete_result.deleted_count > 0:
return jsonify({"message": "Book deleted successfully"}), 200
else:
return jsonify({"message": "Book Not Found"}), 404



if __name__ == '__main__':
app.run(debug=True)

     
 
what is notes.io
 

Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...

With notes.io;

  • * You can take a note from anywhere and any device with internet connection.
  • * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
  • * You can quickly share your contents without website, blog and e-mail.
  • * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
  • * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.

Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.

Easy: Notes.io doesn’t require installation. Just write and share note!

Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )

Free: Notes.io works for 12 years and has been free since the day it was started.


You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;


Email: [email protected]

Twitter: http://twitter.com/notesio

Instagram: http://instagram.com/notes.io

Facebook: http://facebook.com/notesio



Regards;
Notes.io Team

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.