Compare commits

..

6 Commits

Author SHA1 Message Date
ІО-23 Шмуляр Олег 1ad5a7587b
add record-related endpoints 2024-10-28 20:10:04 +02:00
ІО-23 Шмуляр Олег e6954e5710
add category-related endpoints 2024-10-28 18:18:28 +02:00
ІО-23 Шмуляр Олег 5d9da36bc5
add workaround for postman flow
Add endpoint specifically designed to make sure postman flow will
execute correctly on any run regardless of what previous changes have
been done to the server database.
2024-10-28 17:27:34 +02:00
ІО-23 Шмуляр Олег 3390b2e374
add user-related endpoints 2024-10-28 17:19:47 +02:00
ІО-23 Шмуляр Олег 8b7e93bd67
use volumes instead of copying application files
This change drastically reduces the amount of rebuilding required during
application development and debugging stages.
2024-10-28 14:08:23 +02:00
ІО-23 Шмуляр Олег fc6775ed2e
set __pycache__/ directories to be ignored by git 2024-10-28 14:05:30 +02:00
5 changed files with 307 additions and 3 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
__pycache__/

View File

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

View File

@ -1,7 +1,10 @@
from flask import Flask
from flask import Flask, request
import time
import json
from app.local_db import LocalDB
app = Flask(__name__)
ldb = LocalDB()
@app.route("/healthcheck")
def ep_healthcheck():
@ -9,3 +12,141 @@ def ep_healthcheck():
"date": time.strftime('%Y.%m.%d %H:%M:%S'),
"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

162
app/local_db.py Normal file
View File

@ -0,0 +1,162 @@
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,3 +10,5 @@ services:
PORT: "12402"
ports:
- "12402:12402"
volumes:
- ./app:/app/app