feat: Refactor code structure and optimize performance across multiple modules
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
const String attachStaffRolesMutation = r'''
|
||||
mutation AttachStaffRoles($roles: [AttachStaffSkillInput!]!) {
|
||||
attach_staff_roles(roles: $roles){}
|
||||
}
|
||||
''';
|
||||
|
||||
const String detachStaffRolesMutation = r'''
|
||||
mutation DetachStaffRoles($ids: [ID!]!) {
|
||||
detach_staff_roles(ids: $ids)
|
||||
}
|
||||
''';
|
||||
@@ -0,0 +1,65 @@
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:krow/core/application/clients/api/api_client.dart';
|
||||
import 'package:krow/core/application/clients/api/gql.dart';
|
||||
import 'package:krow/core/data/models/skill.dart';
|
||||
import 'package:krow/core/data/models/staff_role.dart';
|
||||
import 'package:krow/features/profile/role/data/gql.dart';
|
||||
|
||||
@injectable
|
||||
class StaffRoleApiProvider {
|
||||
final ApiClient _apiClient;
|
||||
|
||||
StaffRoleApiProvider(this._apiClient);
|
||||
|
||||
Future<List<StaffRole>> fetchStaffRoles() async {
|
||||
var result = await _apiClient.query(schema: getStaffRolesQuery);
|
||||
|
||||
if (result.hasException) {
|
||||
throw Exception(result.exception.toString());
|
||||
}
|
||||
|
||||
return result.data!['staff_roles'].map<StaffRole>((e) {
|
||||
return StaffRole.fromJson(e);
|
||||
}).toList();
|
||||
}
|
||||
|
||||
Future<List<Skill>> fetchSkills() {
|
||||
return _apiClient.query(schema: getSkillsQuery).then((result) {
|
||||
if (result.hasException) {
|
||||
throw Exception(result.exception.toString());
|
||||
}
|
||||
|
||||
return result.data!['skills'].map<Skill>((e) {
|
||||
return Skill.fromJson(e);
|
||||
}).toList();
|
||||
});
|
||||
}
|
||||
|
||||
Future<String> saveStaffRole(StaffRole role) {
|
||||
return _apiClient.mutate(schema: attachStaffRolesMutation, body: {
|
||||
'roles': [
|
||||
{
|
||||
'level': role.level.toString().split('.').last,
|
||||
'experience': role.experience,
|
||||
'skill_id': role.skill!.id,
|
||||
}
|
||||
],
|
||||
}).then((result) {
|
||||
if (result.hasException) {
|
||||
throw Exception(result.exception.toString());
|
||||
}
|
||||
|
||||
return '';
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> deleteStaffRole(StaffRole role) {
|
||||
return _apiClient.mutate(schema: detachStaffRolesMutation, body: {
|
||||
'ids': [role.skill?.id],
|
||||
}).then((result) {
|
||||
if (result.hasException) {
|
||||
throw Exception(result.exception.toString());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:krow/core/data/models/skill.dart';
|
||||
import 'package:krow/core/data/models/staff_role.dart';
|
||||
import 'package:krow/features/profile/role/data/staff_role_api.dart';
|
||||
import 'package:krow/features/profile/role/domain/staff_role_repository.dart';
|
||||
|
||||
@Injectable(as: StaffRoleRepository)
|
||||
class StaffRoleRepositoryImpl extends StaffRoleRepository {
|
||||
final StaffRoleApiProvider _staffRoleApi;
|
||||
|
||||
StaffRoleRepositoryImpl(this._staffRoleApi);
|
||||
|
||||
@override
|
||||
Future<List<StaffRole>> getStaffRole() async {
|
||||
return _staffRoleApi.fetchStaffRoles();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<Skill>> getSkills() {
|
||||
return _staffRoleApi.fetchSkills();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<String> saveStaffRole(StaffRole role) {
|
||||
return _staffRoleApi.saveStaffRole(role);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> deleteStaffRole(StaffRole role) {
|
||||
return _staffRoleApi.deleteStaffRole(role);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user