migrate user, category and record API to db

This commit is contained in:
ІО-23 Шмуляр Олег 2024-12-28 15:10:29 +02:00
parent b96ba3eb34
commit 32fd20b3ae
1 changed files with 105 additions and 48 deletions

View File

@ -66,6 +66,8 @@ def ep_healthcheck():
@app.route("/reset_users_because_postman_is_dumb_like_that")
def ep_reset():
db.session.query(RecordModel).delete()
db.session.query(CategoryModel).delete()
db.session.query(UserModel).delete()
db.session.commit()
return {}, 200
@ -132,12 +134,12 @@ def ep_category_get():
body = request.json
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:
return category
if len(result) == 1:
return user_schema.dumps(result[0]), 200
else:
return category, 404
return {}, 404
else:
return {}, 403
@ -145,81 +147,136 @@ def ep_category_get():
def ep_category_post():
body = request.json
if 'name' in body:
r = ldb.add_category(body['name'])
if 'uuid' in r:
return r
else:
return r, 403
else:
if not body:
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"])
def ep_category_delete():
body = request.json
if 'uuid' in body:
category = ldb.del_category(body['uuid'])
if 'uuid' in category:
return category
else:
return category, 404
else:
if 'uuid' not in body:
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
db.session.query(CategoryModel).filter(CategoryModel.uuid == cat_id).delete()
db.session.commit()
return category_schema.dumps(result[0]), 200
@app.route("/record/<record_id>", methods = ["GET"])
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:
return r
if len(result) == 1:
return user_schema.dumps(result[0]), 200
else:
return r, 404
return {}, 404
@app.route("/record", methods = ["GET"])
def ep_record_get_filtered():
options = {}
r = db.session.query(RecordModel)
filtered = False
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:
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)
return json.dumps(r)
if filtered:
return records_schema.dumps(r.all())
else:
return [], 403
@app.route("/record/<record_id>", methods = ["DELETE"])
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:
return r
else:
return r, 404
if len(result) == 0:
return {}, 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"])
def ep_record_post():
body = request.json
if 'user_id' not in body:
return {}, 400
if not body:
return {}, 403
if 'cat_id' not in body:
return {}, 400
if 'uuid' in body:
return {}, 403
if 'amount' not in body:
return {}, 400
body.update({'uuid': uuid.uuid4().hex})
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:
return r
else:
return r, 403
if 'user_id' in body:
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)
try:
db.session.add(r)
db.session.commit()
except Exception as e:
db.session.rollback()
return {}, 403
return jsonify(record_schema.load(body)), 200