migrate post user and get users api

This commit is contained in:
ІО-23 Шмуляр Олег 2024-12-27 23:23:21 +02:00
parent 9f5c2505de
commit 7ef08e64c2
1 changed files with 36 additions and 17 deletions

View File

@ -1,7 +1,7 @@
from flask import Flask, request from flask import Flask, request, jsonify
import time import time
import json import json
from app.local_db import LocalDB import uuid
from marshmallow import Schema, fields from marshmallow import Schema, fields
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
@ -16,16 +16,12 @@ class UserModel(db.Model):
uuid = db.Column(db.String(32), unique=True, primary_key=True, nullable=False) uuid = db.Column(db.String(32), unique=True, primary_key=True, nullable=False)
name = db.Column(db.String(64), nullable=False) name = db.Column(db.String(64), nullable=False)
records = db.relationship('RecordSchema', backref='user.uuid')
class CategoryModel(db.Model): class CategoryModel(db.Model):
__tablename__ = "category" __tablename__ = "category"
uuid = db.Column(db.String(32), unique=True, primary_key=True, nullable=False) uuid = db.Column(db.String(32), unique=True, primary_key=True, nullable=False)
name = db.Column(db.String(64), nullable=False) name = db.Column(db.String(64), nullable=False)
records = db.relationship('RecordSchema', backref='category.uuid') class RecordModel(db.Model):
class RecordSchema(db.Model):
__tablename__ = "record" __tablename__ = "record"
uuid = db.Column(db.String(32), primary_key=True, nullable=False) uuid = db.Column(db.String(32), primary_key=True, nullable=False)
user_uuid = db.Column(db.String(32), db.ForeignKey('user.uuid')) user_uuid = db.Column(db.String(32), db.ForeignKey('user.uuid'))
@ -48,6 +44,15 @@ class RecordSchema(Schema):
date = fields.Date() date = fields.Date()
amount = fields.Integer() amount = fields.Integer()
user_schema = UserSchema()
users_schema = UserSchema(many = True)
category_schema = CategorySchema()
categories_schema = CategorySchema(many = True)
record_schema = RecordSchema()
records_schema = RecordSchema(many = True)
# "migration" # "migration"
with app.app_context(): with app.app_context():
db.create_all() db.create_all()
@ -61,12 +66,12 @@ def ep_healthcheck():
@app.route("/reset_users_because_postman_is_dumb_like_that") @app.route("/reset_users_because_postman_is_dumb_like_that")
def ep_reset(): def ep_reset():
ldb.reset()
return {} return {}
@app.route("/users", methods = ["GET"]) @app.route("/users", methods = ["GET"])
def ep_users_get(): def ep_users_get():
return ldb.get_users() result = db.session.query(UserModel).all()
return users_schema.dumps(result)
@app.route("/user/<user_id>", methods = ["GET"]) @app.route("/user/<user_id>", methods = ["GET"])
def ep_user_get(user_id): def ep_user_get(user_id):
@ -81,16 +86,30 @@ def ep_user_get(user_id):
def ep_user_post(): def ep_user_post():
body = request.json body = request.json
if 'name' in body: if not body:
r = ldb.add_user(body['name'])
if 'uuid' in r:
return r
else:
return r, 403
else:
return {}, 403 return {}, 403
if 'uuid' in body:
return {}, 403
body.update({'uuid': uuid.uuid4().hex})
try:
_ = user_schema.load(body)
except ValidationError as e:
return {}, 403
u = UserModel(**body)
try:
db.session.add(u)
db.session.commit()
except Exception as e:
db.session.rollback()
return {}, 403
return jsonify(user_schema.load(body)), 200
@app.route("/user/<user_id>", methods = ["DELETE"]) @app.route("/user/<user_id>", methods = ["DELETE"])
def ep_user_delete(user_id): def ep_user_delete(user_id):
r = ldb.del_user(user_id) r = ldb.del_user(user_id)