Overview
Use cases implement the business logic for apiary management operations in the Softbee application. Following Clean Architecture principles, these use cases orchestrate the flow of data between the presentation layer and the repository layer.
All use cases follow the UseCase interface pattern and return Either<Failure, T> types for functional error handling.
Source: lib/feature/apiaries/domain/usecases/
GetApiariesUseCase
Retrieves all apiaries for the authenticated user.
Source: lib/feature/apiaries/domain/usecases/get_apiaries.dart
Class Definition
class GetApiariesUseCase implements UseCase < List < Apiary >, NoParams > {
final ApiaryRepository repository;
GetApiariesUseCase ( this .repository);
@override
Future < Either < Failure , List < Apiary >>> call ( NoParams params) async {
return await repository. getApiaries ();
}
}
Parameters
This use case requires no parameters. Pass NoParams() when calling.
Returns
A list of all apiaries belonging to the authenticated user.
AuthFailure: No authentication token found
ServerFailure: Network or server error occurred
Usage Example
final getApiaries = GetApiariesUseCase (apiaryRepository);
final result = await getApiaries ( NoParams ());
result. fold (
(failure) {
// Handle error
if (failure is AuthFailure ) {
print ( 'User not authenticated' );
} else if (failure is ServerFailure ) {
print ( 'Server error: ${ failure . message } ' );
}
},
(apiaries) {
// Success - display apiaries
print ( 'Found ${ apiaries . length } apiaries' );
for ( var apiary in apiaries) {
print ( ' ${ apiary . name } : ${ apiary . beehivesCount } hives' );
}
},
);
CreateApiaryUseCase
Creates a new apiary for the user.
Source: lib/feature/apiaries/domain/usecases/create_apiary_usecase.dart
Class Definition
class CreateApiaryUseCase implements UseCase < Apiary , CreateApiaryParams > {
final ApiaryRepository repository;
CreateApiaryUseCase ( this .repository);
@override
Future < Either < Failure , Apiary >> call ( CreateApiaryParams params) async {
return await repository. createApiary (
params.userId,
params.name,
params.location,
params.beehivesCount,
params.treatments,
);
}
}
Parameters
The use case accepts a CreateApiaryParams object:
The ID of the user creating the apiary. Used for ownership and authorization.
The name of the new apiary. Must be a non-empty string.
Optional location or address of the apiary.
Optional initial count of beehives in the apiary.
Whether treatments are being applied to beehives in this apiary.
Returns
The newly created apiary with server-generated ID and timestamps.
AuthFailure: No authentication token found
ServerFailure: Network or server error occurred
ValidationFailure: Invalid input parameters
Usage Example
final createApiary = CreateApiaryUseCase (apiaryRepository);
final params = CreateApiaryParams (
userId : 'user_123' ,
name : 'Sunset Apiary' ,
location : '456 Meadow Lane' ,
beehivesCount : 10 ,
treatments : false ,
);
final result = await createApiary (params);
result. fold (
(failure) {
print ( 'Failed to create apiary: ${ failure . message } ' );
},
(apiary) {
print ( 'Created apiary: ${ apiary . name } (ID: ${ apiary . id } )' );
},
);
UpdateApiaryUseCase
Updates an existing apiary’s information.
Source: lib/feature/apiaries/domain/usecases/update_apiary_usecase.dart
Class Definition
class UpdateApiaryUseCase implements UseCase < Apiary , UpdateApiaryParams > {
final ApiaryRepository repository;
UpdateApiaryUseCase ( this .repository);
@override
Future < Either < Failure , Apiary >> call ( UpdateApiaryParams params) async {
return await repository. updateApiary (
params.apiaryId,
params.userId,
params.name,
params.location,
params.beehivesCount,
params.treatments,
);
}
}
Parameters
The use case accepts an UpdateApiaryParams object:
The ID of the apiary to update.
The ID of the user performing the update. Used for authorization to ensure only the owner can update.
New name for the apiary. If null, the name is not updated.
New location for the apiary. If null, the location is not updated.
New beehive count. If null, the count is not updated.
New treatments status. If null, the status is not updated.
Returns
The updated apiary entity with all current values.
AuthFailure: No authentication token found or user not authorized
ServerFailure: Network or server error occurred
NotFoundFailure: Apiary with the given ID does not exist
Usage Example
final updateApiary = UpdateApiaryUseCase (apiaryRepository);
final params = UpdateApiaryParams (
apiaryId : 'apiary_123' ,
userId : 'user_123' ,
name : 'Sunset Apiary - Expanded' ,
beehivesCount : 15 ,
treatments : true ,
);
final result = await updateApiary (params);
result. fold (
(failure) {
if (failure is NotFoundFailure ) {
print ( 'Apiary not found' );
} else {
print ( 'Update failed: ${ failure . message } ' );
}
},
(apiary) {
print ( 'Updated apiary: ${ apiary . name } ' );
print ( 'New hive count: ${ apiary . beehivesCount } ' );
},
);
DeleteApiaryUseCase
Deletes an apiary from the system.
Source: lib/feature/apiaries/domain/usecases/delete_apiary_usecase.dart
Class Definition
class DeleteApiaryUseCase implements UseCase < void , DeleteApiaryParams > {
final ApiaryRepository repository;
DeleteApiaryUseCase ( this .repository);
@override
Future < Either < Failure , void >> call ( DeleteApiaryParams params) async {
return await repository. deleteApiary (params.apiaryId, params.userId);
}
}
Parameters
The use case accepts a DeleteApiaryParams object:
The ID of the apiary to delete.
The ID of the user performing the deletion. Used for authorization to ensure only the owner can delete.
Returns
Returns void on successful deletion.
AuthFailure: No authentication token found or user not authorized
ServerFailure: Network or server error occurred
NotFoundFailure: Apiary with the given ID does not exist
Usage Example
final deleteApiary = DeleteApiaryUseCase (apiaryRepository);
final params = DeleteApiaryParams (
apiaryId : 'apiary_123' ,
userId : 'user_123' ,
);
final result = await deleteApiary (params);
result. fold (
(failure) {
print ( 'Deletion failed: ${ failure . message } ' );
},
(_) {
print ( 'Apiary successfully deleted' );
},
);
Dependency Injection
All use cases require an ApiaryRepository instance. Here’s how to set up dependency injection:
import 'package:get_it/get_it.dart' ;
final getIt = GetIt .instance;
void setupApiaryDependencies () {
// Register repository
getIt. registerLazySingleton < ApiaryRepository >(
() => ApiaryRepositoryImpl (
remoteDataSource : getIt (),
localDataSource : getIt (),
),
);
// Register use cases
getIt. registerLazySingleton (() => GetApiariesUseCase ( getIt ()));
getIt. registerLazySingleton (() => CreateApiaryUseCase ( getIt ()));
getIt. registerLazySingleton (() => UpdateApiaryUseCase ( getIt ()));
getIt. registerLazySingleton (() => DeleteApiaryUseCase ( getIt ()));
}
Error Handling
All use cases return Either<Failure, T> for functional error handling. Common failure types:
Indicates authentication issues, such as missing or invalid tokens. The user should be redirected to login. if (failure is AuthFailure ) {
Navigator . pushReplacementNamed (context, '/login' );
}
Indicates network or server errors. Display an appropriate error message to the user. if (failure is ServerFailure ) {
showErrorSnackbar ( 'Server error. Please try again later.' );
}
Indicates the requested resource was not found. This may occur when trying to update or delete a non-existent apiary. if (failure is NotFoundFailure ) {
showErrorSnackbar ( 'Apiary not found.' );
}