Compare commits

..

No commits in common. "1ad5a7587b74d531f09da47e1922e0da52a65d15" and "3eacc59ef9fadbac25f277bc8ee85b15ef4c71ee" have entirely different histories.

5 changed files with 3 additions and 307 deletions

1
.gitignore vendored
View File

@ -1 +0,0 @@
__pycache__/

View File

@ -9,4 +9,6 @@ COPY ./requirements.txt /app/requirements.txt
RUN python -m pip install -r requirements.txt RUN python -m pip install -r requirements.txt
COPY ./app/ /app/app/
CMD flask run -h 0.0.0.0 -p $PORT CMD flask run -h 0.0.0.0 -p $PORT

View File

@ -1,10 +1,7 @@
from flask import Flask, request from flask import Flask
import time import time
import json
from app.local_db import LocalDB
app = Flask(__name__) app = Flask(__name__)
ldb = LocalDB()
@app.route("/healthcheck") @app.route("/healthcheck")
def ep_healthcheck(): def ep_healthcheck():
@ -12,141 +9,3 @@ def ep_healthcheck():
"date": time.strftime('%Y.%m.%d %H:%M:%S'), "date": time.strftime('%Y.%m.%d %H:%M:%S'),
"status": "OK" "status": "OK"
} }
@app.route("/reset_users_because_postman_is_dumb_like_that")
def ep_reset():
ldb.reset()
return {}
@app.route("/users", methods = ["GET"])
def ep_users_get():
return ldb.get_users()
@app.route("/user/<user_id>", methods = ["GET"])
def ep_user_get(user_id):
user = ldb.get_user(user_id)
if 'uuid' in user:
return user
else:
return user, 404
@app.route("/user", methods = ["POST"])
def ep_user_post():
body = request.json
if 'name' in body:
r = ldb.add_user(body['name'])
if 'uuid' in r:
return r
else:
return r, 403
else:
return {}, 403
@app.route("/user/<user_id>", methods = ["DELETE"])
def ep_user_delete(user_id):
r = ldb.del_user(user_id)
if 'uuid' in r:
return r
else:
return r, 403
@app.route("/category", methods = ["GET"])
def ep_category_get():
body = request.json
if 'uuid' in body:
category = ldb.get_category(body['uuid'])
if 'uuid' in category:
return category
else:
return category, 404
else:
return {}, 403
@app.route("/category", methods = ["POST"])
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:
return {}, 403
@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:
return {}, 403
@app.route("/record/<record_id>", methods = ["GET"])
def ep_record_get(record_id):
r = ldb.get_record(record_id)
if 'uuid' in r:
return r
else:
return r, 404
@app.route("/record", methods = ["GET"])
def ep_record_get_filtered():
options = {}
if 'user_id' in request.json:
options['user_id'] = request.json['user_id']
if 'cat_id' in request.json:
options['cat_id'] = request.json['cat_id']
if len(list(options.keys())) == 0:
return [], 400
r = ldb.filter_records(options)
return json.dumps(r)
@app.route("/record/<record_id>", methods = ["DELETE"])
def ep_record_del(record_id):
r = ldb.del_record(record_id)
if 'uuid' in r:
return r
else:
return r, 404
@app.route("/record", methods = ["POST"])
def ep_record_post():
body = request.json
if 'user_id' not in body:
return {}, 400
if 'cat_id' not in body:
return {}, 400
if 'amount' not in body:
return {}, 400
r = ldb.add_record(body['user_id'], body['cat_id'], body['amount'])
if 'uuid' in r:
return r
else:
return r, 403

View File

@ -1,162 +0,0 @@
import uuid
import time
class LocalDB:
def __init__(self):
self.users = {}
self.categories = {}
self.records = {}
def reset(self):
for k, v in list(self.users.items()):
if v == "test1":
del self.users[k]
for k, v in list(self.categories.items()):
if v == "test2":
del self.categories[k]
def get_users(self):
return self.users
def get_user(self, uid):
if uid in self.users:
return {
'uuid': uid,
'name': self.users[uid]
}
else:
return {}
def add_user(self, name):
if not name:
return {}
if name in set(self.users.values()):
return {}
new_uuid = uuid.uuid4().hex
self.users[new_uuid] = name
return {
'uuid': new_uuid,
'name': name
}
def del_user(self, uid):
if uid in self.users:
name = self.users[uid]
del self.users[uid]
return {
'uuid': uid,
'name': name
}
else:
return {}
def get_category(self, uid):
if uid in self.categories:
return {
'uuid': uid,
'name': self.categories[uid]
}
else:
return {}
def add_category(self, name):
if not name:
return {}
if name in set(self.categories.values()):
return {}
new_uuid = uuid.uuid4().hex
self.categories[new_uuid] = name
return {
'uuid': new_uuid,
'name': name
}
def del_category(self, uid):
if uid in self.categories:
name = self.categories[uid]
del self.categories[uid]
return {
'uuid': uid,
'name': name
}
else:
return {}
def get_record(self, uid):
if uid in self.records:
r = self.records[uid]
return {
'uuid': uid,
'user_id': r['user_id'],
'cat_id': r['cat_id'],
'date': r['date'],
'amount': r['amount']
}
else:
return {}
def filter_records(self, filters):
results = []
for k, v in self.records.items():
if 'user_id' in filters and v['user_id'] != filters['user_id']:
continue
if 'cat_id' in filters and v['cat_id'] != filters['cat_id']:
continue
results.append({
'uuid': k,
'user_id': v['user_id'],
'cat_id': v['cat_id'],
'date': v['date'],
'amount': v['amount']
})
return results
def add_record(self, user_id, cat_id, amount):
if user_id not in self.users:
return {}
if cat_id not in self.categories:
return {}
new_uuid = uuid.uuid4().hex
self.records[new_uuid] = {
'user_id': user_id,
'cat_id': cat_id,
'date': time.time(),
'amount': amount
}
return {
'uuid': new_uuid,
'user_id': user_id,
'cat_id': cat_id,
'date': time.time(),
'amount': amount
}
def del_record(self, uid):
if uid in self.records:
r = self.records[uid]
del self.records[uid]
return {
'uuid': uid,
'user_id': r['user_id'],
'cat_id': r['cat_id'],
'date': r['date'],
'amount': r['amount']
}
else:
return {}

View File

@ -10,5 +10,3 @@ services:
PORT: "12402" PORT: "12402"
ports: ports:
- "12402:12402" - "12402:12402"
volumes:
- ./app:/app/app