Skip to content

Commit

Permalink
Merge pull request #25 from B3-3iL-DLW/refactor/refactor-data-layer-api
Browse files Browse the repository at this point in the history
Refactor/refactor data layer api
  • Loading branch information
PHPLukaas authored Mar 12, 2024
2 parents d456c43 + 1ae71c4 commit b4fd334
Show file tree
Hide file tree
Showing 34 changed files with 504 additions and 246 deletions.
15 changes: 12 additions & 3 deletions frontend/app_student/lib/api/api_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';

import 'package:http/http.dart' as http;

class ApiService {
Expand All @@ -8,14 +9,22 @@ class ApiService {

Future<List<T>> getData<T>(
String endpoint, T Function(Map<String, dynamic>) fromJson) async {
String fullUrl = '$apiUrl$endpoint';
fullUrl = Uri.encodeFull(fullUrl);
try {
final response = await http.get(Uri.parse('$apiUrl$endpoint'));
final response = await http.get(Uri.parse(fullUrl));

if (response.statusCode == 200) {
List jsonResponse = json.decode(response.body);
return jsonResponse.map((item) => fromJson(item)).toList();
return jsonResponse.map((item) {
try {
return fromJson(item);
} catch (e) {
rethrow;
}
}).toList();
} else {
throw Exception('Failed to load data');
throw Exception('ERROR ${response.statusCode} Failed to load data');
}
} catch (e) {
throw Exception('Failed to load data: $e');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ClassGroupEntity {
final String file;
final String name;

ClassGroupEntity({required this.file, required this.name});

factory ClassGroupEntity.fromJson(Map<String, dynamic> json) {
return ClassGroupEntity(
file: json['file'],
name: json['name'],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import '../entities/class_group_entity.dart';

class ClassGroupModel {
final ClassGroupEntity classGroup;

ClassGroupModel({required this.classGroup});

String get name => classGroup.name;
String get file => classGroup.file;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:app_student/api/class_groups/entities/class_group_entity.dart';
import 'package:app_student/api/api_service.dart';
import 'package:app_student/api/class_groups/models/class_group_model.dart';

class ClassGroupRepository {
final ApiService apiService;

ClassGroupRepository({required this.apiService});

Future<List<ClassGroupModel>> getClasses() {
return apiService.getData('/api/classes', (item) {
final entity = ClassGroupEntity.fromJson(item);
return ClassGroupModel(classGroup: entity);
});
}
}
13 changes: 0 additions & 13 deletions frontend/app_student/lib/api/classes/entities/class.dart

This file was deleted.

This file was deleted.

31 changes: 0 additions & 31 deletions frontend/app_student/lib/api/courses/entities/course.dart

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import '../../events/entities/event_entity.dart';

class DayScheduleEntity {
final DateTime date;
final int jour;
final List<EventEntity> events;

DayScheduleEntity({
required this.date,
required this.jour,
required this.events,
});

factory DayScheduleEntity.fromJson(Map<String, dynamic> json) {
var eventsFromJson = json['events'] as List;
List<EventEntity> eventsList =
eventsFromJson.map((i) => EventEntity.fromJson(i)).toList();

DateTime date = DateTime.parse(json['date']);

return DayScheduleEntity(
date: date,
jour: json['jour'],
events: eventsList,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import '../../events/entities/event_entity.dart';
import '../entities/day_schedule_entity.dart';

class DayScheduleModel {
final DayScheduleEntity entity;

DayScheduleModel({required this.entity});

DateTime get date => entity.date;

int get jour => entity.jour;

List<EventEntity> get events => entity.events;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import 'package:app_student/api/api_service.dart';
import '../entities/day_schedule.dart';
import '../entities/day_schedule_entity.dart';

class DayScheduleRepository {
final String className;
final ApiService apiService;

DayScheduleRepository({required this.className, required this.apiService});

Future<List<DaySchedule>> getDaySchedules($className) {
Future<List<DayScheduleEntity>> getDaySchedule($className) {
return apiService.getData('/api/timetable?class_param=$className',
(item) => DaySchedule.fromJson(item));
(item) => DayScheduleEntity.fromJson(item));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class EventHoursEntity {
final String startAt;
final String endAt;

EventHoursEntity({
required this.startAt,
required this.endAt,
});

factory EventHoursEntity.fromJson(Map<String, dynamic> json) {
return EventHoursEntity(
startAt: json['startAt'],
endAt: json['endAt'],
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import '../entities/event_hours_entity.dart';

class EventHoursModel {
final EventHoursEntity entity;

EventHoursModel({required this.entity});

String get startAt => entity.startAt;
String get endAt => entity.endAt;
}
33 changes: 33 additions & 0 deletions frontend/app_student/lib/api/events/entities/event_entity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import '../../event_hours/entities/event_hours_entity.dart';

class EventEntity {
final int id;
final int creneau;
final String activite;
final String couleur;
final EventHoursEntity horaires;
final String salle;
final bool visio;

EventEntity({
required this.id,
required this.creneau,
required this.activite,
required this.couleur,
required this.horaires,
required this.salle,
required this.visio,
});

factory EventEntity.fromJson(Map<String, dynamic> json) {
return EventEntity(
creneau: json['creneau'] ?? 'null',
activite: json['activite'] ?? 'null',
id: json['id'] ?? 'null',
couleur: json['couleur'] ?? 'null',
horaires: EventHoursEntity.fromJson(json['horaire']),
salle: json['salle'] ?? 'null',
visio: json['visio'] ?? false,
);
}
}
15 changes: 15 additions & 0 deletions frontend/app_student/lib/api/events/models/event_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import '../../event_hours/models/event_hours_model.dart';
import '../entities/event_entity.dart';

class EventModel {
final EventEntity entity;

EventModel({required this.entity});

int get id => entity.id;
int get creneau => entity.creneau;
String get activite => entity.activite;
String get couleur => entity.couleur;
EventHoursModel get horaires => EventHoursModel(entity: entity.horaires);
String get salle => entity.salle;
}
14 changes: 14 additions & 0 deletions frontend/app_student/lib/api/users/entities/user_entity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:app_student/api/class_groups/entities/class_group_entity.dart';

class UserEntity {
final String ine;
final String firstName;
final DateTime birthDate;
final ClassGroupEntity? classGroup;

UserEntity(
{required this.ine,
required this.firstName,
required this.birthDate,
this.classGroup});
}
11 changes: 11 additions & 0 deletions frontend/app_student/lib/api/users/models/user_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import '../entities/user_entity.dart';

class UserModel {
final UserEntity entity;

UserModel({required this.entity});

String get file => entity.ine;
String get name => entity.firstName;
DateTime get birthDate => entity.birthDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:app_student/api/day_schedule/entities/day_schedule_entity.dart';

class WeekScheduleEntity {
final String code;
final List<DayScheduleEntity> daySchedules;

WeekScheduleEntity({
required this.code,
required this.daySchedules,
});

factory WeekScheduleEntity.fromJson(Map<String, dynamic> json) {
return WeekScheduleEntity(
code: json['code'],
daySchedules: (json['DaySchedule'] as List<dynamic>)
.map((daySchedule) =>
DayScheduleEntity.fromJson(daySchedule as Map<String, dynamic>))
.toList(),
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:app_student/api/day_schedule/entities/day_schedule_entity.dart';

import '../entities/week_schedule_entity.dart';

class WeekScheduleModel {
final WeekScheduleEntity weekSchedule;

WeekScheduleModel({
required this.weekSchedule,
});

String get code => weekSchedule.code;
List<DayScheduleEntity> get daySchedules => weekSchedule.daySchedules;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import 'package:app_student/api/api_service.dart';
import 'package:app_student/api/week_schedule/entities/week_schedule_entity.dart';
import 'package:app_student/api/week_schedule/models/week_schedule_model.dart';

class WeekScheduleRepository {
final ApiService apiService;

WeekScheduleRepository({required this.apiService, required String className});

Future<List<WeekScheduleModel>> getWeeksSchedule(className) {
return apiService.getData('/api/timetable?class_param=$className', (item) {
final entity = WeekScheduleEntity.fromJson(item);
return WeekScheduleModel(weekSchedule: entity);
});
}
}
Loading

0 comments on commit b4fd334

Please sign in to comment.