2021-08-23 06:14:40 +00:00
|
|
|
from datetime import datetime
|
2021-09-12 17:13:43 +00:00
|
|
|
from functools import wraps
|
2021-08-23 06:14:40 +00:00
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from fastapi.security.api_key import APIKey
|
|
|
|
|
|
|
|
from .security import get_api_key
|
|
|
|
from core.tracker import Tracker
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
|
|
|
2021-09-12 17:13:43 +00:00
|
|
|
def check_name(router_fn):
|
|
|
|
@wraps(router_fn)
|
|
|
|
def _check_name_fn(name: str, *args, **kwargs):
|
|
|
|
name += f"-{kwargs['access_token']}"
|
|
|
|
return router_fn(name, *args, **kwargs)
|
|
|
|
return _check_name_fn
|
|
|
|
|
|
|
|
|
2021-08-23 06:14:40 +00:00
|
|
|
@router.get("/tracker/points")
|
2021-09-12 17:13:43 +00:00
|
|
|
@check_name
|
|
|
|
def list_points(name: str, start_date=None, end_date=None, access_token: APIKey = Depends(get_api_key)):
|
|
|
|
if start_date is not None:
|
|
|
|
start_date = datetime.fromisoformat(start_date)
|
|
|
|
if end_date is not None:
|
|
|
|
end_date = datetime.fromisoformat(end_date)
|
2021-08-23 06:14:40 +00:00
|
|
|
try:
|
|
|
|
tracker = Tracker.from_data(name)
|
2021-09-12 17:13:43 +00:00
|
|
|
return tracker.list_points(start_date, end_date)
|
2021-08-23 06:14:40 +00:00
|
|
|
except FileNotFoundError:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/tracker/points/add")
|
2021-09-12 17:13:43 +00:00
|
|
|
@check_name
|
2021-08-25 03:10:46 +00:00
|
|
|
def add_point(name: str, value: int, date_str: str = None, access_token: APIKey = Depends(get_api_key)):
|
|
|
|
if date_str is None:
|
|
|
|
date_str = datetime.now().isoformat()
|
2021-08-23 06:14:40 +00:00
|
|
|
try:
|
|
|
|
tracker = Tracker.from_data(name)
|
|
|
|
except FileNotFoundError:
|
|
|
|
tracker = Tracker(name, [])
|
|
|
|
tracker.add_point(date_str, value)
|
2021-09-12 17:13:43 +00:00
|
|
|
return tracker.list_points()
|
2021-08-23 06:14:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
@router.put("/tracker/points/modify")
|
2021-09-12 17:13:43 +00:00
|
|
|
@check_name
|
2021-08-23 06:14:40 +00:00
|
|
|
def modify_point(name: str, date_str: str, value: int, access_token: APIKey = Depends(get_api_key)):
|
|
|
|
tracker = Tracker.from_data(name)
|
|
|
|
tracker.modify_point(date_str, value)
|
|
|
|
return 200
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/tracker/points/delete")
|
2021-09-12 17:13:43 +00:00
|
|
|
@check_name
|
2021-08-23 06:14:40 +00:00
|
|
|
def delete_point(name: str, date_str: str, access_token: APIKey = Depends(get_api_key)):
|
|
|
|
tracker = Tracker.from_data(name)
|
|
|
|
tracker.delete_point(date_str)
|
|
|
|
return 200
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/tracker/rename")
|
2021-09-12 17:13:43 +00:00
|
|
|
@check_name
|
2021-08-23 06:14:40 +00:00
|
|
|
def rename_tracker(name: str, new_name: str, access_token: APIKey = Depends(get_api_key)):
|
|
|
|
tracker = Tracker.from_data(name)
|
|
|
|
tracker.rename(new_name)
|
|
|
|
return 200
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/tracker/delete")
|
2021-09-12 17:13:43 +00:00
|
|
|
@check_name
|
2021-08-23 06:14:40 +00:00
|
|
|
def delete_tracker(name: str, access_token: APIKey = Depends(get_api_key)):
|
|
|
|
tracker = Tracker.from_data(name)
|
2021-09-12 17:13:43 +00:00
|
|
|
tracker.delete()
|