Skip to content

Commit fdec8cc

Browse files
committed
changes
1 parent c013e43 commit fdec8cc

23 files changed

+804
-307
lines changed

lib/backend/exeat.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import 'package:fluent_ui/fluent_ui.dart';
2+
import 'package:house_management/model/student.dart';
3+
4+
class ExeatProvider extends ChangeNotifier {
5+
fetchExeats() async {}
6+
7+
addExeat({StudentModel? student}) async {}
8+
}

lib/backend/fetch.dart

Lines changed: 15 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:fluent_ui/fluent_ui.dart';
22
import 'package:house_management/model/exeat.dart';
3+
import 'package:house_management/model/item.dart';
34
import 'package:house_management/model/logistics.dart';
45
import 'package:house_management/model/programme.dart';
56
import 'package:house_management/model/status.dart';
@@ -9,10 +10,6 @@ import '../model/house.dart';
910
import '../model/student.dart';
1011

1112
class Backend extends ChangeNotifier {
12-
final List<StudentModel> _students = [];
13-
14-
List<StudentModel> get students => _students;
15-
1613
final List<ProgrammeModel> _programmes = [];
1714

1815
List<ProgrammeModel> get programmes => _programmes;
@@ -24,13 +21,6 @@ class Backend extends ChangeNotifier {
2421

2522
List<StatusModel> get status => _status;
2623

27-
bool _isStudentSelected = false;
28-
29-
bool get isStudentSelected => _isStudentSelected;
30-
late StudentModel _selectedStudent;
31-
32-
StudentModel get selectedStudent => _selectedStudent;
33-
3424
late final List<LogisticsModel> _studentLogistics = [];
3525

3626
List<LogisticsModel> get studentLogistics => _studentLogistics;
@@ -50,75 +40,8 @@ class Backend extends ChangeNotifier {
5040
notifyListeners();
5141
}
5242

53-
fetchStudentLogistics({StudentModel? student}) async {
54-
_studentLogistics.clear();
55-
String query = "select * from logistics where std_id=${student!.id}";
56-
var res = await connection.mappedResultsQuery(query);
57-
for (final row in res) {
58-
Map<String, dynamic> logistic = row["logistics"];
59-
_studentLogistics.add(LogisticsModel.fromMap(logistic));
60-
}
61-
notifyListeners();
62-
}
63-
6443
fetchStudentPunishment({int? id}) async {}
6544

66-
setStudent(StudentModel student) {
67-
_selectedStudent = student;
68-
_isStudentSelected = true;
69-
notifyListeners();
70-
}
71-
72-
clearStudent() {
73-
_isStudentSelected = false;
74-
notifyListeners();
75-
}
76-
77-
Future updateStudent({required String query}) async {
78-
await connection.transaction((ctx) async {
79-
await ctx.query(query);
80-
});
81-
fetchStudents();
82-
}
83-
84-
Future addStudent({
85-
required name,
86-
required programme,
87-
required house,
88-
required parentName,
89-
required residence,
90-
required contact,
91-
required dob,
92-
required status,
93-
}) async {
94-
await connection.transaction((ctx) async {
95-
await ctx.query("insert into registration"
96-
"(name, programme, house, parent_name, residence, contact, \"DOB\", status) "
97-
"values ('$name', '$programme', "
98-
"'$house', '$parentName', '$residence', '$contact', "
99-
"'$dob', '$status')");
100-
}).then((val) {
101-
fetchStudents();
102-
});
103-
}
104-
105-
Future fetchStudents({
106-
int? id,
107-
String? query,
108-
}) async {
109-
_students.clear();
110-
var res = await connection.mappedResultsQuery(
111-
query ??
112-
"select * from registration r join house h on lower(r.house)=lower(h.name) ${id != null ? "where std_id = $id" : ""}",
113-
);
114-
for (final row in res) {
115-
Map<String, dynamic> student = row["registration"];
116-
student['house'] = row['house'];
117-
_students.add(StudentModel.fromMap(student));
118-
}
119-
notifyListeners();
120-
}
121-
12245
Future fetchProgrammes() async {
12346
_programmes.clear();
12447
var res = await connection.mappedResultsQuery("select * from programme");
@@ -152,4 +75,18 @@ class Backend extends ChangeNotifier {
15275
}
15376
notifyListeners();
15477
}
78+
79+
final List<ItemModel> _items = [];
80+
81+
List<ItemModel> get items => _items;
82+
83+
Future fetchItems() async {
84+
_items.clear();
85+
var res = await connection.mappedResultsQuery("select * from items");
86+
for (final row in res) {
87+
var item = row["items"];
88+
_items.add(ItemModel.fromMap(item));
89+
}
90+
notifyListeners();
91+
}
15592
}

lib/backend/logistic.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import 'package:fluent_ui/fluent_ui.dart';
2+
import 'package:house_management/model/student.dart';
3+
4+
import '../main.dart';
5+
import '../model/logistics.dart';
6+
7+
class LogisticsProvider extends ChangeNotifier {
8+
final List<LogisticsModel> _logistics = [];
9+
10+
List<LogisticsModel> get logistics => _logistics;
11+
12+
fetchLogistics({StudentModel? student}) async {
13+
if (student == null) _logistics.clear();
14+
String query =
15+
"select l.date, r.name, r.house, l.items from logistics l join registration r on l.std_id=r.std_id";
16+
if (student != null) query = "$query where r.std_id=${student.id}";
17+
var res = await connection.mappedResultsQuery(query);
18+
for (final row in res) {
19+
Map<String, dynamic> logistic = row["logistics"];
20+
logistic['name'] = row["registration"]['name'];
21+
logistic['house'] = row["registration"]['house'];
22+
if (student != null) {
23+
return logistic;
24+
}
25+
_logistics.add(LogisticsModel.fromMap(logistic));
26+
}
27+
notifyListeners();
28+
}
29+
30+
addLogistics({StudentModel? student}) async {
31+
// check if data already exists
32+
String query = "select * from logistics where std_id = ${student!.id}";
33+
List res = await connection.mappedReesultsQuery(query);
34+
if (res.isEmpty) {
35+
// insert new data
36+
} else {
37+
// update data
38+
}
39+
}
40+
}

lib/backend/punishment.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import 'package:fluent_ui/fluent_ui.dart';
2+
3+
class PunishmentProvider extends ChangeNotifier {}

lib/backend/student.dart

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
import 'package:fluent_ui/fluent_ui.dart';
2+
import 'package:house_management/backend/logistic.dart';
3+
import 'package:house_management/model/student.dart';
4+
import 'package:house_management/utils/to_title_case.dart';
5+
import 'package:provider/provider.dart';
6+
7+
import '../main.dart';
8+
import '../model/logistics.dart';
9+
10+
class StudentProvider extends ChangeNotifier {
11+
final List<StudentModel> _students = [];
12+
13+
List<StudentModel> get students => _students;
14+
15+
Future addStudent({
16+
required name,
17+
required programme,
18+
required house,
19+
required parentName,
20+
required residence,
21+
required contact,
22+
required dob,
23+
required status,
24+
}) async {
25+
await connection.transaction((ctx) async {
26+
await ctx.query("insert into registration"
27+
"(name, programme, house, parent_name, residence, contact, \"DOB\", status) "
28+
"values ('$name', '$programme', "
29+
"'$house', '$parentName', '$residence', '$contact', "
30+
"'$dob', '$status')");
31+
}).then((val) {
32+
fetchStudents();
33+
});
34+
}
35+
36+
late final List<LogisticsModel> _studentLogistics = [];
37+
38+
List<LogisticsModel> get studentLogistics => _studentLogistics;
39+
40+
Future addLogistics({
41+
required StudentModel student,
42+
required List<String> items,
43+
required BuildContext context,
44+
}) async {
45+
String query = "insert into logistics (std_id, date, items)"
46+
"values (${student.id}, now(), array[${items.join(", ")}])";
47+
await connection.transaction((ctx) async {
48+
await ctx.query(query);
49+
});
50+
fetchLogistics(student: student, context: context);
51+
}
52+
53+
fetchLogistics(
54+
{required StudentModel student, required BuildContext context}) async {
55+
_studentLogistics.clear();
56+
var logistics = await Provider.of<LogisticsProvider>(context, listen: false)
57+
.fetchLogistics(student: student);
58+
if (logistics != null) {
59+
_studentLogistics.add(LogisticsModel.fromMap(logistics));
60+
}
61+
notifyListeners();
62+
}
63+
64+
updateLogistics(
65+
{required StudentModel student,
66+
required items,
67+
required BuildContext context}) async {
68+
String query =
69+
"update logistics set items = array_cat(items, ARRAY[${items.join(", ")}]) where std_id=${student.id}";
70+
await connection.transaction((ctx) async {
71+
await ctx.query(query);
72+
});
73+
fetchLogistics(student: student, context: context);
74+
}
75+
76+
fetchPunishment({required StudentModel student}) async {}
77+
78+
fetchExeats({required StudentModel student}) async {}
79+
80+
late StudentModel _selectedStudent;
81+
bool _isStudentSelected = false;
82+
83+
bool get isStudentSelected => _isStudentSelected;
84+
85+
StudentModel get selectedStudent => _selectedStudent;
86+
87+
setStudent(StudentModel student) {
88+
_selectedStudent = student;
89+
_isStudentSelected = true;
90+
notifyListeners();
91+
}
92+
93+
clearStudent() {
94+
_isStudentSelected = false;
95+
notifyListeners();
96+
}
97+
98+
Future searchStudent({String? name}) async {
99+
String query = "select * from registration r join "
100+
"house h on lower(r.house)=lower(h.name)"
101+
" where r.name ilike '%$name%'";
102+
await fetchStudents(query: query);
103+
}
104+
105+
Future fetchStudents({
106+
int? id,
107+
String? query,
108+
}) async {
109+
_students.clear();
110+
var res = await connection.mappedResultsQuery(
111+
query ??
112+
"select * from registration r join house h on lower(r.house)=lower(h.name) ${id != null ? "where std_id = $id" : ""}",
113+
);
114+
for (final row in res) {
115+
Map<String, dynamic> student = row["registration"];
116+
student['house'] = row['house'];
117+
student['name'] = toTitle(student['name']);
118+
_students.add(StudentModel.fromMap(student));
119+
}
120+
notifyListeners();
121+
}
122+
123+
Future updateStudent({
124+
required id,
125+
required name,
126+
required programme,
127+
required house,
128+
required parentName,
129+
required residence,
130+
required contact,
131+
required dob,
132+
required status,
133+
}) async {
134+
String query = "update registration set "
135+
"name=coalesce('$name', name),"
136+
"house=coalesce('$house', house),"
137+
"programme=coalesce('$programme',programme),"
138+
"parent_name=coalesce('$parentName',parent_name),"
139+
"residence=coalesce('$residence',residence),"
140+
"contact=coalesce('$contact',contact),"
141+
"\"DOB\"=coalesce('$dob',\"DOB\"),"
142+
"status=coalesce('$status',status) "
143+
"where std_id = $id";
144+
await connection.transaction((ctx) async {
145+
await ctx.query(query);
146+
});
147+
fetchStudents();
148+
}
149+
}

lib/main.dart

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import 'package:fluent_ui/fluent_ui.dart';
2+
import 'package:house_management/backend/exeat.dart';
3+
import 'package:house_management/backend/logistic.dart';
4+
import 'package:house_management/backend/punishment.dart';
5+
import 'package:house_management/backend/student.dart';
26
import 'package:house_management/constants/prefs.dart';
37
import 'package:house_management/pages/auth/login_page.dart';
48
import 'package:postgres/postgres.dart';
@@ -19,8 +23,13 @@ void main() {
1923
// LOAD DATA FROM SHARED PREFERENCES
2024
Future init() async {
2125
prefs = await SharedPreferences.getInstance();
22-
connection = PostgreSQLConnection("localhost", 5432, "2021/22",
23-
username: "postgres", password: "q1w2e3r4t5y6");
26+
connection = PostgreSQLConnection(
27+
"localhost",
28+
5432,
29+
"2021/22",
30+
username: "postgres",
31+
password: "q1w2e3r4t5y6",
32+
);
2433
await connection.open().then((value) async {
2534
print('connection opened');
2635
await connection.transaction((ctx) async {
@@ -52,6 +61,10 @@ class _MyAppState extends State<MyApp> {
5261
providers: [
5362
ChangeNotifierProvider(create: (context) => Prefs()),
5463
ChangeNotifierProvider(create: (context) => Backend()),
64+
ChangeNotifierProvider(create: (context) => StudentProvider()),
65+
ChangeNotifierProvider(create: (context) => ExeatProvider()),
66+
ChangeNotifierProvider(create: (context) => LogisticsProvider()),
67+
ChangeNotifierProvider(create: (context) => PunishmentProvider()),
5568
],
5669
child: const HomePage(),
5770
);
@@ -66,6 +79,16 @@ class HomePage extends StatefulWidget {
6679
}
6780

6881
class _HomePageState extends State<HomePage> {
82+
@override
83+
void initState() {
84+
super.initState();
85+
init();
86+
}
87+
88+
init() async {
89+
await Provider.of<Backend>(context, listen: false).fetchItems();
90+
}
91+
6992
@override
7093
Widget build(BuildContext context) {
7194
return Consumer<Prefs>(builder: (context, prefs, child) {

0 commit comments

Comments
 (0)