|
| 1 | +from flask import Flask, request, jsonify |
| 2 | +from flask_sqlalchemy import SQLAlchemy |
| 3 | +from flask_marshmallow import Marshmallow |
| 4 | +import os |
| 5 | + |
| 6 | + |
| 7 | +# app init |
| 8 | +app = Flask(__name__) |
| 9 | +basedir = os.path.abspath(os.path.dirname(__file__)) |
| 10 | + |
| 11 | +# Database |
| 12 | +app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///" + os.path.join( |
| 13 | + basedir, "db.sqlite" |
| 14 | + ) |
| 15 | +app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False |
| 16 | + |
| 17 | +# Init db |
| 18 | +db = SQLAlchemy(app) |
| 19 | +# Init ma |
| 20 | +ma = Marshmallow(app) |
| 21 | + |
| 22 | + |
| 23 | +# Product Class/Model |
| 24 | +class Product(db.Model): |
| 25 | + id = db.Column(db.Integer, primary_key=True) |
| 26 | + name = db.Column(db.String(100), unique=True) |
| 27 | + description = db.Column(db.String(200)) |
| 28 | + price = db.Column(db.Float) |
| 29 | + quantity = db.Column(db.Integer) |
| 30 | + |
| 31 | + def __init__(self, name, description, price, quantity): |
| 32 | + self.name = name |
| 33 | + self.description = description |
| 34 | + self.price = price |
| 35 | + self.quantity = quantity |
| 36 | + |
| 37 | + |
| 38 | +# Product Schema |
| 39 | +class ProductSchema(ma.Schema): |
| 40 | + class Meta: |
| 41 | + fields = ("id", "name", "description", "price", "quantity") |
| 42 | + |
| 43 | + |
| 44 | +# Init Schema |
| 45 | +product_schema = ProductSchema() |
| 46 | +products_schema = ProductSchema(many=True) |
| 47 | + |
| 48 | +# Create the Tables |
| 49 | +with app.app_context(): |
| 50 | + db.create_all() |
| 51 | + |
| 52 | + |
| 53 | +@app.route("/", methods=["GET"]) |
| 54 | +def get(): |
| 55 | + return jsonify({ |
| 56 | + 'Url Routes':{ |
| 57 | + "home-GET": "/", |
| 58 | + "get all product-GET": "Product", |
| 59 | + "add a product-POST": "Product", |
| 60 | + "get a product detail-GET":"Product/id", |
| 61 | + "edit a product detail-PUT":"Product/id", |
| 62 | + "delete a product-DELETE":"Product/id" |
| 63 | + }, |
| 64 | + }) |
| 65 | + |
| 66 | + |
| 67 | +# add product |
| 68 | +@app.route("/product", methods=["POST"]) |
| 69 | +def add_product(): |
| 70 | + name = request.json["name"] |
| 71 | + description = request.json["description"] |
| 72 | + price = request.json["price"] |
| 73 | + quantity = request.json["quantity"] |
| 74 | + |
| 75 | + new_product = Product(name, description, price, quantity) |
| 76 | + db.session.add(new_product) |
| 77 | + db.session.commit() |
| 78 | + |
| 79 | + return product_schema.jsonify(new_product) |
| 80 | + |
| 81 | + |
| 82 | +# get all product |
| 83 | +@app.route("/product", methods=["GET"]) |
| 84 | +def get_products(): |
| 85 | + products = Product.query.all() |
| 86 | + result = products_schema.dump(products) |
| 87 | + return result |
| 88 | + |
| 89 | + |
| 90 | +# get single product |
| 91 | +@app.route("/product/<int:id>", methods=["GET"]) |
| 92 | +def get_product(id): |
| 93 | + product = Product.query.get(id) |
| 94 | + result = product_schema.dump(product) |
| 95 | + return result |
| 96 | + |
| 97 | + |
| 98 | +# edit a product |
| 99 | +@app.route("/product/<int:id>", methods=["PUT"]) |
| 100 | +def edit_product(id): |
| 101 | + product = Product.query.get(id) |
| 102 | + |
| 103 | + name = request.json["name"] |
| 104 | + description = request.json["description"] |
| 105 | + price = request.json["price"] |
| 106 | + quantity = request.json["quantity"] |
| 107 | + |
| 108 | + product.name = name |
| 109 | + product.price = price |
| 110 | + product.description = description |
| 111 | + product.quantity = quantity |
| 112 | + |
| 113 | + db.session.commit() |
| 114 | + |
| 115 | + return product_schema.jsonify(product) |
| 116 | + |
| 117 | + |
| 118 | +# delete a product |
| 119 | +@app.route("/product/<int:id>", methods=["DELETE"]) |
| 120 | +def delete_product(id): |
| 121 | + product = Product.query.get(id) |
| 122 | + db.session.delete(product) |
| 123 | + db.session.commit() |
| 124 | + return {"msg": "product deleted"} |
| 125 | + |
| 126 | + |
| 127 | +# Run Server |
| 128 | +if __name__ == "__main__": |
| 129 | + app.run(debug=True) |
0 commit comments