feat: Refactor code structure and optimize performance across multiple modules
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
class MutateKitDto {
|
||||
String id;
|
||||
String? photo;
|
||||
|
||||
MutateKitDto({
|
||||
required this.id,
|
||||
this.photo,
|
||||
});
|
||||
|
||||
Map<String, String> toJson() => {
|
||||
'id': id,
|
||||
if (photo != null) 'photo': photo!,
|
||||
};
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return toJson().toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
const String confirmStaffUniformsMutation = '''
|
||||
mutation ConfirmStaffUniforms(\$skillId: String!, \$uniforms: [ConfirmStaffUniformInput!]!) {
|
||||
confirm_staff_uniforms(skill_id: \$skillId, uniforms: \$uniforms)
|
||||
}
|
||||
''';
|
||||
|
||||
const String confirmStaffEquipmentsMutation = '''
|
||||
mutation ConfirmStaffEquipments(\$skillId: String!, \$equipments: [ConfirmStaffEquipmentInput!]!) {
|
||||
confirm_staff_equipments(skill_id: \$skillId, equipments: \$equipments)
|
||||
}
|
||||
''';
|
||||
|
||||
const String uploadImageMutation = '''
|
||||
mutation UploadImage(\$file: Upload!) {
|
||||
upload_file(file: \$file) {
|
||||
token
|
||||
url
|
||||
}
|
||||
}
|
||||
''';
|
||||
@@ -0,0 +1,85 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:http/http.dart';
|
||||
import 'package:http_parser/http_parser.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:krow/core/application/clients/api/api_client.dart';
|
||||
import 'package:krow/core/application/clients/api/api_exception.dart';
|
||||
import 'package:krow/core/application/clients/api/gql.dart';
|
||||
import 'package:krow/core/data/models/staff_role.dart';
|
||||
import 'package:krow/features/profile/role_kit/data/models/mutate_kit_dto.dart';
|
||||
import 'package:krow/features/profile/role_kit/data/staf_kit_gql.dart';
|
||||
|
||||
@injectable
|
||||
class StaffRoleKitApiProvider {
|
||||
final ApiClient _apiClient;
|
||||
|
||||
StaffRoleKitApiProvider(this._apiClient);
|
||||
|
||||
Future<List<StaffRole>> fetchStaffRoles() async {
|
||||
var result = await _apiClient.query(schema: getStaffRolesQuery);
|
||||
|
||||
if (result.hasException) {
|
||||
throw parseBackendError(result.exception);
|
||||
}
|
||||
|
||||
return result.data!['staff_roles'].map<StaffRole>((e) {
|
||||
return StaffRole.fromJson(e);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
Future<void> putUniform(String skillId, List<MutateKitDto> kits) async {
|
||||
final Map<String, dynamic> variables = {
|
||||
'skillId': skillId,
|
||||
'uniforms': kits,
|
||||
};
|
||||
var result = await _apiClient.mutate(
|
||||
schema: confirmStaffUniformsMutation, body: variables);
|
||||
|
||||
if (result.hasException) {
|
||||
throw parseBackendError(result.exception);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> putEquipment(String skillId, List<MutateKitDto> kits) async {
|
||||
final Map<String, dynamic> variables = {
|
||||
'skillId': skillId,
|
||||
'equipments': kits,
|
||||
};
|
||||
var result = await _apiClient.mutate(
|
||||
schema: confirmStaffEquipmentsMutation, body: variables);
|
||||
if (result.hasException) {
|
||||
throw parseBackendError(result.exception);
|
||||
}
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>> uploadImage(String imagePath) async {
|
||||
final ApiClient apiClient = ApiClient();
|
||||
|
||||
var byteData = File(imagePath).readAsBytesSync();
|
||||
|
||||
var multipartFile = MultipartFile.fromBytes(
|
||||
'photo',
|
||||
byteData,
|
||||
filename: '${DateTime.now().millisecondsSinceEpoch}.jpg',
|
||||
contentType: MediaType('image', 'jpg'),
|
||||
);
|
||||
|
||||
final Map<String, dynamic> variables = {
|
||||
'file': multipartFile,
|
||||
};
|
||||
|
||||
final result = await apiClient.mutate(
|
||||
schema: uploadImageMutation,
|
||||
body: variables,
|
||||
);
|
||||
|
||||
if (result.hasException) {
|
||||
debugPrint(result.exception.toString());
|
||||
} else {
|
||||
return result.data!['upload_file'];
|
||||
}
|
||||
return <String, dynamic>{};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
import 'package:krow/core/data/models/staff_role.dart';
|
||||
import 'package:krow/features/profile/role_kit/domain/staff_role_kit_repository_impl.dart';
|
||||
|
||||
import 'models/mutate_kit_dto.dart';
|
||||
|
||||
abstract class StaffRoleKitRepository {
|
||||
Future<List<StaffRole>> getStaffRole();
|
||||
|
||||
Future<void> putKit(
|
||||
String skillId, List<MutateKitDto> kits, RoleKitType type);
|
||||
|
||||
Future<Map<String, dynamic>> uploadImage(String imagePath);
|
||||
}
|
||||
Reference in New Issue
Block a user