Exemplo n.º 1
0
        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);
        }
        public async Task <int> Handle(InsertEmployeeCommand request, CancellationToken cancellationToken)
        {
            var vacationtypes = await VacationTypeRepository.Select();

            var vacationdays = new Dictionary <DonVo.CQRS.Standard21.Domain.Model.Company.VacationType, int>();

            foreach (var item in request.VacationDays)
            {
                vacationdays.Add(vacationtypes.Single(t => t.Id == item.Key), item.Value);
            }

            var position = await PositionRepository.Get(request.PositionId);

            if (position == null)
            {
                throw new ArgumentOutOfRangeException("Position does not exist.");
            }

            var department = await DepartmentRepository.Get(request.DepartmentId);

            if (department == null)
            {
                throw new ArgumentOutOfRangeException("Department does not exist.");
            }

            var manager = await EmployeeRepository.Get(request.ManagerId.GetValueOrDefault(0));

            var employee = new DonVo.CQRS.Standard21.Domain.Model.Company.Employee
                           (
                vacationdays,
                request.FirstName,
                request.LastName,
                request.BirthDate,
                request.Gender,
                request.Email,
                request.Phone,
                request.Street,
                request.PostalCode,
                request.City,
                position,
                department,
                manager
                           );

            var id = await EmployeeRepository.Insert(employee);

            var user = await UserManager.FindByIdAsync(employee.ApplicationUserId.ToString());

            await UserManager.AddToRolesAsync(user, request.Roles);

            await Mediator.Publish(new EmployeeInsertedEvent { Id = employee.Id });

            return(id);
        }
        public async Task <IEnumerable <VacationTypeViewModel> > Handle(GetVacationTypesQuery request, CancellationToken cancellationToken)
        {
            var vacationtypes = await VacationTypeRepository.Select();

            var viewmodel = new List <VacationTypeViewModel>();

            foreach (var item in vacationtypes)
            {
                var vm = new VacationTypeViewModel();
                vm.LoadFromDomain(item);
                vm.IsSelected = item.Id == request.SelectedId;
                viewmodel.Add(vm);
            }
            foreach (var item in viewmodel)
            {
                item.VacationTypes = viewmodel.Where(i => i.Id != item.Id && !i.PoolId.HasValue).ToList();
            }
            return(viewmodel.OrderBy(item => item.Id).ToList());
        }