Skip to content

Commit 89e2e0e

Browse files
committed
feat: intialize user repository
1 parent ca3390e commit 89e2e0e

7 files changed

Lines changed: 180 additions & 14 deletions

File tree

lib/data/services/remote/api_service.dart

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import 'package:dio/dio.dart';
22
import 'package:flutter/foundation.dart';
33

44
import '../../config/config.dart';
5-
import '../local/storage_service.dart';
65

76
class ApiService {
87
/// Service initializer
@@ -15,6 +14,7 @@ class ApiService {
1514
/// Safe method to send GET request to an endpoint **below** [Environment.baseUrl].
1615
static Future<Map<String, dynamic>> get(
1716
String endpoint, {
17+
Map<String, dynamic>? headers,
1818
Map<String, dynamic>? query,
1919
}) async {
2020
Response? response;
@@ -26,7 +26,7 @@ class ApiService {
2626
validateStatus: (status) {
2727
return status! < 500;
2828
},
29-
headers: {'authorization': StorageService.authToken},
29+
headers: headers,
3030
),
3131
);
3232
return {
@@ -49,7 +49,8 @@ class ApiService {
4949
/// Safe method to send POST request to an endpoint **below** [Environment.baseUrl].
5050
static Future<Map<String, dynamic>> post(
5151
String endpoint, {
52-
required Map<String, dynamic> data,
52+
Map<String, dynamic>? headers,
53+
Map<String, dynamic>? data,
5354
}) async {
5455
Response? response;
5556
try {
@@ -60,7 +61,7 @@ class ApiService {
6061
validateStatus: (status) {
6162
return status! < 500;
6263
},
63-
headers: {'authorization': StorageService.authToken},
64+
headers: headers,
6465
),
6566
);
6667
return {
@@ -83,7 +84,8 @@ class ApiService {
8384
/// Safe method to send PUT request to an endpoint **below** [Environment.baseUrl].
8485
static Future<Map<String, dynamic>> put(
8586
String endpoint, {
86-
required Map<String, dynamic> data,
87+
Map<String, dynamic>? headers,
88+
Map<String, dynamic>? data,
8789
}) async {
8890
Response? response;
8991
try {
@@ -94,7 +96,7 @@ class ApiService {
9496
validateStatus: (status) {
9597
return status! < 500;
9698
},
97-
headers: {'authorization': StorageService.authToken},
99+
headers: headers,
98100
),
99101
);
100102
return {

lib/domain/models/handle.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import 'package:freezed_annotation/freezed_annotation.dart';
2+
3+
part 'handle.freezed.dart';
4+
part 'handle.g.dart';
5+
6+
@freezed
7+
class Handle with _$Handle {
8+
factory Handle({
9+
@JsonKey(name: 'handle.codechef') String? codechef,
10+
@JsonKey(name: 'handle.codeforces') String? codeforces,
11+
@JsonKey(name: 'handle.hackerrank') String? hackerrank,
12+
@JsonKey(name: 'handle.spoj') String? spoj,
13+
}) = _Handle;
14+
15+
factory Handle.fromJson(Map<String, dynamic> json) => _$HandleFromJson(json);
16+
}

lib/domain/models/sign_up.dart

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'package:flutter/foundation.dart';
2+
import 'package:freezed_annotation/freezed_annotation.dart';
3+
4+
import 'handle.dart';
5+
6+
part 'sign_up.freezed.dart';
7+
part 'sign_up.g.dart';
8+
9+
@freezed
10+
class SignUp with _$SignUp {
11+
factory SignUp({
12+
required String email,
13+
required String username,
14+
required String password,
15+
String? fullname,
16+
String? institute,
17+
Handle? handle,
18+
}) = _SignUp;
19+
20+
factory SignUp.fromJson(Map<String, dynamic> json) => _$SignUpFromJson(json);
21+
}

lib/domain/repositories/SAMPLE

Whitespace-only changes.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
import '../../data/services/local/storage_service.dart';
2+
3+
import '../../data/services/remote/api_service.dart';
4+
import '../models/sign_up.dart';
5+
6+
class UserRepository {
7+
const UserRepository();
8+
9+
Future<bool> isEmailAvailable(String email) async {
10+
final endPoint = 'user/available?email=$email';
11+
12+
try {
13+
await ApiService.post(
14+
endPoint,
15+
);
16+
return true;
17+
} on Exception catch (err) {
18+
throw Exception(err.toString());
19+
}
20+
}
21+
22+
Future<bool> isUsernameAvailable(String username) async {
23+
final endpoint = 'user/available?username=$username';
24+
25+
try {
26+
await ApiService.post(
27+
endpoint,
28+
);
29+
30+
return true;
31+
} on Exception catch (err) {
32+
throw Exception(err.toString());
33+
}
34+
}
35+
36+
Future<String> signUp(SignUp details) async {
37+
const endpoint = 'user/signup';
38+
final data = {...details.toJson(), ...details.handle?.toJson() ?? {}}
39+
..remove('handle');
40+
try {
41+
final response = await ApiService.post(
42+
endpoint,
43+
data: data,
44+
);
45+
46+
return response['id'];
47+
} on Exception catch (err) {
48+
throw Exception(err.toString());
49+
}
50+
}
51+
52+
Future<String> login(String username, String password) async {
53+
const endpoint = 'user/login';
54+
55+
try {
56+
final response = await ApiService.post(
57+
endpoint,
58+
data: {
59+
'username': username,
60+
'password': password,
61+
},
62+
);
63+
64+
return response['token'];
65+
} on Exception catch (err) {
66+
throw Exception(err.toString());
67+
}
68+
}
69+
70+
Future<bool> logout() async {
71+
const endPoint = 'user/logout';
72+
73+
try {
74+
await ApiService.post(
75+
endPoint,
76+
headers: {'authorization': StorageService.authToken},
77+
);
78+
79+
return true;
80+
} on Exception catch (err) {
81+
throw Exception(err.toString());
82+
}
83+
}
84+
85+
Future<bool> sendVerifyEmail(String uid) async {
86+
final endpoint = 'user/send-verify-email/$uid';
87+
88+
try {
89+
await ApiService.post(
90+
endpoint,
91+
);
92+
93+
return true;
94+
} on Exception catch (err) {
95+
throw Exception(err.toString());
96+
}
97+
}
98+
99+
Future<bool> resetPassword(String email) async {
100+
const endpoint = 'user/password-reset-email';
101+
102+
try {
103+
await ApiService.post(
104+
endpoint,
105+
data: {'email': email},
106+
);
107+
108+
return true;
109+
} on Exception catch (err) {
110+
throw Exception(err.toString());
111+
}
112+
}
113+
}

lib/presentation/core/main_app.dart

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import 'package:flutter/material.dart';
2+
import 'package:flutter_bloc/flutter_bloc.dart';
3+
4+
import '../../data/services/local/storage_service.dart';
5+
import '../../data/services/remote/api_service.dart';
6+
import '../../domain/repositories/user_repository.dart';
27

38
class Codephile extends StatelessWidget {
49
const Codephile({Key? key}) : super(key: key);
@@ -11,8 +16,17 @@ class Codephile extends StatelessWidget {
1116
}
1217

1318
static Future<Widget> run() async {
14-
// TODO(BURG3R5): Initialize services.
15-
// TODO(BURG3R5): Wrap `Codephile` widget with `RepositoryProvider`s.
16-
return const Codephile();
19+
// Service
20+
ApiService.init();
21+
StorageService.init();
22+
23+
return MultiRepositoryProvider(
24+
providers: <RepositoryProvider>[
25+
RepositoryProvider(
26+
create: (context) => const UserRepository(),
27+
),
28+
],
29+
child: const Codephile(),
30+
);
1731
}
1832
}

pubspec.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ packages:
2828
name: async
2929
url: "https://pub.dartlang.org"
3030
source: hosted
31-
version: "2.8.1"
31+
version: "2.8.2"
3232
bloc:
3333
dependency: transitive
3434
description:
@@ -105,7 +105,7 @@ packages:
105105
name: characters
106106
url: "https://pub.dartlang.org"
107107
source: hosted
108-
version: "1.1.0"
108+
version: "1.2.0"
109109
charcode:
110110
dependency: transitive
111111
description:
@@ -435,7 +435,7 @@ packages:
435435
name: matcher
436436
url: "https://pub.dartlang.org"
437437
source: hosted
438-
version: "0.12.10"
438+
version: "0.12.11"
439439
meta:
440440
dependency: transitive
441441
description:
@@ -713,7 +713,7 @@ packages:
713713
name: test_api
714714
url: "https://pub.dartlang.org"
715715
source: hosted
716-
version: "0.4.2"
716+
version: "0.4.3"
717717
timing:
718718
dependency: transitive
719719
description:
@@ -741,7 +741,7 @@ packages:
741741
name: vector_math
742742
url: "https://pub.dartlang.org"
743743
source: hosted
744-
version: "2.1.0"
744+
version: "2.1.1"
745745
watcher:
746746
dependency: transitive
747747
description:

0 commit comments

Comments
 (0)