public void Execute(EditEntranceForm form)
        {
            if (!form.BuildingId.HasValue)
            {
                throw new FormException("Building not chosen.");
            }

            if (!form.Id.HasValue)
            {
                throw new FormException("Entrance not chosen.");
            }

            var building = _buildingService.GetById(form.BuildingId.Value);

            var entrance = _entranceService.GetById(form.Id.Value);

            try
            {
                _entranceService.Rename(entrance, form.Name);
            }
            catch (EntityAlreadyExistsException e)
            {
                throw new FormException(e.Message);
            }

            _entranceService.Update(entrance, building);
        }
        public void Execute(RegisterComingForm form)
        {
            if (!form.EmployeeId.HasValue)
            {
                throw new FormException("Employee not chosen.");
            }

            if (!form.EntranceId.HasValue)
            {
                throw new FormException("Entrance not chosen.");
            }

            var employee = _employeeService.GetById(form.EmployeeId.Value);

            if (employee is null)
            {
                throw new FormException("Employee not found.");
            }

            var entrance = _entranceService.GetById(form.EntranceId.Value);

            if (entrance is null)
            {
                throw new FormException("Entrance not found.");
            }

            _registrationService.RegisterEmployee(employee, RegistrationEventType.Coming, entrance);
        }
        public IActionResult View(int id)
        {
            if (!RoleIs(Role.Administrator, Role.Manager, Role.SecurityGuard))
            {
                return(Forbid());
            }


            var entrance = _entranceService.GetById(id);

            if (entrance is null)
            {
                return(NotFound());
            }

            var entranceViewModel = _mapper.Map <EntranceViewModel>(entrance);

            return(View(entranceViewModel));
        }
Exemplo n.º 4
0
        public void Execute(DeleteEntranceForm form)
        {
            if (!form.Id.HasValue)
            {
                throw new FormException("Entrance not chosen.");
            }

            var entrance = _entranceService.GetById(form.Id.Value);

            _entranceService.Delete(entrance);
        }
        public void Execute(EditUserForm form)
        {
            if (form.Role is null)
            {
                throw new FormException("Роль не выбрана.");
            }

            if (form.Role.HasValue && form.Role.Value == Role.SecurityGuard && !form.EntranceId.HasValue)
            {
                throw new FormException("Вход в здание не выбран.");
            }

            if (form.Role.HasValue && form.Role.Value == Role.Manager && !form.DepartmentId.HasValue)
            {
                throw new FormException("Отдел не выбран.");
            }

            if (form.Role.HasValue && form.Role.Value == Role.Administrator)
            {
                if (form.NeedNotify && string.IsNullOrWhiteSpace(form.Email))
                {
                    throw new FormException("Для получения уведомлений должен быть указан адрес электронной почты.");
                }
            }

            var user = _userService.GetById(form.Id);

            try
            {
                _userService.ChangeLogin(user, form.Login);
            }
            catch (UserAlreadyExistsException e)
            {
                throw new FormException(e.Message);
            }

            if (form.Role.HasValue)
            {
                user.ChangeRole(form.Role.Value);

                switch (form.Role)
                {
                case Role.Administrator:
                {
                    user.ChangeNotification(form.Email, form.NeedNotify);
                    break;
                }

                case Role.SecurityGuard when user is SecurityGuard securityGuard && form.EntranceId.HasValue:
                {
                    var entrance = _entranceService.GetById(form.EntranceId.Value);

                    securityGuard.ChangeEntrance(entrance);
                    break;
                }

                case Role.Manager when user is Manager manager && form.DepartmentId.HasValue:
                {
                    var department = _departmentService.GetById(form.DepartmentId.Value);

                    manager.ChangeDepartment(department);

                    manager.ChangeNotification(form.Email, false);
                    break;
                }

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }
        }
        public User Create(
            string login,
            string password,
            Role role,
            string email     = null,
            bool needNotify  = false,
            int?entranceId   = null,
            int?departmentId = null)
        {
            if (string.IsNullOrWhiteSpace(login))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(login));
            }
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentException("Value cannot be null or whitespace.", nameof(password));
            }

            if (_userRepository.AllActive().Any(x => x.Login.Equals(login, StringComparison.InvariantCultureIgnoreCase)))
            {
                throw new UserAlreadyExistsException("Пользователь с таким логином уже существует");
            }

            User user;

            switch (role)
            {
            case Role.Administrator:
                user = new Administrator(login, password, email, needNotify);
                break;

            case Role.Manager:
            {
                if (!departmentId.HasValue)
                {
                    throw new InvalidOperationException("Department not chosen.");
                }

                var department = _departmentService.GetById(departmentId.Value);

                user = new Manager(login, password, department, email);
                break;
            }

            case Role.SecurityGuard:
            {
                if (!entranceId.HasValue)
                {
                    throw new InvalidOperationException("Entrance not chosen.");
                }

                var entrance = _entranceService.GetById(entranceId.Value);

                user = new SecurityGuard(login, password, entrance);

                entrance.ChangeSecurityGuard((SecurityGuard)user);

                break;
            }

            default: throw new ArgumentOutOfRangeException(nameof(role));
            }

            _userRepository.Add(user);

            return(GetByLogin(login));
        }