public async Task <Result> Handle(CreateBusinessCommand command, CancellationToken cancellationToken) => await _repository.FetchUserAggregate(command.OwnerEmail) .FailureIf("User not found.") .AndThen(user => user.UpdateRole(Role.Owner)) .AndThen(_ => _mapper.Map <Business>(command)) .Validate(_validator) .Finally(business => _repository.AddAndSave(business));
public async Task <Result> Handle(GenerateTimeSlotsCommand command, CancellationToken cancellationToken) => await _repository.FetchUserAggregate(command.OwnerEmail) .FailureIf("User not found.") .Ensure(user => user.GetTimeSlotByDate(command.BusinessId, command.Start) == null, "Time slots already generated for this date.", HttpCode.Conflict) .AndThen(user => user.GetBusiness(command.BusinessId)?.GenerateTimeSlots(command.Start)) .Finally(timeSlots => _repository.AddAndSave(timeSlots.ToArray()));
public async Task <Result <TokenResponse> > Handle(RegisterCommand command, CancellationToken cancellationToken) => await _repository.FetchUserAggregate(command.Email) .ToResult() .Ensure(user => user == null, $"The email '{command.Email}' is already in use.") .AndThen(_ => _mapper.Map <User>(command)) .Validate(_validator) .AndThenF(async newUser => await _repository.AddAndSave(newUser)) .Finally(_mapper.Map <TokenResponse>);
public async Task <Result> Handle(CreateEmployeeCommand command, CancellationToken cancellationToken) => await _repository.FetchUserAggregate(command.OwnerEmail) .FailureIf("Owner not found.") .Ensure(user => user.Role != Role.Owner, "Owner cannot be employee.", HttpCode.Conflict) .FailureIf(async _ => await _repository.FetchUserAggregate(command.UserId), "User not found.") .AndThen(user => user.UpdateRole(Role.Employee)) .AndThen(_ => _mapper.Map <Employee>(command)) .Validate(_validator) .Finally(employee => _repository.AddAndSave(employee));
public async Task <Result> Handle(CreateBookingCommand command, CancellationToken cancellationToken) => await _repository.FetchUserAggregate(command.UserId) .FailureIf("User not found.") .Ensure(user => !user.BookingExists(command.TimeSlotId), "You already have a booking for this time slot.") .FailureIf(async _ => await _repository.FetchTimeSlot(command.TimeSlotId), "Time Slot does not exist.") .Ensure(timeSlot => timeSlot.Bookings.Count() < timeSlot.Capacity, "This time slot is full.") .AndThen(_ => _mapper.Map <Booking>(command)) .Validate(_validator) .Finally(booking => _repository.AddAndSave(booking));
public async Task <Result> Handle(SendMessageCommand command, CancellationToken cancellationToken) => await _repository.FetchUserAggregate(command.SenderId) .ToResult() .AndThen(async user => { var business = await _repository.FetchBusiness(command.SenderId); return(new { business, user }); }) .Ensure(entry => entry.user != null || entry.business != null, "Invalid sender.") .AndThen(async _ => { var user = await _repository.FetchUserAggregate(command.ReceiverId); var business = await _repository.FetchBusiness(command.ReceiverId); return(new { business, user }); }) .Ensure(entry => entry.user != null || entry.business != null, "Invalid receiver.") .AndThen(_ => _mapper.Map <Message>(command)) .Validate(_validator) .Finally(message => _repository.AddAndSave(message));