public async Task <Unit> Handle(DeleteVacationTypeCommand request, CancellationToken cancellationToken) { var vacationtype = await VacationTypeRepository.Get(request.Id); if (vacationtype == null) { throw new ArgumentOutOfRangeException("Vacation Type does not exist."); } var employees = await EmployeeRepository.Select(); if (employees.Any(e => e.GetVacations(DateTimeOffset.Now.Year).Any(v => v.VacationType.Equals(vacationtype)))) { throw new ArgumentOutOfRangeException("Some employees has active vacations of this type."); } var vacationtypes = await VacationTypeRepository.Select(); foreach (var type in vacationtypes.Where(e => e.Pool != null && e.Pool.Equals(vacationtype))) { type.UpdatePool(vacationtype.Pool, type.RowVersion); await VacationTypeRepository.Update(type); } await VacationTypeRepository.Delete(vacationtype); await Mediator.Publish(new VacationTypeDeletedEvent { Id = vacationtype.Id }); return(await Unit.Task); }
// GET: VacationTypesController/Delete/5 public async Task <ActionResult> Delete(int id) { var vacationtype = await _repo.FindById(id); if (vacationtype == null) { return(NotFound()); } var isSuccess = await _repo.Delete(vacationtype); if (!isSuccess) { ModelState.AddModelError("", "Something Went Wrong..."); return(BadRequest()); } return(RedirectToAction(nameof(Index))); }