Skip to content

Commit 9ebe0df

Browse files
din0ssakrifor
andauthored
Implement profile fetching/updating (#21)
Co-authored-by: Athanasios Lagopoulos <sakrifor@gmail.com>
1 parent f899985 commit 9ebe0df

19 files changed

Lines changed: 312 additions & 347 deletions

lib/DataFetcher.dart

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@ import 'dart:io';
44

55
import 'package:dio/dio.dart';
66
import 'package:flutter/foundation.dart';
7+
import 'package:grade_plus_plus/entities/course/PassedCourse.dart';
78

89
import 'LocalKeyValuePersistence.dart';
910
import 'entities/course/BaseCourseData.dart';
1011
import 'entities/course/Course.dart';
1112
import 'entities/course/CourseDifficulty.dart';
12-
import 'entities/course/PassedCourseData.dart';
1313
import 'entities/course/PredictedCourse.dart';
1414
import 'entities/course/SuggestedCourseData.dart';
1515
import 'entities/user/FormData.dart' as UserFD;
1616
import 'entities/user/SchoolData.dart';
17-
import 'entities/user/SemesterData.dart';
1817
import 'entities/user/Teacher.dart';
1918
import 'entities/user/UserData.dart';
2019

@@ -84,14 +83,13 @@ class DataFetcher {
8483
}
8584
}
8685

87-
static UserData fetchUserData() {
88-
// To be implemented for data fetching
89-
return new UserData(
90-
estYear: null,
91-
schoolData: null,
92-
favSubjects: null,
93-
favTeachers: null,
94-
semesterDataList: null);
86+
static Future<UserData> fetchUserData() async {
87+
try {
88+
var res = await dio.get("user/profile/");
89+
return UserData.fromJson(res.data);
90+
} on DioError catch (_) {
91+
return LocalKeyValuePersistence.getUserData();
92+
}
9593
}
9694

9795
static List<SuggestedCourseData> fetchSuggestedCourses() {
@@ -123,6 +121,36 @@ class DataFetcher {
123121
}
124122
}
125123

124+
static Future<bool> updateName(String name) async {
125+
try {
126+
await dio.patch(
127+
"user/profile/",
128+
data: {"name": name},
129+
);
130+
return true;
131+
} on DioError catch (_) {
132+
return false;
133+
}
134+
}
135+
136+
static Future<bool> updateFavorites(
137+
Set<String> courses,
138+
Set<String> teachers,
139+
) async {
140+
try {
141+
await dio.post(
142+
"user/favorites",
143+
data: json.encode({
144+
"courses": courses.toList(),
145+
"teachers": teachers.toList(),
146+
}),
147+
);
148+
return true;
149+
} on DioError catch (_) {
150+
return false;
151+
}
152+
}
153+
126154
static List<PredictedCourse> fetchDefaultPredictedCourses() {
127155
return <PredictedCourse>[
128156
PredictedCourse(
@@ -136,10 +164,8 @@ class DataFetcher {
136164
return UserData(
137165
name: 'Test Subject',
138166
schoolData: SchoolData(
139-
department: 'Computer Science',
140-
semester: 5,
167+
school: 'Computer Science',
141168
),
142-
estYear: 2021,
143169
favSubjects: <String>[
144170
"astronomy",
145171
"object oriented programming",
@@ -150,49 +176,16 @@ class DataFetcher {
150176
"mr bean",
151177
"the janitor",
152178
],
153-
semesterDataList: <SemesterData>[
154-
SemesterData(
155-
id: 1,
156-
courseDataList: <PassedCourseData>[
157-
PassedCourseData(
158-
baseData: BaseCourseData(
159-
title: 'My course title',
160-
code: 'ABC-01-234',
161-
teacher: 'John Smith',
162-
averageGrade: 9.9,
163-
difficulty: CourseDifficulty.EASY,
164-
),
165-
grade: 9,
166-
year: '2013-2014',
167-
),
168-
PassedCourseData(
169-
baseData: BaseCourseData(
170-
title: 'Some subject',
171-
code: 'ABC-91-114',
172-
teacher: 'Dr Jeff',
173-
averageGrade: 7.5,
174-
difficulty: CourseDifficulty.MEDIUM,
175-
),
176-
grade: 10,
177-
year: '2013-2014',
178-
),
179-
],
179+
passedCourses: <PassedCourse>[
180+
PassedCourse(
181+
courseID: "40002935",
182+
grade: 9,
183+
year: '2013-2014',
180184
),
181-
SemesterData(
182-
id: 2,
183-
courseDataList: <PassedCourseData>[
184-
PassedCourseData(
185-
baseData: BaseCourseData(
186-
title: 'My new course title',
187-
code: 'XYZ-01-234',
188-
teacher: 'Hello World',
189-
averageGrade: 4.5,
190-
difficulty: CourseDifficulty.HARD,
191-
),
192-
grade: 2,
193-
year: '2013-2014',
194-
),
195-
],
185+
PassedCourse(
186+
courseID: "40002933",
187+
grade: 10,
188+
year: '2013-2014',
196189
),
197190
],
198191
);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import 'package:meta/meta.dart';
2+
3+
class PassedCourse {
4+
PassedCourse({
5+
@required this.courseID,
6+
@required this.grade,
7+
@required this.year,
8+
});
9+
10+
final String courseID;
11+
final double grade;
12+
final String year;
13+
14+
factory PassedCourse.fromJson(Map<String, dynamic> json) {
15+
return PassedCourse(
16+
courseID: json['_id'] as String,
17+
grade: json['grade'].toDouble() as double,
18+
year: json['year_passed'] as String);
19+
}
20+
21+
Map<String, dynamic> toJson() =>
22+
<String, dynamic>{'_id': courseID, 'grade': grade, 'year_passed': year};
23+
}

lib/entities/user/FormData.dart

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import 'package:json_annotation/json_annotation.dart';
2-
3-
part 'FormData.g.dart';
4-
5-
@JsonSerializable(explicitToJson: true)
61

72

83
class FormData {
@@ -20,7 +15,6 @@ class FormData {
2015
this.hobbies,
2116
});
2217

23-
2418
String name;
2519
String school;
2620
int semester;
@@ -33,8 +27,33 @@ class FormData {
3327
String distance;
3428
List<String> hobbies;
3529

36-
factory FormData.fromJson(Map<String, dynamic> json) => _$FormDataFromJson(json);
37-
38-
Map<String, dynamic> toJson() => _$FormDataToJson(this);
39-
30+
factory FormData.fromJson(Map<String, dynamic> json) {
31+
return FormData(
32+
name: json['name'] as String,
33+
school: json['school'] as String,
34+
semester: json['semester'] as int,
35+
reason: json['reason'] as String,
36+
studyTime: json['study_time'] as int,
37+
lectures: json['lectures'] as String,
38+
privateLessons: json['privateLessons'] as bool,
39+
postgraduate: json['postgraduate'] as String,
40+
roomates: json['roomates'] as String,
41+
distance: json['distance'] as String,
42+
hobbies: (json['hobbies'] as List)?.map((e) => e as String)?.toList(),
43+
);
44+
}
45+
46+
Map<String, dynamic> toJson() => <String, dynamic>{
47+
'name': name,
48+
'school': school,
49+
'semester': semester,
50+
'reason': reason,
51+
'study_time': studyTime,
52+
'lectures': lectures,
53+
'privateLessons': privateLessons.toString(),
54+
'postgraduate': postgraduate,
55+
'roomates': roomates,
56+
'distance': distance,
57+
'hobbies': hobbies,
58+
};
4059
}

lib/entities/user/FormData.g.dart

Lines changed: 0 additions & 37 deletions
This file was deleted.

lib/entities/user/SchoolData.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ import 'package:meta/meta.dart';
44
part 'SchoolData.g.dart';
55

66
@JsonSerializable(explicitToJson: true)
7-
87
class SchoolData {
98
const SchoolData({
10-
@required this.department,
11-
@required this.semester,
9+
@required this.school,
1210
});
1311

14-
final String department;
15-
final int semester;
12+
final String school;
1613

1714
factory SchoolData.fromJson(Map<String, dynamic> json) =>
1815
_$SchoolDataFromJson(json);

lib/entities/user/SchoolData.g.dart

Lines changed: 2 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/entities/user/SemesterData.dart

Lines changed: 0 additions & 23 deletions
This file was deleted.

lib/entities/user/SemesterData.g.dart

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/entities/user/Teacher.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
import 'package:json_annotation/json_annotation.dart';
21
import 'package:meta/meta.dart';
32

4-
part 'Teacher.g.dart';
5-
6-
@JsonSerializable(explicitToJson: true)
73
class Teacher {
84
Teacher({@required this.id, @required this.name, @required this.courses});
95

106
final String id;
117
final String name;
128
final List<String> courses;
139

14-
factory Teacher.fromJson(Map<String, dynamic> json) =>
15-
_$TeacherFromJson(json);
10+
factory Teacher.fromJson(Map<String, dynamic> json) {
11+
return Teacher(
12+
id: json['_id'] as String,
13+
name: json['name'] as String,
14+
courses: (json['courses'] as List)?.map((e) => e as String)?.toList(),
15+
);
16+
}
1617

17-
Map<String, dynamic> toJson() => _$TeacherToJson(this);
18+
Map<String, dynamic> toJson() => <String, dynamic>{
19+
'id': id,
20+
'name': name,
21+
'courses': courses,
22+
};
1823
}

0 commit comments

Comments
 (0)