Compare commits
3 Commits
b96ba3eb34
...
lab3
| Author | SHA1 | Date | |
|---|---|---|---|
| a42d94779c | |||
|
bba801f2d2
|
|||
| 32fd20b3ae |
+207
-47
@@ -15,6 +15,7 @@ class UserModel(db.Model):
|
|||||||
__tablename__ = "user"
|
__tablename__ = "user"
|
||||||
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)
|
||||||
|
bal_uuid = db.Column(db.String(32), db.ForeignKey('balance.uuid'))
|
||||||
|
|
||||||
class CategoryModel(db.Model):
|
class CategoryModel(db.Model):
|
||||||
__tablename__ = "category"
|
__tablename__ = "category"
|
||||||
@@ -29,9 +30,16 @@ class RecordModel(db.Model):
|
|||||||
date = db.Column(db.Date)
|
date = db.Column(db.Date)
|
||||||
amount = db.Column(db.Integer)
|
amount = db.Column(db.Integer)
|
||||||
|
|
||||||
|
class BalanceModel(db.Model):
|
||||||
|
__tablename__ = "balance"
|
||||||
|
uuid = db.Column(db.String(32), primary_key=True, nullable=False)
|
||||||
|
value = db.Column(db.Integer, nullable=False)
|
||||||
|
|
||||||
|
|
||||||
class UserSchema(Schema):
|
class UserSchema(Schema):
|
||||||
uuid = fields.Str()
|
uuid = fields.Str()
|
||||||
name = fields.Str()
|
name = fields.Str()
|
||||||
|
bal_uuid = fields.Str()
|
||||||
|
|
||||||
class CategorySchema(Schema):
|
class CategorySchema(Schema):
|
||||||
uuid = fields.Str()
|
uuid = fields.Str()
|
||||||
@@ -44,6 +52,10 @@ class RecordSchema(Schema):
|
|||||||
date = fields.Date()
|
date = fields.Date()
|
||||||
amount = fields.Integer()
|
amount = fields.Integer()
|
||||||
|
|
||||||
|
class BalanceSchema(Schema):
|
||||||
|
uuid = fields.Str()
|
||||||
|
value = fields.Integer()
|
||||||
|
|
||||||
user_schema = UserSchema()
|
user_schema = UserSchema()
|
||||||
users_schema = UserSchema(many = True)
|
users_schema = UserSchema(many = True)
|
||||||
|
|
||||||
@@ -53,6 +65,8 @@ categories_schema = CategorySchema(many = True)
|
|||||||
record_schema = RecordSchema()
|
record_schema = RecordSchema()
|
||||||
records_schema = RecordSchema(many = True)
|
records_schema = RecordSchema(many = True)
|
||||||
|
|
||||||
|
balance_schema = BalanceSchema()
|
||||||
|
|
||||||
# "migration"
|
# "migration"
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
db.create_all()
|
db.create_all()
|
||||||
@@ -66,7 +80,10 @@ 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():
|
||||||
|
db.session.query(RecordModel).delete()
|
||||||
|
db.session.query(CategoryModel).delete()
|
||||||
db.session.query(UserModel).delete()
|
db.session.query(UserModel).delete()
|
||||||
|
db.session.query(BalanceModel).delete()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
return {}, 200
|
return {}, 200
|
||||||
|
|
||||||
@@ -94,7 +111,9 @@ def ep_user_post():
|
|||||||
if 'uuid' in body:
|
if 'uuid' in body:
|
||||||
return {}, 403
|
return {}, 403
|
||||||
|
|
||||||
|
b = BalanceModel(uuid=uuid.uuid4().hex, value=0)
|
||||||
body.update({'uuid': uuid.uuid4().hex})
|
body.update({'uuid': uuid.uuid4().hex})
|
||||||
|
body.update({'bal_uuid': b.uuid})
|
||||||
|
|
||||||
try:
|
try:
|
||||||
_ = user_schema.load(body)
|
_ = user_schema.load(body)
|
||||||
@@ -104,6 +123,7 @@ def ep_user_post():
|
|||||||
u = UserModel(**body)
|
u = UserModel(**body)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
db.session.add(b)
|
||||||
db.session.add(u)
|
db.session.add(u)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@@ -122,8 +142,13 @@ def ep_user_delete(user_id):
|
|||||||
if len(result) == 0:
|
if len(result) == 0:
|
||||||
return {}, 404
|
return {}, 404
|
||||||
|
|
||||||
|
try:
|
||||||
db.session.query(UserModel).filter(UserModel.uuid == user_id).delete()
|
db.session.query(UserModel).filter(UserModel.uuid == user_id).delete()
|
||||||
|
db.session.query(BalanceModel).filter(BalanceModel.uuid == result[0].bal_uuid).delete()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
return user_schema.dumps(result[0]), 200
|
return user_schema.dumps(result[0]), 200
|
||||||
|
|
||||||
@@ -132,12 +157,12 @@ def ep_category_get():
|
|||||||
body = request.json
|
body = request.json
|
||||||
|
|
||||||
if 'uuid' in body:
|
if 'uuid' in body:
|
||||||
category = ldb.get_category(body['uuid'])
|
result = db.session.query(CategoryModel).filter(CategoryModel.uuid == body['uuid']).all()
|
||||||
|
|
||||||
if 'uuid' in category:
|
if len(result) == 1:
|
||||||
return category
|
return user_schema.dumps(result[0]), 200
|
||||||
else:
|
else:
|
||||||
return category, 404
|
return {}, 404
|
||||||
else:
|
else:
|
||||||
return {}, 403
|
return {}, 403
|
||||||
|
|
||||||
@@ -145,81 +170,216 @@ def ep_category_get():
|
|||||||
def ep_category_post():
|
def ep_category_post():
|
||||||
body = request.json
|
body = request.json
|
||||||
|
|
||||||
if 'name' in body:
|
if not body:
|
||||||
r = ldb.add_category(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:
|
||||||
|
_ = category_schema.load(body)
|
||||||
|
except ValidationError as e:
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
|
c = CategoryModel(**body)
|
||||||
|
|
||||||
|
try:
|
||||||
|
db.session.add(c)
|
||||||
|
db.session.commit()
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
|
return jsonify(category_schema.load(body)), 200
|
||||||
|
|
||||||
@app.route("/category", methods = ["DELETE"])
|
@app.route("/category", methods = ["DELETE"])
|
||||||
def ep_category_delete():
|
def ep_category_delete():
|
||||||
body = request.json
|
body = request.json
|
||||||
|
|
||||||
if 'uuid' in body:
|
if 'uuid' not in body:
|
||||||
category = ldb.del_category(body['uuid'])
|
|
||||||
|
|
||||||
if 'uuid' in category:
|
|
||||||
return category
|
|
||||||
else:
|
|
||||||
return category, 404
|
|
||||||
else:
|
|
||||||
return {}, 403
|
return {}, 403
|
||||||
|
|
||||||
|
cat_id = body['uuid']
|
||||||
|
|
||||||
|
try:
|
||||||
|
result = db.session.query(CategoryModel).filter(CategoryModel.uuid == cat_id).all()
|
||||||
|
except Exception as e:
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
|
if len(result) == 0:
|
||||||
|
return {}, 404
|
||||||
|
|
||||||
|
try:
|
||||||
|
db.session.query(CategoryModel).filter(CategoryModel.uuid == cat_id).delete()
|
||||||
|
db.session.commit()
|
||||||
|
except Exception as e:
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
|
return category_schema.dumps(result[0]), 200
|
||||||
|
|
||||||
@app.route("/record/<record_id>", methods = ["GET"])
|
@app.route("/record/<record_id>", methods = ["GET"])
|
||||||
def ep_record_get(record_id):
|
def ep_record_get(record_id):
|
||||||
r = ldb.get_record(record_id)
|
result = db.session.query(RecordModel).filter(RecordModel.uuid == record_id).all()
|
||||||
|
|
||||||
if 'uuid' in r:
|
if len(result) == 1:
|
||||||
return r
|
return user_schema.dumps(result[0]), 200
|
||||||
else:
|
else:
|
||||||
return r, 404
|
return {}, 404
|
||||||
|
|
||||||
@app.route("/record", methods = ["GET"])
|
@app.route("/record", methods = ["GET"])
|
||||||
def ep_record_get_filtered():
|
def ep_record_get_filtered():
|
||||||
options = {}
|
r = db.session.query(RecordModel)
|
||||||
|
|
||||||
|
filtered = False
|
||||||
|
|
||||||
if 'user_id' in request.json:
|
if 'user_id' in request.json:
|
||||||
options['user_id'] = request.json['user_id']
|
r = r.filter(RecordModel.user_uuid == request.json['user_id'])
|
||||||
|
filtered = True
|
||||||
|
elif 'user_uuid' in request.json:
|
||||||
|
r = r.filter(RecordModel.user_uuid == request.json['user_uuid'])
|
||||||
|
filtered = True
|
||||||
|
|
||||||
if 'cat_id' in request.json:
|
if 'cat_id' in request.json:
|
||||||
options['cat_id'] = request.json['cat_id']
|
r = r.filter(RecordModel.cat_uuid == request.json['cat_id'])
|
||||||
|
filtered = True
|
||||||
|
if 'cat_uuid' in request.json:
|
||||||
|
r = r.filter(RecordModel.cat_uuid == request.json['cat_uuid'])
|
||||||
|
filtered = True
|
||||||
|
|
||||||
if len(list(options.keys())) == 0:
|
|
||||||
return [], 400
|
|
||||||
|
|
||||||
r = ldb.filter_records(options)
|
if filtered:
|
||||||
|
return records_schema.dumps(r.all())
|
||||||
return json.dumps(r)
|
else:
|
||||||
|
return [], 403
|
||||||
|
|
||||||
@app.route("/record/<record_id>", methods = ["DELETE"])
|
@app.route("/record/<record_id>", methods = ["DELETE"])
|
||||||
def ep_record_del(record_id):
|
def ep_record_del(record_id):
|
||||||
r = ldb.del_record(record_id)
|
try:
|
||||||
|
result = db.session.query(RecordModel).filter(RecordModel.uuid == record_id).all()
|
||||||
|
except Exception as e:
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
if 'uuid' in r:
|
if len(result) == 0:
|
||||||
return r
|
return {}, 404
|
||||||
else:
|
|
||||||
return r, 404
|
db.session.query(RecordModel).filter(RecordModel.uuid == record_id).delete()
|
||||||
|
db.session.commit()
|
||||||
|
|
||||||
|
return record_schema.dumps(result[0]), 200
|
||||||
|
|
||||||
@app.route("/record", methods = ["POST"])
|
@app.route("/record", methods = ["POST"])
|
||||||
def ep_record_post():
|
def ep_record_post():
|
||||||
body = request.json
|
body = request.json
|
||||||
|
|
||||||
if 'user_id' not in body:
|
if not body:
|
||||||
return {}, 400
|
return {}, 403
|
||||||
|
|
||||||
if 'cat_id' not in body:
|
if 'uuid' in body:
|
||||||
return {}, 400
|
return {}, 403
|
||||||
|
|
||||||
if 'amount' not in body:
|
body.update({'uuid': uuid.uuid4().hex})
|
||||||
return {}, 400
|
|
||||||
|
|
||||||
r = ldb.add_record(body['user_id'], body['cat_id'], body['amount'])
|
# backward compatibility with lab2 DB model
|
||||||
|
if 'cat_id' in body:
|
||||||
|
body.update({'cat_uuid': body['cat_id']})
|
||||||
|
del body['cat_id']
|
||||||
|
|
||||||
if 'uuid' in r:
|
if 'user_id' in body:
|
||||||
return r
|
body.update({'user_uuid': body['user_id']})
|
||||||
|
del body['user_id']
|
||||||
|
|
||||||
|
try:
|
||||||
|
_ = record_schema.load(body)
|
||||||
|
except Exception as e:
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
|
r = RecordModel(**body)
|
||||||
|
|
||||||
|
b_id = db.session \
|
||||||
|
.query(UserModel) \
|
||||||
|
.filter(UserModel.uuid == body['user_uuid']) \
|
||||||
|
.all()[0] \
|
||||||
|
.bal_uuid
|
||||||
|
|
||||||
|
v = db.session \
|
||||||
|
.query(BalanceModel) \
|
||||||
|
.filter(BalanceModel.uuid == b_id) \
|
||||||
|
.all()[0] \
|
||||||
|
.value
|
||||||
|
|
||||||
|
BalanceModel.metadata.tables.get("balance").update().where(BalanceModel.metadata.tables.get("balance").c.uuid == b_id).values(value = v-body['amount'])
|
||||||
|
|
||||||
|
try:
|
||||||
|
db.session.add(r)
|
||||||
|
db.session.commit()
|
||||||
|
except Exception as e:
|
||||||
|
db.session.rollback()
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
|
|
||||||
|
return jsonify(record_schema.load(body)), 200
|
||||||
|
|
||||||
|
@app.route("/balance_up", methods = ["POST"])
|
||||||
|
def ep_balance_up():
|
||||||
|
body = request.json
|
||||||
|
|
||||||
|
if 'user_id' in body:
|
||||||
|
body.update({'user_uuid': body['user_id']})
|
||||||
|
del body['user_id']
|
||||||
|
elif 'uuid' in body:
|
||||||
|
body.update({'user_uuid': body['uuid']})
|
||||||
|
del body['uuid']
|
||||||
|
|
||||||
|
if 'user_uuid' not in body:
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
|
try:
|
||||||
|
b_id = db.session \
|
||||||
|
.query(UserModel) \
|
||||||
|
.filter(UserModel.uuid == body['user_uuid']) \
|
||||||
|
.all()[0] \
|
||||||
|
.bal_uuid
|
||||||
|
|
||||||
|
v = db.session \
|
||||||
|
.query(BalanceModel) \
|
||||||
|
.filter(BalanceModel.uuid == b_id) \
|
||||||
|
.all()[0] \
|
||||||
|
.value
|
||||||
|
|
||||||
|
BalanceModel.metadata.tables.get("balance").update().where(BalanceModel.metadata.tables.get("balance").c.uuid == b_id).values(value = v + body['amount'])
|
||||||
|
except Exception as e:
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
|
return {}, 200
|
||||||
|
|
||||||
|
@app.route("/balance", methods = ["GET"])
|
||||||
|
def ep_balance_get():
|
||||||
|
body = request.json
|
||||||
|
|
||||||
|
if 'user_id' in body:
|
||||||
|
body.update({'user_uuid': body['user_id']})
|
||||||
|
del body['user_id']
|
||||||
|
elif 'uuid' in body:
|
||||||
|
body.update({'user_uuid': body['uuid']})
|
||||||
|
del body['uuid']
|
||||||
|
|
||||||
|
if 'user_uuid' not in body:
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
|
try:
|
||||||
|
b_id = db.session \
|
||||||
|
.query(UserModel) \
|
||||||
|
.filter(UserModel.uuid == body['user_uuid']) \
|
||||||
|
.all()[0] \
|
||||||
|
.bal_uuid
|
||||||
|
|
||||||
|
result = db.session.query(BalanceModel).filter(BalanceModel.uuid == b_id).all()
|
||||||
|
except Exception as e:
|
||||||
|
return {}, 403
|
||||||
|
|
||||||
|
if len(result) == 1:
|
||||||
|
return user_schema.dumps(result[0]), 200
|
||||||
else:
|
else:
|
||||||
return r, 403
|
return {}, 404
|
||||||
|
|||||||
Reference in New Issue
Block a user