-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathevents_repository.dart
160 lines (150 loc) · 5.45 KB
/
events_repository.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import 'dart:math';
import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:ecellapp/core/res/errors.dart';
import 'package:ecellapp/core/utils/logger.dart';
import 'package:ecellapp/models/event.dart';
import 'package:http/http.dart' as http;
import 'package:ecellapp/core/res/strings.dart';
import 'package:ecellapp/core/utils/injection.dart';
import 'package:flutter/material.dart';
@immutable
abstract class EventsRepository {
/// Takes in nothing, gives the events,their details and throws a suitable exception if something goes wrong.
Future<List<Event>> getAllEvents();
}
class FakeEventsRepository implements EventsRepository {
@override
Future<List<Event>> getAllEvents() async {
// Simulate network delay
await Future.delayed(Duration(seconds: 1));
if (Random().nextBool()) {
// random network error
throw NetworkException();
} else {
var json = {
"message": "Events Fetched successfully.",
"data": [
{
"id": 2,
"name": "E-Cell",
"venue": "Audi Hall",
"date": "2020-09-29T15:21:48.828+00:00",
"time": "test_time",
"details":
"A very detailed text which is very detailed so much that it cannot be more detailed",
"details_html": "test.html",
"cover_pic":
"https://static.businessworld.in/article/article_extra_large_image/1610638130_Zle1Cn_CES_2021.jpg",
"icon":
"https://static.businessworld.in/article/article_extra_large_image/1610638130_Zle1Cn_CES_2021.jpg",
"email": "[email protected]",
"flag": true,
"year": 2019,
"ecell_user": 4,
"created_at": "2020-09-29T15:21:48.828+00:00",
"modified_at": "2020-09-29T15:21:48.828+00:00"
},
{
"id": 3,
"name": "Aavartan",
"venue": "Amul Parlor",
"date": "2020-09-29T15:21:48.828+00:00",
"time": "test_time",
"details":
"A very detailed text which is very detailed so much that it cannot be more detailed",
"details_html": "test.html",
"cover_pic":
"https://static.businessworld.in/article/article_extra_large_image/1610638130_Zle1Cn_CES_2021.jpg",
"icon":
"https://static.businessworld.in/article/article_extra_large_image/1610638130_Zle1Cn_CES_2021.jpg",
"email": "[email protected]",
"flag": true,
"year": 2019,
"ecell_user": 4,
"created_at": "2020-09-29T15:21:48.828+00:00",
"modified_at": "2020-09-29T15:21:48.828+00:00"
},
{
"id": 0,
"name": "test",
"venue": "test_venue",
"date": "2020-09-29T15:21:48.828+00:00",
"time": "test_time",
"details": "test_details",
"details_html": "test.html",
"cover_pic":
"https://static.businessworld.in/article/article_extra_large_image/1610638130_Zle1Cn_CES_2021.jpg",
"icon":
"https://static.businessworld.in/article/article_extra_large_image/1610638130_Zle1Cn_CES_2021.jpg",
"email": "[email protected]",
"flag": true,
"year": 2019,
"ecell_user": 3,
"created_at": "2020-09-29T15:21:48.828+00:00",
"modified_at": "2020-09-29T15:21:48.828+00:00"
}
]
};
List<Event> events = [];
(json["data"] as List).forEach((e) => events.add(Event.fromJson(e)));
// fake successful response (the data entered here is same as in the API Doc example)
return events;
}
}
}
class APIEventsRepository implements EventsRepository {
final String classTag = "APIgetAllEventsRepository";
@override
Future<List<Event>> getAllEvents() async {
final String tag = classTag + "getAllEvents";
http.Response response;
try {
response = await sl.get<http.Client>().get(
Uri.parse(S.getEventsUrl),
);
} catch (e) {
Log.e(tag: tag, message: "NetworkError:" + e.toString());
throw NetworkException();
}
if (response.statusCode == 200) {
Log.i(tag: tag, message: "Request Successful");
var json = jsonDecode(utf8.decode(response.bodyBytes));
List<Event> events = List.empty(growable: true);
(json["data"] as List).forEach((e) => events.add(Event.fromJson(e)));
return events;
} else if (response.statusCode == 404) {
throw ValidationException(response.body);
} else {
Log.s(
tag: tag,
message:
"Unknown response code -> ${response.statusCode}, message ->" +
response.body);
throw UnknownException();
}
}
}
class APIEventFormRepository {
Future<Map<String, dynamic>> getAllEventForm() async {
final db = FirebaseFirestore.instance;
Map<String, dynamic> eventForm = {};
try {
await db.collection('EVENTS').doc('FORMS').get().then(
(DocumentSnapshot doc) {
final data = doc.data() as Map<String, dynamic>;
eventForm = data;
print(data);
},
onError: (e) => print("Error getting document: $e"),
);
return eventForm;
} on FirebaseException catch (e) {
print(e);
throw UnknownException();
} catch (error) {
print(error);
throw UnknownException();
}
}
}