feat: 사용자 관리 API 연동 구현
- UserRemoteDataSource: 사용자 CRUD, 상태 변경, 비밀번호 변경, 중복 확인 API 구현 - UserService: DTO-Model 변환 로직 및 역할/전화번호 매핑 처리 - UserListController: ChangeNotifier 패턴 적용, 페이지네이션, 검색, 필터링 기능 추가 - UserFormController: API 연동, username 중복 확인 기능 추가 - user_form.dart: username/password 필드 추가 및 실시간 검증 - user_list_redesign.dart: Provider 패턴 적용, 무한 스크롤 구현 - equipment_out_form_controller.dart: 구문 오류 수정 - API 통합 진행률: 85% (사용자 관리 100% 완료)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'package:get_it/get_it.dart';
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:superport/core/errors/exceptions.dart';
|
||||
import 'package:superport/core/errors/failures.dart';
|
||||
import 'package:superport/data/datasources/remote/company_remote_datasource.dart';
|
||||
@@ -8,8 +9,11 @@ import 'package:superport/data/models/company/branch_dto.dart';
|
||||
import 'package:superport/models/company_model.dart';
|
||||
import 'package:superport/models/address_model.dart';
|
||||
|
||||
@lazySingleton
|
||||
class CompanyService {
|
||||
final CompanyRemoteDataSource _remoteDataSource = GetIt.instance<CompanyRemoteDataSource>();
|
||||
final CompanyRemoteDataSource _remoteDataSource;
|
||||
|
||||
CompanyService(this._remoteDataSource);
|
||||
|
||||
// 회사 목록 조회
|
||||
Future<List<Company>> getCompanies({
|
||||
@@ -19,15 +23,15 @@ class CompanyService {
|
||||
bool? isActive,
|
||||
}) async {
|
||||
try {
|
||||
final dtoList = await _remoteDataSource.getCompanies(
|
||||
final response = await _remoteDataSource.getCompanies(
|
||||
page: page,
|
||||
perPage: perPage,
|
||||
search: search,
|
||||
isActive: isActive,
|
||||
);
|
||||
|
||||
return dtoList.map((dto) => _convertListDtoToCompany(dto)).toList();
|
||||
} on ServerException catch (e) {
|
||||
return response.items.map((dto) => _convertListDtoToCompany(dto)).toList();
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to fetch company list: $e');
|
||||
@@ -50,7 +54,7 @@ class CompanyService {
|
||||
|
||||
final response = await _remoteDataSource.createCompany(request);
|
||||
return _convertResponseToCompany(response);
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to create company: $e');
|
||||
@@ -62,7 +66,7 @@ class CompanyService {
|
||||
try {
|
||||
final response = await _remoteDataSource.getCompanyDetail(id);
|
||||
return _convertResponseToCompany(response);
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to fetch company detail: $e');
|
||||
@@ -77,7 +81,7 @@ class CompanyService {
|
||||
final branches = response.branches.map((dto) => _convertBranchDtoToBranch(dto)).toList();
|
||||
|
||||
return company.copyWith(branches: branches);
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to fetch company with branches: $e');
|
||||
@@ -100,7 +104,7 @@ class CompanyService {
|
||||
|
||||
final response = await _remoteDataSource.updateCompany(id, request);
|
||||
return _convertResponseToCompany(response);
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to update company: $e');
|
||||
@@ -111,7 +115,7 @@ class CompanyService {
|
||||
Future<void> deleteCompany(int id) async {
|
||||
try {
|
||||
await _remoteDataSource.deleteCompany(id);
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to delete company: $e');
|
||||
@@ -126,7 +130,7 @@ class CompanyService {
|
||||
'id': dto.id,
|
||||
'name': dto.name,
|
||||
}).toList();
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to fetch company names: $e');
|
||||
@@ -147,7 +151,7 @@ class CompanyService {
|
||||
|
||||
final response = await _remoteDataSource.createBranch(companyId, request);
|
||||
return _convertBranchResponseToBranch(response);
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to create branch: $e');
|
||||
@@ -158,7 +162,7 @@ class CompanyService {
|
||||
try {
|
||||
final response = await _remoteDataSource.getBranchDetail(companyId, branchId);
|
||||
return _convertBranchResponseToBranch(response);
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to fetch branch detail: $e');
|
||||
@@ -178,7 +182,7 @@ class CompanyService {
|
||||
|
||||
final response = await _remoteDataSource.updateBranch(companyId, branchId, request);
|
||||
return _convertBranchResponseToBranch(response);
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to update branch: $e');
|
||||
@@ -188,7 +192,7 @@ class CompanyService {
|
||||
Future<void> deleteBranch(int companyId, int branchId) async {
|
||||
try {
|
||||
await _remoteDataSource.deleteBranch(companyId, branchId);
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to delete branch: $e');
|
||||
@@ -199,13 +203,58 @@ class CompanyService {
|
||||
try {
|
||||
final dtoList = await _remoteDataSource.getCompanyBranches(companyId);
|
||||
return dtoList.map((dto) => _convertBranchDtoToBranch(dto)).toList();
|
||||
} on ServerException catch (e) {
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to fetch company branches: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// 회사-지점 전체 정보 조회
|
||||
Future<List<CompanyWithBranches>> getCompaniesWithBranches() async {
|
||||
try {
|
||||
return await _remoteDataSource.getCompaniesWithBranches();
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to fetch companies with branches: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// 회사명 중복 확인
|
||||
Future<bool> checkDuplicateCompany(String name) async {
|
||||
try {
|
||||
return await _remoteDataSource.checkDuplicateCompany(name);
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to check duplicate: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// 회사 검색
|
||||
Future<List<Company>> searchCompanies(String query) async {
|
||||
try {
|
||||
final dtoList = await _remoteDataSource.searchCompanies(query);
|
||||
return dtoList.map((dto) => _convertListDtoToCompany(dto)).toList();
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to search companies: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// 회사 활성 상태 변경
|
||||
Future<void> updateCompanyStatus(int id, bool isActive) async {
|
||||
try {
|
||||
await _remoteDataSource.updateCompanyStatus(id, isActive);
|
||||
} on ApiException catch (e) {
|
||||
throw Failure(message: e.message);
|
||||
} catch (e) {
|
||||
throw Failure(message: 'Failed to update company status: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// 변환 헬퍼 메서드들
|
||||
Company _convertListDtoToCompany(CompanyListDto dto) {
|
||||
return Company(
|
||||
|
||||
231
lib/services/user_service.dart
Normal file
231
lib/services/user_service.dart
Normal file
@@ -0,0 +1,231 @@
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:superport/core/errors/exceptions.dart';
|
||||
import 'package:superport/data/datasources/remote/user_remote_datasource.dart';
|
||||
import 'package:superport/data/models/user/user_dto.dart';
|
||||
import 'package:superport/models/user_model.dart';
|
||||
|
||||
@lazySingleton
|
||||
class UserService {
|
||||
final UserRemoteDataSource _userRemoteDataSource;
|
||||
|
||||
UserService() : _userRemoteDataSource = UserRemoteDataSource();
|
||||
|
||||
/// 사용자 목록 조회
|
||||
Future<List<User>> getUsers({
|
||||
int page = 1,
|
||||
int perPage = 20,
|
||||
bool? isActive,
|
||||
int? companyId,
|
||||
String? role,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _userRemoteDataSource.getUsers(
|
||||
page: page,
|
||||
perPage: perPage,
|
||||
isActive: isActive,
|
||||
companyId: companyId,
|
||||
role: role != null ? _mapRoleToApi(role) : null,
|
||||
);
|
||||
|
||||
return response.users.map((dto) => _userDtoToModel(dto)).toList();
|
||||
} catch (e) {
|
||||
throw Exception('사용자 목록 조회 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 특정 사용자 조회
|
||||
Future<User> getUser(int id) async {
|
||||
try {
|
||||
final dto = await _userRemoteDataSource.getUser(id);
|
||||
return _userDtoToModel(dto);
|
||||
} catch (e) {
|
||||
throw Exception('사용자 조회 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 사용자 생성
|
||||
Future<User> createUser({
|
||||
required String username,
|
||||
required String email,
|
||||
required String password,
|
||||
required String name,
|
||||
required String role,
|
||||
required int companyId,
|
||||
int? branchId,
|
||||
String? phone,
|
||||
String? position,
|
||||
}) async {
|
||||
try {
|
||||
final request = CreateUserRequest(
|
||||
username: username,
|
||||
email: email,
|
||||
password: password,
|
||||
name: name,
|
||||
role: _mapRoleToApi(role),
|
||||
companyId: companyId,
|
||||
branchId: branchId,
|
||||
phone: phone,
|
||||
);
|
||||
|
||||
final dto = await _userRemoteDataSource.createUser(request);
|
||||
return _userDtoToModel(dto);
|
||||
} catch (e) {
|
||||
throw Exception('사용자 생성 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 사용자 정보 수정
|
||||
Future<User> updateUser(
|
||||
int id, {
|
||||
String? name,
|
||||
String? email,
|
||||
String? password,
|
||||
String? phone,
|
||||
int? companyId,
|
||||
int? branchId,
|
||||
String? role,
|
||||
String? position,
|
||||
}) async {
|
||||
try {
|
||||
final request = UpdateUserRequest(
|
||||
name: name,
|
||||
email: email,
|
||||
password: password,
|
||||
phone: phone,
|
||||
companyId: companyId,
|
||||
branchId: branchId,
|
||||
role: role != null ? _mapRoleToApi(role) : null,
|
||||
);
|
||||
|
||||
final dto = await _userRemoteDataSource.updateUser(id, request);
|
||||
return _userDtoToModel(dto);
|
||||
} catch (e) {
|
||||
throw Exception('사용자 수정 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 사용자 삭제
|
||||
Future<void> deleteUser(int id) async {
|
||||
try {
|
||||
await _userRemoteDataSource.deleteUser(id);
|
||||
} catch (e) {
|
||||
throw Exception('사용자 삭제 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 사용자 상태 변경
|
||||
Future<User> changeUserStatus(int id, bool isActive) async {
|
||||
try {
|
||||
final request = ChangeStatusRequest(isActive: isActive);
|
||||
final dto = await _userRemoteDataSource.changeUserStatus(id, request);
|
||||
return _userDtoToModel(dto);
|
||||
} catch (e) {
|
||||
throw Exception('사용자 상태 변경 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 비밀번호 변경
|
||||
Future<void> changePassword(
|
||||
int id,
|
||||
String currentPassword,
|
||||
String newPassword,
|
||||
) async {
|
||||
try {
|
||||
final request = ChangePasswordRequest(
|
||||
currentPassword: currentPassword,
|
||||
newPassword: newPassword,
|
||||
);
|
||||
await _userRemoteDataSource.changePassword(id, request);
|
||||
} catch (e) {
|
||||
throw Exception('비밀번호 변경 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 사용자명 중복 확인
|
||||
Future<bool> checkDuplicateUsername(String username) async {
|
||||
try {
|
||||
return await _userRemoteDataSource.checkDuplicateUsername(username);
|
||||
} catch (e) {
|
||||
throw Exception('중복 확인 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// 사용자 검색
|
||||
Future<List<User>> searchUsers({
|
||||
required String query,
|
||||
int? companyId,
|
||||
String? status,
|
||||
String? permissionLevel,
|
||||
int page = 1,
|
||||
int perPage = 20,
|
||||
}) async {
|
||||
try {
|
||||
final response = await _userRemoteDataSource.searchUsers(
|
||||
query: query,
|
||||
companyId: companyId,
|
||||
status: status,
|
||||
permissionLevel: permissionLevel != null
|
||||
? _mapRoleToApi(permissionLevel)
|
||||
: null,
|
||||
page: page,
|
||||
perPage: perPage,
|
||||
);
|
||||
|
||||
return response.users.map((dto) => _userDtoToModel(dto)).toList();
|
||||
} catch (e) {
|
||||
throw Exception('사용자 검색 실패: ${e.toString()}');
|
||||
}
|
||||
}
|
||||
|
||||
/// DTO를 Model로 변환
|
||||
User _userDtoToModel(UserDto dto) {
|
||||
return User(
|
||||
id: dto.id,
|
||||
companyId: dto.companyId ?? 0,
|
||||
branchId: dto.branchId,
|
||||
name: dto.name,
|
||||
role: _mapRoleFromApi(dto.role),
|
||||
position: null, // API에서 position 정보가 없음
|
||||
email: dto.email,
|
||||
phoneNumbers: dto.phone != null
|
||||
? [{'type': '기본', 'number': dto.phone!}]
|
||||
: [],
|
||||
username: dto.username,
|
||||
isActive: dto.isActive,
|
||||
createdAt: dto.createdAt,
|
||||
updatedAt: dto.updatedAt,
|
||||
);
|
||||
}
|
||||
|
||||
/// 권한을 API 형식으로 변환
|
||||
String _mapRoleToApi(String role) {
|
||||
switch (role) {
|
||||
case 'S':
|
||||
return 'admin';
|
||||
case 'M':
|
||||
return 'staff';
|
||||
default:
|
||||
return 'staff';
|
||||
}
|
||||
}
|
||||
|
||||
/// API 권한을 앱 형식으로 변환
|
||||
String _mapRoleFromApi(String role) {
|
||||
switch (role) {
|
||||
case 'admin':
|
||||
return 'S';
|
||||
case 'manager':
|
||||
return 'M';
|
||||
case 'staff':
|
||||
return 'M';
|
||||
default:
|
||||
return 'M';
|
||||
}
|
||||
}
|
||||
|
||||
/// 전화번호 목록에서 첫 번째 전화번호 추출
|
||||
String? getPhoneForApi(List<Map<String, String>> phoneNumbers) {
|
||||
if (phoneNumbers.isEmpty) return null;
|
||||
return phoneNumbers.first['number'];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user