Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions back/microsservicos/aluguel/bin/aluguel_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,27 @@ import 'package:aluguel_dart/application/get_aluguel_usecase.dart';
import 'package:aluguel_dart/application/create_aluguel_usecase.dart';
import 'package:aluguel_dart/application/update_aluguel_usecase.dart';
import 'package:aluguel_dart/application/delete_aluguel_usecase.dart';
import 'package:aluguel_dart/application/verify_door_code_usecase.dart';
import 'package:aluguel_dart/domain/repositories/aluguel_repository.dart';
import 'package:aluguel_dart/presentation/http/controllers/get_aluguel_controller.dart';
import 'package:aluguel_dart/presentation/http/controllers/create_aluguel_controller.dart';
import 'package:aluguel_dart/presentation/http/controllers/update_aluguel_controller.dart';
import 'package:aluguel_dart/presentation/http/controllers/delete_aluguel_controller.dart';
import 'package:aluguel_dart/presentation/http/controllers/get_door_hash_controller.dart';

Future<void> _shutdownServer(HttpServer server, String message) async {
stdout.writeln(message);
try {
await closeRabbitMQConnection();
} catch (e) {
stderr.writeln('Erro ao fechar RabbitMQ: $e');
} finally {
try {
await server.close(force: true);
} catch (_) {}
exit(0);
}
}

Future<void> main() async {
final AluguelRepository repo = Environments.getAluguelRepo();
Expand All @@ -29,12 +45,14 @@ Future<void> main() async {
final updateAluguelUsecase = UpdateAluguelUsecase(repository: repo);
final deleteAluguelUsecase = DeleteAluguelUsecase(repository: repo);
final getAllAluguelUsecase = GetAllAluguelUsecase(repository: repo);
final verifyDoorCodeUsecase = VerifyDoorCodeUsecase(repository: repo);

final getController = GetAluguelController(getAluguelUsecase);
final createController = CreateAluguelController(createAluguelUsecase);
final updateController = UpdateAluguelController(updateAluguelUsecase);
final deleteController = DeleteAluguelController(deleteAluguelUsecase);
final getAllAluguelController = GetAllAluguelController(getAllAluguelUsecase);
final getDoorHashController = GetDoorHashController(verifyDoorCodeUsecase);

final router = Router()
..mount(
Expand All @@ -44,7 +62,8 @@ Future<void> main() async {
createController: createController,
updateController: updateController,
deleteController: deleteController,
getAllAluguelController: getAllAluguelController
getAllAluguelController: getAllAluguelController,
getDoorHashController: getDoorHashController,
).call,
);

Expand All @@ -57,30 +76,12 @@ Future<void> main() async {
final server = await io.serve(handler, InternetAddress.anyIPv4, port);
print('Aluguel. Porta: $port');
await startQueue();
ProcessSignal.sigint.watch().listen((_) async {
stdout.writeln('Service aluguel interrupted!');
try {
await closeRabbitMQConnection();
} catch (e) {
stderr.writeln('Erro ao fechar RabbitMQ: $e');
} finally {
try {
await server.close(force: true);
} catch (_) {}
exit(0);
}
});
ProcessSignal.sigterm.watch().listen((_) async {
stdout.writeln('Service aluguel terminated!');
try {
await closeRabbitMQConnection();
} catch (e) {
stderr.writeln('Erro ao fechar RabbitMQ: $e');
} finally {
try {
await server.close(force: true);
} catch (_) {}
exit(0);
}
});
ProcessSignal.sigint.watch().listen(
(_) async => _shutdownServer(server, 'Service aluguel interrupted!'),
);
if (!Platform.isWindows) {
ProcessSignal.sigterm.watch().listen(
(_) async => _shutdownServer(server, 'Service aluguel terminated!'),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@ class CreateAluguelUsecase {
Future<Aluguel> call({
required String userId,
required String workspaceId,
required int startDate,
required int endDate,
required int startDate,
required int endDate,
required int people,
required num finalPrice,
required num finalPrice
}) async {

if (endDate < startDate) {
throw StateError('endDate não pode ser menor que startDate.');
}
if (people <= 0) {
throw StateError('O número de pessoas para a reserva deve ser diferente de null e maior que.');
throw StateError(
'O número de pessoas para a reserva deve ser diferente de null e maior que zero.',
);
}
if (finalPrice < 0) {
throw StateError('finalPrice não pode ser negativo.');
}

Aluguel createdAluguel = await repository.createAluguel(
final createdAluguel = await repository.createAluguel(
userId: userId,
workspaceId: workspaceId,
startDate: startDate,
Expand All @@ -37,18 +38,18 @@ class CreateAluguelUsecase {
status: 'PENDING',
);

RabbitMQEvent aluguelCreated = RabbitMQEvent(eventType: 'AluguelCreated', payload: createdAluguel.toJson());
final aluguelCreated = RabbitMQEvent(
eventType: 'AluguelCreated',
payload: createdAluguel.toJson(),
);

final published = await publishEvent('aluguel.created', aluguelCreated.toJson());
final published =
await publishEvent('aluguel.created', aluguelCreated.toJson());

if(published){
if (published) {
return createdAluguel;
} else {
throw StateError('Não foi possível criar o aluguel');
}

}
}



Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class GetAluguelUsecase {
final AluguelRepository repository;

GetAluguelUsecase({required this.repository});

Future<Aluguel> call(String id) async {
if (id.isEmpty) {
throw ArgumentError('ID do aluguel não pode ser vazio.');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:aluguel_dart/domain/entities/aluguel.dart';
import 'package:aluguel_dart/domain/repositories/aluguel_repository.dart';
import 'package:aluguel_dart/infrastructure/clients/rabbitmq/rabbitmq.dart';

class UpdateAluguelUsecase {
final AluguelRepository repository;
Expand All @@ -8,13 +9,13 @@ class UpdateAluguelUsecase {

Future<Aluguel> call(
String id, {
int? startDate,
int? startDate,
int? endDate,
int? people,
double? finalPrice,
String? status,
String? doorCode,
}) async {

if (startDate != null && endDate != null && endDate < startDate) {
throw StateError('endDate não pode ser menor que startDate.');
}
Expand All @@ -25,13 +26,45 @@ class UpdateAluguelUsecase {
throw StateError('finalPrice não pode ser negativo.');
}

String? desiredDoorCode = doorCode;

if (status != null &&
status.toUpperCase() == 'CONFIRMED' &&
doorCode == null) {
final current = await repository.getAluguel(id);
if (current == null) {
throw StateError('aluguel_not_found');
}

final fetchedDoorCode = await fetchDoorCodeFromCatalog(
current.workspaceId,
);
if (fetchedDoorCode == null || fetchedDoorCode.isEmpty) {
throw StateError('door_code_not_found');
}
desiredDoorCode = fetchedDoorCode;
}

String? sanitizedDoorCode;
if (desiredDoorCode != null) {
final trimmedDoorCode = desiredDoorCode.trim();
if (trimmedDoorCode.isEmpty) {
throw StateError('doorCode nǜo pode ser vazio.');
}
if (!RegExp(r'^\d{5}$').hasMatch(trimmedDoorCode)) {
throw StateError('doorCode deve conter exatamente 5 d��gitos.');
}
sanitizedDoorCode = trimmedDoorCode;
}

return repository.updateAluguel(
id,
startDate: startDate,
endDate: endDate,
people: people,
finalPrice: finalPrice,
status: status,
doorCode: sanitizedDoorCode,
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import 'package:aluguel_dart/domain/repositories/aluguel_repository.dart';
import 'package:aluguel_dart/infrastructure/clients/rabbitmq/rabbitmq.dart';

class VerifyDoorCodeUsecase {
final AluguelRepository repository;

VerifyDoorCodeUsecase({required this.repository});

Future<bool> call({
required String aluguelId,
required String plainDoorCode,
}) async {
if (!RegExp(r'^\d{5}$').hasMatch(plainDoorCode)) {
throw StateError('doorCode deve conter exatamente 5 digitos.');
}

final aluguel = await repository.getAluguel(aluguelId);

if (aluguel == null) {
throw StateError('aluguel_not_found');
}

if (aluguel.status.toUpperCase() != 'CONFIRMED') {
return false;
}

final nowEpoch = DateTime.now().millisecondsSinceEpoch ~/ 1000;
if (nowEpoch < aluguel.startDate || nowEpoch > aluguel.endDate) {
return false;
}

return await verifyDoorCodeWithCatalog(
workspaceId: aluguel.workspaceId,
doorCode: plainDoorCode,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Aluguel {
final int people;
final double finalPrice;
final String status;
final String? doorCode;
final int createdAt;
final int updatedAt;

Expand All @@ -19,6 +20,7 @@ class Aluguel {
required this.people,
required this.finalPrice,
required this.status,
this.doorCode,
required this.createdAt,
required this.updatedAt,
});
Expand All @@ -33,6 +35,7 @@ class Aluguel {
'people': people,
'finalPrice': finalPrice,
'status': status,
'doorCode': doorCode,
'createdAt': createdAt,
'updatedAt': updatedAt,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ abstract class AluguelRepository {
int? people,
double? finalPrice,
String? status,
String? doorCode,
});


Future<void> deleteAluguel(String id);
}
}
Loading