add record-related endpoints
This commit is contained in:
		
							parent
							
								
									e6954e5710
								
							
						
					
					
						commit
						1ad5a7587b
					
				@ -1,5 +1,6 @@
 | 
			
		||||
from flask import Flask, request
 | 
			
		||||
import time
 | 
			
		||||
import json
 | 
			
		||||
from app.local_db import LocalDB
 | 
			
		||||
 | 
			
		||||
app = Flask(__name__)
 | 
			
		||||
@ -94,3 +95,58 @@ def ep_category_delete():
 | 
			
		||||
            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
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
import uuid
 | 
			
		||||
import time
 | 
			
		||||
 | 
			
		||||
class LocalDB:
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
@ -85,3 +86,77 @@ class LocalDB:
 | 
			
		||||
            }
 | 
			
		||||
        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 {}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user