diff --git a/app/__init__.py b/app/__init__.py index 4dd679e..f1374e0 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -3,8 +3,54 @@ import time import json from app.local_db import LocalDB +from marshmallow import Schema, fields +from flask_sqlalchemy import SQLAlchemy + app = Flask(__name__) -ldb = LocalDB() +app.config.from_pyfile('config.py', silent=True) + +db = SQLAlchemy(app) + +class UserModel(db.Model): + __tablename__ = "user" + uuid = db.Column(db.String(32), unique=True, primary_key=True, nullable=False) + name = db.Column(db.String(64), nullable=False) + + records = db.relationship('RecordSchema', backref='user.uuid') + +class CategoryModel(db.Model): + __tablename__ = "category" + uuid = db.Column(db.String(32), unique=True, primary_key=True, nullable=False) + name = db.Column(db.String(64), nullable=False) + + records = db.relationship('RecordSchema', backref='category.uuid') + +class RecordSchema(db.Model): + __tablename__ = "record" + uuid = db.Column(db.String(32), primary_key=True, nullable=False) + user_uuid = db.Column(db.String(32), db.ForeignKey('user.uuid')) + cat_uuid = db.Column(db.String(32), db.ForeignKey('category.uuid')) + date = db.Column(db.Date) + amount = db.Column(db.Integer) + +class UserSchema(Schema): + uuid = fields.Str() + name = fields.Str() + +class CategorySchema(Schema): + uuid = fields.Str() + name = fields.Str() + +class RecordSchema(Schema): + uuid = fields.Str() + user_uuid = fields.Str() + cat_uuid = fields.Str() + date = fields.Date() + amount = fields.Integer() + +# "migration" +with app.app_context(): + db.create_all() @app.route("/healthcheck") def ep_healthcheck(): diff --git a/app/config.py b/app/config.py new file mode 100644 index 0000000..a4051e4 --- /dev/null +++ b/app/config.py @@ -0,0 +1,7 @@ +import os + +PROPAGATE_EXCEPTIONS = True +SQLALCHEMY_DATABASE_URI = f'postgresql://{os.environ["POSTGRES_USER"]}:{os.environ["POSTGRES_PASSWORD"]}@db:5432/accountance' +SQLALCHEMY_TRACK_MODIFICATIONS = False +API_TITLE = "Finance REST API" +API_VERSION = 'v1'