(add): Reports api endpoints, pycache correction
This commit is contained in:
+2
-1
@@ -1,2 +1,3 @@
|
|||||||
/data
|
/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
|
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
|
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""")
|
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.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ from system.prefixes import prefix_router
|
|||||||
from data.tickets import tickets_router
|
from data.tickets import tickets_router
|
||||||
from data.baskets import basket_router
|
from data.baskets import basket_router
|
||||||
from data.drawing import drawing_router
|
from data.drawing import drawing_router
|
||||||
|
from data.reports import reports_router
|
||||||
|
|
||||||
def imp_routers(app: FastAPI):
|
def imp_routers(app: FastAPI):
|
||||||
app.include_router(auth_router)
|
app.include_router(auth_router)
|
||||||
@@ -11,3 +12,4 @@ def imp_routers(app: FastAPI):
|
|||||||
app.include_router(tickets_router)
|
app.include_router(tickets_router)
|
||||||
app.include_router(basket_router)
|
app.include_router(basket_router)
|
||||||
app.include_router(drawing_router)
|
app.include_router(drawing_router)
|
||||||
|
app.include_router(reports_router)
|
||||||
|
|||||||
@@ -43,3 +43,31 @@ export const drawing = sqliteView('drawing', {
|
|||||||
}).as(sql`SELECT b.prefix, b.b_id, b.description, b.winning_ticket, t.last_name, t.first_name, t.phone_number
|
}).as(sql`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
|
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`)
|
ORDER BY b.prefix, b.b_id`)
|
||||||
|
|
||||||
|
export const reportByName = sqliteView('report_by_name', {
|
||||||
|
last_name: text('last_name'),
|
||||||
|
first_name: text('first_name'),
|
||||||
|
phone_number: text('phone_number'),
|
||||||
|
pref: text('pref'),
|
||||||
|
prefix: text('prefix'),
|
||||||
|
b_id: integer('b_id'),
|
||||||
|
description: text('description'),
|
||||||
|
donors: text('donors'),
|
||||||
|
winning_ticket: integer('winning_ticket')
|
||||||
|
}).as(sql`SELECT t.last_name, t.first_name, t.phone_number, t.pref, b.prefix, b.b_id, b.description, b.donors, b.winning_ticket
|
||||||
|
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`)
|
||||||
|
|
||||||
|
export const reportByBasket = sqliteView('report_by_basket', {
|
||||||
|
prefix: text('prefix'),
|
||||||
|
b_id: integer('b_id'),
|
||||||
|
description: text('description'),
|
||||||
|
donors: text('donors'),
|
||||||
|
winning_ticket: integer('winning_ticket'),
|
||||||
|
last_name: text('last_name'),
|
||||||
|
first_name: text('first_name'),
|
||||||
|
phone_number: text('phone_number'),
|
||||||
|
pref: text('pref')
|
||||||
|
}).as(sql`SELECT b.prefix, b.b_id, b.description, b.donors, b.winning_ticket, 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`)
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { db } from '$lib/server/db/index.js';
|
||||||
|
import { reportByBasket } from '$lib/server/db/schema.js';
|
||||||
|
import { getPath, getSettings } from '$lib/server/settings';
|
||||||
|
import { error, json } from '@sveltejs/kit';
|
||||||
|
import { eq } from 'drizzle-orm';
|
||||||
|
|
||||||
|
export const GET = async ({ params }) => {
|
||||||
|
const { prefix } = params;
|
||||||
|
const s = getSettings();
|
||||||
|
if (s.remote_server) {
|
||||||
|
const connStr = getPath(s);
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${connStr}/api/reports/bybasket/${prefix}`, {headers: {'TAM-KEY': s.remote_key}});
|
||||||
|
if (!res.ok) throw error(res.status);
|
||||||
|
const data = await res.json();
|
||||||
|
return json(data);
|
||||||
|
} catch {
|
||||||
|
return json([]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const data = await db.select().from(reportByBasket).where(eq(reportByBasket.prefix, prefix));
|
||||||
|
return json(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import { db } from '$lib/server/db/index.js';
|
||||||
|
import { reportByName } from '$lib/server/db/schema.js';
|
||||||
|
import { getPath, getSettings } from '$lib/server/settings';
|
||||||
|
import { error, json } from '@sveltejs/kit';
|
||||||
|
import { eq } from 'drizzle-orm';
|
||||||
|
|
||||||
|
export const GET = async ({ params }) => {
|
||||||
|
const { prefix } = params;
|
||||||
|
const s = getSettings();
|
||||||
|
if (s.remote_server) {
|
||||||
|
const connStr = getPath(s);
|
||||||
|
try {
|
||||||
|
const res = await fetch(`${connStr}/api/reports/byname/${prefix}`, {headers: {'TAM-KEY': s.remote_key}});
|
||||||
|
if (!res.ok) throw error(res.status);
|
||||||
|
const data = await res.json();
|
||||||
|
return json(data);
|
||||||
|
} catch {
|
||||||
|
return json([]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const data = await db.select().from(reportByName).where(eq(reportByName.prefix, prefix));
|
||||||
|
return json(data);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user