From c8056043e55d927a1d5df669290c1d2da8583fe1 Mon Sep 17 00:00:00 2001 From: JosephValle Date: Wed, 21 Feb 2024 09:47:44 -0500 Subject: [PATCH] Temp --- .../api_clients/schools_api_client.dart | 33 +++++ lib/objects/school.dart | 26 ++++ .../schools/widgets/school_tile.dart | 127 +++++++++++------- 3 files changed, 138 insertions(+), 48 deletions(-) diff --git a/lib/network_interface/api_clients/schools_api_client.dart b/lib/network_interface/api_clients/schools_api_client.dart index eedb733..6574384 100644 --- a/lib/network_interface/api_clients/schools_api_client.dart +++ b/lib/network_interface/api_clients/schools_api_client.dart @@ -1,8 +1,11 @@ import 'dart:async'; +import 'package:adams_county_scheduler/network_interface/api_clients/students_api_client.dart'; import 'package:adams_county_scheduler/objects/school.dart'; +import 'package:adams_county_scheduler/objects/student.dart'; import 'package:cloud_firestore/cloud_firestore.dart'; import 'package:firebase_storage/firebase_storage.dart'; +import 'package:flutter/cupertino.dart'; import 'package:image_picker/image_picker.dart'; import '../collection_names.dart'; @@ -78,4 +81,34 @@ class SchoolsApiClient { .map((e) => School.fromJson(e.data())) .toList(); } + + + Future deleteSchoolStudents({required School school}) async { + try { + final List students = (await StudentApiClient().getStudents()) + .where((student) => student.school == school.shortName) + .toList(); + + // Assuming 'students' collection stores the students + WriteBatch batch = _firestore.batch(); + + // Step 2: Batch delete all students + for (final student in students) { + // Add delete operation to batch + var docRef = _firestore.collection('students').doc(student.id); + batch.delete(docRef); + } + + // Committing the batch delete + await batch.commit(); + + // Step 3: Update school doc 'studentCount' field to 0 + var schoolDocRef = _firestore.collection('schools').doc(school.id); + await schoolDocRef.update({'studentCount': 0}); + + } catch (e) { + debugPrint(e.toString()); + rethrow; + } + } } diff --git a/lib/objects/school.dart b/lib/objects/school.dart index 99be809..43f23d0 100644 --- a/lib/objects/school.dart +++ b/lib/objects/school.dart @@ -80,6 +80,32 @@ class School extends HiveObject { required this.time, }); + // copyWith method + School copyWith({ + String? id, + String? name, + String? shortName, + String? category, + String? imageUrl, + int? studentCount, + int? activeCareerCount, + int? classroomCount, + String? time, + }) { + return School( + id: id ?? this.id, + name: name ?? this.name, + shortName: shortName ?? this.shortName, + category: category ?? this.category, + imageUrl: imageUrl ?? this.imageUrl, + studentCount: studentCount ?? this.studentCount, + activeCareerCount: activeCareerCount ?? this.activeCareerCount, + classroomCount: classroomCount ?? this.classroomCount, + time: time ?? this.time, + ); + } + + factory School.fromJson(Map json) => _$SchoolFromJson(json); Map toJson() => _$SchoolToJson(this); diff --git a/lib/user_interface/schools/widgets/school_tile.dart b/lib/user_interface/schools/widgets/school_tile.dart index db079de..df15e06 100644 --- a/lib/user_interface/schools/widgets/school_tile.dart +++ b/lib/user_interface/schools/widgets/school_tile.dart @@ -1,3 +1,4 @@ +import 'package:adams_county_scheduler/network_interface/api_clients/schools_api_client.dart'; import 'package:adams_county_scheduler/objects/school.dart'; import 'package:adams_county_scheduler/user_interface/school_detail/school_detail_page.dart'; import 'package:adams_county_scheduler/user_interface/widgets/colored_container.dart'; @@ -7,12 +8,27 @@ import 'package:flutter/material.dart'; import '../../../utilities/routes/routes.dart'; -class SchoolTile extends StatelessWidget { +class SchoolTile extends StatefulWidget { final School school; final bool header; const SchoolTile({super.key, required this.school, this.header = false}); + @override + State createState() => _SchoolTileState(); +} + +class _SchoolTileState extends State { + late School school; + late bool header; + + @override + void initState() { + super.initState(); + header = widget.header; + school = widget.school; + } + @override Widget build(BuildContext context) { return Padding( @@ -23,9 +39,9 @@ class SchoolTile extends StatelessWidget { onTap: () => header ? null : Navigator.of(context).pushNamed( - Routes.schoolDetailPage, - arguments: SchoolDetailPageArgs(school: school), - ), + Routes.schoolDetailPage, + arguments: SchoolDetailPageArgs(school: school), + ), height: MediaQuery.of(context).size.height / 3, childPadding: EdgeInsets.zero, backgroundColor: Theme.of(context).colorScheme.surface, @@ -36,25 +52,25 @@ class SchoolTile extends StatelessWidget { width: double.infinity, child: ClipRRect( borderRadius: - header ? BorderRadius.zero : BorderRadius.circular(10), + header ? BorderRadius.zero : BorderRadius.circular(10), child: Stack( children: [ school.imageUrl.isEmpty ? Container( - width: double.infinity, - height: double.infinity, - color: ACColors.secondaryColor, - ) + width: double.infinity, + height: double.infinity, + color: ACColors.secondaryColor, + ) : Container( - decoration: BoxDecoration( - image: DecorationImage( - fit: BoxFit.cover, - image: NetworkImage( - school.imageUrl, + decoration: BoxDecoration( + image: DecorationImage( + fit: BoxFit.cover, + image: NetworkImage( + school.imageUrl, + ), + ), + ), ), - ), - ), - ), Container( decoration: BoxDecoration( gradient: LinearGradient( @@ -77,6 +93,21 @@ class SchoolTile extends StatelessWidget { ), ), ), + Align( + alignment: Alignment.topRight, + child: Padding( + padding: const EdgeInsets.all(8.0), + child: IconButton( + onPressed: () async { + await SchoolsApiClient() + .deleteSchoolStudents(school: school); + setState(() { + school = school.copyWith(studentCount: 0); + }); + }, + icon: Icon(Icons.delete)), + ), + ), Align( alignment: Alignment.topLeft, child: Container( @@ -99,7 +130,7 @@ class SchoolTile extends StatelessWidget { maxLines: 2, style: TextStyle( fontSize: - MediaQuery.of(context).size.width / 30, + MediaQuery.of(context).size.width / 30, ), ), ), @@ -108,41 +139,41 @@ class SchoolTile extends StatelessWidget { header ? Container() : Align( - alignment: Alignment.bottomCenter, - child: Padding( - padding: const EdgeInsets.all(8.0), - child: Row( - crossAxisAlignment: CrossAxisAlignment.end, - children: [ - Expanded( - child: Align( - alignment: Alignment.bottomCenter, - child: RichText( - text: TextSpan( - text: 'Total Students:\t', - style: TextStyle( - fontWeight: FontWeight.w200, - color: Theme.of(context) - .colorScheme - .onBackground, - ), - children: [ - TextSpan( - text: school.studentCount - .toString(), - style: const TextStyle( - fontWeight: FontWeight.w600, + alignment: Alignment.bottomCenter, + child: Padding( + padding: const EdgeInsets.all(8.0), + child: Row( + crossAxisAlignment: CrossAxisAlignment.end, + children: [ + Expanded( + child: Align( + alignment: Alignment.bottomCenter, + child: RichText( + text: TextSpan( + text: 'Total Students:\t', + style: TextStyle( + fontWeight: FontWeight.w200, + color: Theme.of(context) + .colorScheme + .onBackground, + ), + children: [ + TextSpan( + text: school.studentCount + .toString(), + style: const TextStyle( + fontWeight: FontWeight.w600, + ), + ), + ], ), ), - ], + ), ), - ), + ], ), ), - ], - ), - ), - ), + ), ], ), ),