From 490806aaee4e03da8ea94e212d66e17d1510f701 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Tue, 28 Jan 2025 21:12:41 -0500 Subject: [PATCH] Added more info to classes API endpoint. --- .../classroom_compliance/api_class.d | 34 +++++++++++++++++-- app/src/api/classroom_compliance.ts | 11 +++++- .../apps/classroom_compliance/ClassItem.vue | 10 ++++-- .../apps/classroom_compliance/ClassesView.vue | 4 +-- 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/api/source/api_modules/classroom_compliance/api_class.d b/api/source/api_modules/classroom_compliance/api_class.d index 4f2eee0..f356b65 100644 --- a/api/source/api_modules/classroom_compliance/api_class.d +++ b/api/source/api_modules/classroom_compliance/api_class.d @@ -46,10 +46,38 @@ void getClasses(ref HttpRequestContext ctx) { Connection conn = getDb(); scope(exit) conn.close(); User user = getUserOrThrow(ctx, conn); - auto classes = findAll( + const query = " + SELECT + c.id, c.number, c.school_year, + COUNT(DISTINCT s.id) AS student_count, + COUNT(DISTINCT e.id) AS entry_count, + MAX(e.date) AS last_entry_date + FROM classroom_compliance_class c + LEFT JOIN classroom_compliance_student s ON c.id = s.class_id + LEFT JOIN classroom_compliance_entry e ON c.id = e.class_id + WHERE c.user_id = ? + GROUP BY c.id + ORDER BY school_year DESC, number ASC + "; + struct ClassResponse { + ulong id; + ushort number; + string schoolYear; + uint studentCount; + uint entryCount; + string lastEntryDate; + } + ClassResponse[] classes = findAll( conn, - "SELECT * FROM classroom_compliance_class WHERE user_id = ? ORDER BY school_year DESC, number ASC", - &ClassroomComplianceClass.parse, + query, + r => ClassResponse( + r.getUlong(1), + r.getUshort(2), + r.getString(3), + r.getUint(4), + r.getUint(5), + r.getDate(6).toISOExtString() + ), user.id ); writeJsonBody(ctx, classes); diff --git a/app/src/api/classroom_compliance.ts b/app/src/api/classroom_compliance.ts index 8e0ed12..d20cb13 100644 --- a/app/src/api/classroom_compliance.ts +++ b/app/src/api/classroom_compliance.ts @@ -16,6 +16,15 @@ export interface Class { schoolYear: string } +export interface ClassesResponseClass { + id: number + number: number + schoolYear: string + studentCount: number + entryCount: number + lastEntryDate: string +} + export interface Student { id: number name: string @@ -100,7 +109,7 @@ export class ClassroomComplianceAPIClient extends APIClient { return super.post('/classes', { number: number, schoolYear: schoolYear }) } - getClasses(): APIResponse { + getClasses(): APIResponse { return super.get('/classes') } diff --git a/app/src/apps/classroom_compliance/ClassItem.vue b/app/src/apps/classroom_compliance/ClassItem.vue index 0f39c8a..1e6bca3 100644 --- a/app/src/apps/classroom_compliance/ClassItem.vue +++ b/app/src/apps/classroom_compliance/ClassItem.vue @@ -1,16 +1,20 @@