Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
JosephValle committed Feb 21, 2024
1 parent 92a30b8 commit c805604
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 48 deletions.
33 changes: 33 additions & 0 deletions lib/network_interface/api_clients/schools_api_client.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -78,4 +81,34 @@ class SchoolsApiClient {
.map<School>((e) => School.fromJson(e.data()))
.toList();
}


Future<void> deleteSchoolStudents({required School school}) async {
try {
final List<Student> 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;
}
}
}
26 changes: 26 additions & 0 deletions lib/objects/school.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> json) => _$SchoolFromJson(json);

Map<String, dynamic> toJson() => _$SchoolToJson(this);
Expand Down
127 changes: 79 additions & 48 deletions lib/user_interface/schools/widgets/school_tile.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<SchoolTile> createState() => _SchoolTileState();
}

class _SchoolTileState extends State<SchoolTile> {
late School school;
late bool header;

@override
void initState() {
super.initState();
header = widget.header;
school = widget.school;
}

@override
Widget build(BuildContext context) {
return Padding(
Expand All @@ -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,
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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,
),
),
),
Expand All @@ -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,
),
),
],
),
),
],
),
),
),
],
),
),
],
),
),
),
),
],
),
),
Expand Down

0 comments on commit c805604

Please sign in to comment.