-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile_api_service_impl.dart
More file actions
167 lines (139 loc) · 4.4 KB
/
Copy pathprofile_api_service_impl.dart
File metadata and controls
167 lines (139 loc) · 4.4 KB
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
161
162
163
164
165
166
167
// coverage:ignore-file
// profile_api_service_impl.dart
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lam7a/core/services/api_service.dart';
import 'package:lam7a/features/profile/dtos/profile_dto.dart';
import 'profile_api_service.dart';
class ProfileApiServiceImpl implements ProfileApiService {
final Ref ref;
ProfileApiServiceImpl(this.ref);
ApiService get _api => ref.read(apiServiceProvider);
@override
Future<ProfileDto> getProfileByUsername(String username) async {
final res = await _api.get(endpoint: "/profile/username/$username");
return ProfileDto.fromJson(_extractObject(res));
}
@override
Future<ProfileDto> getMyProfile() async {
final res = await _api.get(endpoint: "/profile/me");
return ProfileDto.fromJson(_extractObject(res));
}
@override
Future<ProfileDto> updateMyProfile(Map<String, dynamic> body) async {
final res = await _api.patch(endpoint: "/profile/me", data: body);
return ProfileDto.fromJson(_extractObject(res));
}
Map<String, dynamic> _extractObject(dynamic res) {
if (res is Map && res.containsKey("data")) {
return Map<String, dynamic>.from(res["data"]);
}
return Map<String, dynamic>.from(res);
}
List<Map<String, dynamic>> _extractList(dynamic res) {
if (res is Map && res.containsKey("data")) {
return List<Map<String, dynamic>>.from(res["data"]);
}
return List<Map<String, dynamic>>.from(res);
}
Future<String> _extractUploadUrl(dynamic res) {
final data = _extractObject(res);
return Future.value(
data["profile_image_url"] ??
data["banner_image_url"] ??
data["url"] ??
"",
);
}
@override
Future<String> uploadProfilePicture(String filePath) async {
final form = FormData.fromMap({'file': await MultipartFile.fromFile(filePath)});
final res = await _api.post(endpoint: "/profile/me/profile-picture", data: form);
return _extractUploadUrl(res);
}
@override
Future<String> uploadBanner(String filePath) async {
final form = FormData.fromMap({'file': await MultipartFile.fromFile(filePath)});
final res = await _api.post(endpoint: "/profile/me/banner", data: form);
return _extractUploadUrl(res);
}
@override
Future<void> followUser(int id) async {
await _api.post(endpoint: "/users/$id/follow");
}
@override
Future<void> unfollowUser(int id) async {
await _api.delete(endpoint: "/users/$id/follow");
}
@override
Future<List<Map<String, dynamic>>> getFollowers(int id) async {
final res = await _api.get(endpoint: "/users/$id/followers");
return _extractList(res);
}
@override
Future<List<Map<String, dynamic>>> getFollowing(int id) async {
final res = await _api.get(endpoint: "/users/$id/following");
return _extractList(res);
}
@override
Future<void> muteUser(int id) async {
await _api.post(endpoint: "/users/$id/mute");
}
@override
Future<void> unmuteUser(int id) async {
await _api.delete(endpoint: "/users/$id/mute");
}
@override
Future<void> blockUser(int id) async {
await _api.post(endpoint: "/users/$id/block");
}
@override
Future<void> unblockUser(int id) async {
await _api.delete(endpoint: "/users/$id/block");
}
@override
Future<List<Map<String, dynamic>>> getProfilePosts(
String userId, int page, int limit) async {
final res = await _api.get(
endpoint: "/posts/profile/$userId",
queryParameters: {
"page": page,
"limit": limit,
},
);
return _extractList(res);
}
@override
Future<List<Map<String, dynamic>>> getProfileReplies(
String userId, int page, int limit) async {
final res = await _api.get(
endpoint: "/posts/profile/$userId/replies",
queryParameters: {
"page": page,
"limit": limit,
},
);
return _extractList(res);
}
@override
Future<List<Map<String, dynamic>>> getProfileLikes(
String userId, int page, int limit) async {
final res = await _api.get(
endpoint: "/posts/liked/$userId",
queryParameters: {
"page": page,
"limit": limit,
},
);
return _extractList(res);
}
@override
Future<void> deleteProfilePicture() async {
await _api.delete(endpoint: '/profile/me/profile-picture');
}
@override
Future<void> deleteBanner() async {
await _api.delete(endpoint: '/profile/me/banner');
}
}