DEV Community

Ernest Kabahima
Ernest Kabahima

Posted on

Simple Event Registration API with MongoDB and Flask

let's start

Simple Event Registration API is a demonstration of how to create, read and write data with MongoDB using Flask

MongoDB

MongoDB is a document database with the scalability and flexibility that you want with the querying and indexing that you need. Its document model is simple for developers to learn and use, while still providing all the capabilities needed to meet the most complex requirements at any scale but You can get started here

Use cases for MongoDB include the following;

  1. Single View
  2. The Internet Of Things
  3. Mobile
  4. Real-Time Analytics
  5. Personalization
  6. Catalog
  7. Content Management

To install MongoDB Click here if not installed

Flask

  • Flask is a micro web framework written in Python. It is classified as a microframework because it does not require particular tools or libraries.

If you are new to python and Flask You can get started here with the following articles
first, get to understand python secondly start playing with Flask

Postman

This is a tool that makes the game of building API's fun, its a must-have before even thinking of building an API. Postman is currently one of the most popular tools used in API testing. It started in 2012 as a side project by Abhinav Asthana to simplify API workflow in testing and development.

API stands for Application Programming Interface which allows software applications to communicate with each other via API calls.

More about APIs Click Here

Set up

Install python on your machine if not installed you can check by using the following command python3 --version
check if the virtual environment is installed if not install it Linux for Windows

1.Create Virtual environment

python3 -m venv venv

venv - means the name of the virtual environment it can be anything
Read More

2.Activate Virtual environment

source venv/bin/activate or . venv/bin/activate

2.Installing the following

  1. Flask
    pip install flask

  2. Pymongo
    pip install pymongo

  3. check using pip list

  • Create a file called app.py with the following command Touch app.py
  • paste the following code
from flask import Flask, jsonify, request
from flask_pymongo import PyMongo

app = Flask(__name__)
app.config["MONGO_URI"] = "mongodb://localhost:27017/regdb"
mongo = PyMongo(app)


@app.route('/', methods = ['GET'])
def get_all_attendees():
"""To get all the items from the database.its a Get request and we use the databasename.find()"""
    attendees = mongo.db.attendees
    output = []

    for r in attendees.find():
        output.append({
                    'name':r['name'],
            'address':r['address'],
                    'Phone_Number':r['p_number']
                    })
    return jsonify({'result' : output})

@app.route('/attendee/<name>', methods = ['GET'])
def get_one_attendee(name):
"""To get one item from the database.its a Get request and We use the databasename.find_one"""
    attendees = mongo.db.attendees
    attendees.find_one({'name':name})

    output.append({
                    'name':r['name'],
            'address':r['address'],
                    'Phone_Number':r['p_number']
                    })
    return jsonify({'result' : output})

@app.route('/register', methods = ['POST'])
def register():
"""To add an item to the database. its a post request and we use databasename.insert()"""
    attendees = mongo.db.attendees
    name = request.json['name']
    language=request.json['language']
    reg_id = attendees.insert({
                    'name':r['name'],
            'address':r['address'],
                    'Phone_Number':r['p_number']
                    })
    r = attendees.find_one({'_id':reg_id})
        output = ({
                    'name':r['name'],
            'address':r['address'],
                    'Phone_Number':r['p_number']
                    })
        return jsonify({'result' : output})

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

Go ahead and test your API with Postman

Thanks, Ernest

Top comments (0)