mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-10 15:17:31 +00:00
Created create community bloc, services, and param.
This commit is contained in:
@ -0,0 +1,39 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/create_community/domain/param/create_community_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/create_community/domain/services/create_community_service.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
import 'package:syncrow_web/services/api/http_service.dart';
|
||||
|
||||
class RemoteCreateCommunityService implements CreateCommunityService {
|
||||
const RemoteCreateCommunityService(this._httpService);
|
||||
|
||||
final HTTPService _httpService;
|
||||
|
||||
static const _defaultErrorMessage = 'Failed to create community';
|
||||
|
||||
@override
|
||||
Future<CommunityModel> createCommunity(CreateCommunityParam param) async {
|
||||
try {
|
||||
final response = await _httpService.post(
|
||||
path: 'endpoint',
|
||||
expectedResponseModel: (data) => CommunityModel.fromJson(
|
||||
data as Map<String, dynamic>,
|
||||
),
|
||||
);
|
||||
return response;
|
||||
} on DioException catch (e) {
|
||||
final message = e.response?.data as Map<String, dynamic>?;
|
||||
final error = message?['error'] as Map<String, dynamic>?;
|
||||
final errorMessage = error?['error'] as String? ?? '';
|
||||
final formattedErrorMessage = [
|
||||
_defaultErrorMessage,
|
||||
errorMessage,
|
||||
].join(': ');
|
||||
throw APIException(formattedErrorMessage);
|
||||
} catch (e) {
|
||||
final formattedErrorMessage = [_defaultErrorMessage, '$e'].join(': ');
|
||||
throw APIException(formattedErrorMessage);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
import 'package:equatable/equatable.dart';
|
||||
|
||||
class CreateCommunityParam extends Equatable {
|
||||
const CreateCommunityParam({required this.name});
|
||||
|
||||
final String name;
|
||||
|
||||
@override
|
||||
List<Object> get props => [name];
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/create_community/domain/param/create_community_param.dart';
|
||||
|
||||
abstract class CreateCommunityService {
|
||||
Future<CommunityModel> createCommunity(CreateCommunityParam param);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/communities/domain/models/community_model.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/create_community/domain/param/create_community_param.dart';
|
||||
import 'package:syncrow_web/pages/space_management_v2/modules/create_community/domain/services/create_community_service.dart';
|
||||
import 'package:syncrow_web/services/api/api_exception.dart';
|
||||
|
||||
part 'create_community_event.dart';
|
||||
part 'create_community_state.dart';
|
||||
|
||||
class CreateCommunityBloc extends Bloc<CreateCommunityEvent, CreateCommunityState> {
|
||||
final CreateCommunityService _createCommunityService;
|
||||
|
||||
CreateCommunityBloc(
|
||||
this._createCommunityService,
|
||||
) : super(CreateCommunityInitial()) {
|
||||
on<CreateCommunity>(_onCreateCommunity);
|
||||
}
|
||||
|
||||
Future<void> _onCreateCommunity(
|
||||
CreateCommunity event,
|
||||
Emitter<CreateCommunityState> emit,
|
||||
) async {
|
||||
emit(CreateCommunityLoading());
|
||||
try {
|
||||
final createdCommunity = await _createCommunityService.createCommunity(
|
||||
event.param,
|
||||
);
|
||||
emit(CreateCommunitySuccess(createdCommunity));
|
||||
} on APIException catch (e) {
|
||||
emit(CreateCommunityFailure(e.message));
|
||||
} catch (e) {
|
||||
emit(CreateCommunityFailure(e.toString()));
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
part of 'create_community_bloc.dart';
|
||||
|
||||
sealed class CreateCommunityEvent extends Equatable {
|
||||
const CreateCommunityEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class CreateCommunity extends CreateCommunityEvent {
|
||||
const CreateCommunity(this.param);
|
||||
|
||||
final CreateCommunityParam param;
|
||||
|
||||
@override
|
||||
List<Object> get props => [param];
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
part of 'create_community_bloc.dart';
|
||||
|
||||
sealed class CreateCommunityState extends Equatable {
|
||||
const CreateCommunityState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
final class CreateCommunityInitial extends CreateCommunityState {}
|
||||
|
||||
final class CreateCommunityLoading extends CreateCommunityState {}
|
||||
|
||||
final class CreateCommunitySuccess extends CreateCommunityState {
|
||||
const CreateCommunitySuccess(this.community);
|
||||
|
||||
final CommunityModel community;
|
||||
|
||||
@override
|
||||
List<Object> get props => [community];
|
||||
}
|
||||
|
||||
final class CreateCommunityFailure extends CreateCommunityState {
|
||||
final String message;
|
||||
|
||||
const CreateCommunityFailure(this.message);
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
Reference in New Issue
Block a user