Created
July 26, 2022 13:19
-
-
Save jaydeepkarale/4ade2d044a30847ca8f35772116cb814 to your computer and use it in GitHub Desktop.
Flask CRUD App In 60 Secodnds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, Response, jsonify | |
from flask_sqlalchemy import SQLAlchemy | |
app = Flask(__name__) | |
db = SQLAlchemy(app) | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' | |
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | |
class Todo(db.Model): | |
__tablename__ = 'todo' | |
id = db.Column(db.Integer, primary_key=True) | |
action = db.Column(db.String(80), nullable=False) | |
status = db.Column(db.String(20), nullable=False) | |
def json(self): | |
return { | |
'id': self.id, | |
'action': self.action, | |
'status': self.status | |
} | |
def add_action(action, status): | |
new_action = Todo(action=action, status=status) | |
db.session.add(new_action) | |
db.session.commit() | |
def get_all_actions(): | |
return [ | |
Todo.json(action) for action in Todo.query.all() | |
] | |
def get_action(id): | |
return [Todo.json(Todo.query.filter_by(id=id).first())] | |
def update_action(id, action, status): | |
action_to_update = Todo.query.filter_by(id=id).first() | |
action_to_update.action = action | |
action_to_update.status = status | |
db.session.commit() | |
def delete_action(id): | |
Todo.query.filter_by(id=id).delete() | |
db.session.commit() | |
@app.before_first_request | |
def create_db(): | |
db.create_all() | |
# creation todo action | |
@app.route('/addaction', methods=['POST']) | |
def add_action(): | |
request_data = request.get_json() | |
Todo.add_action( | |
action=request_data["action"], | |
status=request_data["status"] | |
) | |
response = Response( | |
"Action Added", | |
status=201, | |
mimetype='application/json' | |
) | |
return response | |
# READ ACTIONS | |
@app.route('/actions', methods=['GET']) | |
def get_actions(): | |
return jsonify({'Actions': Todo.get_all_actions()}) | |
@app.route('/action/<int:id>', methods=['GET']) | |
def get_action_by_action_id(id): | |
return jsonify(Todo.get_action(id=id)) | |
# UPDATE ACTION | |
@app.route('/updateaction/<int:id>', methods=['PUT']) | |
def update_action(id): | |
request_data = request.get_json() | |
Todo.update_action( | |
id=id, | |
action=request_data["action"], | |
status=request_data["status"] | |
) | |
response = Response( | |
"Action Updated", | |
status=202, | |
mimetype='application/json' | |
) | |
return response | |
# DELETE ACTION | |
@app.route('/deleteaction/<int:id>', methods=['DELETE']) | |
def delete_action(id): | |
Todo.delete_action(id=id) | |
response = Response( | |
"Action Deleted", | |
status=200, | |
mimetype='application/json' | |
) | |
return response | |
if __name__ == "__main__": | |
app.run(port=8080, debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment