From 50e188df3b808b6675951ec9fb7a47a5adafeeef Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 18 Mar 2020 17:35:08 +0100 Subject: [PATCH] Add fiorygi get star --- royalpack/stars/__init__.py | 2 ++ royalpack/stars/api_fiorygi_get.py | 33 +++++++++++++++++++++++++ royalpack/tables/fiorygitransactions.py | 6 ++++- 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 royalpack/stars/api_fiorygi_get.py diff --git a/royalpack/stars/__init__.py b/royalpack/stars/__init__.py index 6ea70aed..a36a1347 100644 --- a/royalpack/stars/__init__.py +++ b/royalpack/stars/__init__.py @@ -8,6 +8,7 @@ from .api_discord_play import ApiDiscordPlayStar from .api_wiki_edit import ApiWikiEditStar from .api_wiki_get import ApiWikiGetStar from .api_wiki_list import ApiWikiListStar +from .api_fiorygi_get import ApiFiorygiGetStar # Enter the PageStars of your Pack here! available_page_stars = [ @@ -20,6 +21,7 @@ available_page_stars = [ ApiWikiEditStar, ApiWikiGetStar, ApiWikiListStar, + ApiFiorygiGetStar, ] # Don't change this, it should automatically generate __all__ diff --git a/royalpack/stars/api_fiorygi_get.py b/royalpack/stars/api_fiorygi_get.py new file mode 100644 index 00000000..60da931b --- /dev/null +++ b/royalpack/stars/api_fiorygi_get.py @@ -0,0 +1,33 @@ +from typing import * +from royalnet.utils import * +from royalnet.backpack.tables import * +from royalnet.constellation.api import * +from ..utils import find_user_api +from ..tables import Fiorygi, FiorygiTransaction + + +class ApiFiorygiGetStar(ApiStar): + path = "/api/user/fiorygi/get/v1" + + summary = "Get the fiorygi of a Royalnet user." + + parameters = { + "id": "The user to get the fiorygi of." + } + + tags = ["user"] + + async def api(self, data: ApiData) -> JSON: + user: User = await find_user_api(data["id"], self.alchemy, data.session) + if user.fiorygi is None: + return { + "fiorygi": 0, + "transactions": [], + "warning": "No associated fiorygi table" + } + fiorygi: Fiorygi = user.fiorygi + transactions: JSON = sorted(fiorygi.transactions, key=lambda t: -t.id) + return { + "fiorygi": fiorygi.fiorygi, + "transactions": list(map(lambda t: {"id": t.id, "change": t.change, "reason": t.reason}, transactions)) + } diff --git a/royalpack/tables/fiorygitransactions.py b/royalpack/tables/fiorygitransactions.py index 75701d0d..28fcbf73 100644 --- a/royalpack/tables/fiorygitransactions.py +++ b/royalpack/tables/fiorygitransactions.py @@ -19,9 +19,13 @@ class FiorygiTransaction: return Column(Integer, ForeignKey("fiorygi.user_id"), nullable=False) @declared_attr - def user(self): + def wallet(self): return relationship("Fiorygi", backref=backref("transactions")) + @property + def user(self): + return self.wallet.user + @declared_attr def reason(self): return Column(String, nullable=False, default="")