(add): Reports api endpoints, pycache correction
This commit is contained in:
+2
-1
@@ -1,2 +1,3 @@
|
||||
/data
|
||||
*/__pycache__
|
||||
__pycache__/
|
||||
*.pyc
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,50 @@
|
||||
from dataclasses import dataclass
|
||||
from db import RepoTemplate
|
||||
from system.auth import AuthRepo
|
||||
from fastapi import APIRouter, Header
|
||||
|
||||
@dataclass
|
||||
class ReportByNameLine:
|
||||
last_name: str = ""
|
||||
first_name: str = ""
|
||||
phone_number: str = ""
|
||||
pref: str = ""
|
||||
prefix: str = ""
|
||||
b_id: int = 0
|
||||
description: str = ""
|
||||
donors: str = ""
|
||||
winning_ticket: int = 0
|
||||
|
||||
@dataclass
|
||||
class ReportByBasketLine:
|
||||
prefix: str = ""
|
||||
b_id: int = 0
|
||||
description: str = ""
|
||||
donors: str = ""
|
||||
winning_ticket: int = 0
|
||||
last_name: str = ""
|
||||
first_name: str = ""
|
||||
phone_number: str = ""
|
||||
pref: str = ""
|
||||
|
||||
class ReportsRepo(RepoTemplate):
|
||||
def get_by_name_report(self, prefix: str):
|
||||
self.cur.execute("SELECT * FROM report_by_name WHERE prefix = ?", (prefix,))
|
||||
results = self.cur.fetchall()
|
||||
return [ReportByNameLine(*r) for r in results]
|
||||
def get_by_basket_report(self, prefix: str):
|
||||
self.cur.execute("SELECT * FROM report_by_basket WHERE prefix = ?", (prefix,))
|
||||
results = self.cur.fetchall()
|
||||
return [ReportByBasketLine(*r) for r in results]
|
||||
|
||||
reports_router = APIRouter(prefix="/api/reports")
|
||||
|
||||
@reports_router.get("/byname/{prefix}")
|
||||
def get_report_by_name(prefix: str, tam_key: str = Header("")):
|
||||
AuthRepo().verify_key(tam_key)
|
||||
return ReportsRepo().get_by_name_report(prefix)
|
||||
|
||||
@reports_router.get("/bybasket/{prefix}")
|
||||
def get_report_by_basket(prefix: str, tam_key: str = Header("")):
|
||||
AuthRepo().verify_key(tam_key)
|
||||
return ReportsRepo().get_by_basket_report(prefix)
|
||||
@@ -25,6 +25,14 @@ def init_db():
|
||||
SELECT b.prefix, b.b_id, b.description, b.winning_ticket, t.last_name, t.first_name, t.phone_number
|
||||
FROM baskets b LEFT JOIN tickets t ON b.prefix = t.prefix AND b.winning_ticket = t.t_id
|
||||
ORDER BY b.prefix, b.b_id""")
|
||||
cur.execute("""CREATE VIEW IF NOT EXISTS report_by_name AS
|
||||
SELECT t.last_name, t.first_name, t.phone_number, t.pref, b.*
|
||||
FROM baskets b LEFT JOIN tickets t ON b.prefix = t.prefix AND b.winning_ticket = t.t_id
|
||||
ORDER BY t.last_name, t.first_name, t.phone_number, b.prefix, b.b_id""")
|
||||
cur.execute("""CREATE VIEW IF NOT EXISTS report_by_basket AS
|
||||
SELECT b.*, t.last_name, t.first_name, t.phone_number, t.pref
|
||||
FROM baskets b LEFT JOIN tickets t ON b.prefix = t.prefix AND b.winning_ticket = t.t_id
|
||||
ORDER BY b.prefix, b.b_id""")
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ from system.prefixes import prefix_router
|
||||
from data.tickets import tickets_router
|
||||
from data.baskets import basket_router
|
||||
from data.drawing import drawing_router
|
||||
from data.reports import reports_router
|
||||
|
||||
def imp_routers(app: FastAPI):
|
||||
app.include_router(auth_router)
|
||||
@@ -11,3 +12,4 @@ def imp_routers(app: FastAPI):
|
||||
app.include_router(tickets_router)
|
||||
app.include_router(basket_router)
|
||||
app.include_router(drawing_router)
|
||||
app.include_router(reports_router)
|
||||
|
||||
Reference in New Issue
Block a user