Ejemplo n.º 1
0
        public IHttpActionResult CreateSitePersonnelLookup(SitePersonnelLookupModel sitePersonnelLookupModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            try
            {
                var sitePersonnelLookupDto = Mapper.Map <SitePersonnelLookupDto>(sitePersonnelLookupModel);

                _sitePersonnelLookupAppService.Create(sitePersonnelLookupDto, AuthHelper.GetCurrentUserId());

                return(Ok("Access record was successfully created."));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
Ejemplo n.º 2
0
        public IHttpActionResult Register(AdminAreaUserViewModel viewModel)
        {
            try
            {
                //need to chop up the different parts of the view model to map to the correct
                // bits of the relevant DTOs
                long currentUserId = AuthHelper.GetCurrentUserId();
                long userId        = 0;
                long?employeeId    = null;

                // USER DTO
                if (viewModel.UserModel != null)
                {
                    var userDto = Mapper.Map <UserDto>(viewModel.UserModel);

                    // check if the login and email have already been used
                    if (_accountAppService.CheckIfLoginExists(userDto.Login))
                    {
                        return(BadRequest("Login already taken"));
                    }

                    if (_accountAppService.CheckIfEmailExists(userDto.Email))
                    {
                        return(BadRequest("Email already taken"));
                    }

                    if (userDto.ExternalTimeSystemId != null)
                    {
                        if (_accountAppService.CheckIfExternalTimeSystemIdExists((int)userDto.ExternalTimeSystemId))
                        {
                            return(BadRequest("ZK Time System ID already taken"));
                        }
                    }

                    userId = _accountAppService.RegisterUserAccount(userDto, currentUserId);
                }


                // EMPLOYEE DTO
                if (viewModel.EmployeeModel != null)
                {
                    var employeeDto = Mapper.Map <EmployeeDto>(viewModel.EmployeeModel);
                    employeeDto.UserId = userId;
                    //TEMP
                    employeeDto.StartDate = new DateTime(1900, 1, 1, 0, 0, 0);
                    employeeDto.LeaveDate = new DateTime(1900, 1, 1, 0, 0, 0);

                    // save and return new id
                    employeeId = _employeeAppService.Create(employeeDto, currentUserId);
                }


                // CONTRACT DTOs
                if (viewModel.ContractModel != null)
                {
                    var contractDto = Mapper.Map <ContractDto>(viewModel.ContractModel);
                    contractDto.EmployeeId = employeeId;
                    //TEMP
                    contractDto.StartDate = new DateTime(1900, 1, 1, 0, 0, 0);
                    contractDto.EndDate   = new DateTime(1900, 1, 1, 0, 0, 0);

                    _contractAppService.Create(contractDto, currentUserId);
                }


                // SITE ACCESS
                if (viewModel.SiteAccessModels.Any())
                {
                    var sitePersonnelLookupDtos = new List <SitePersonnelLookupDto>();
                    foreach (var siteAccessModel in viewModel.SiteAccessModels)
                    {
                        siteAccessModel.EmployeeId = employeeId;

                        var sitePersonnelLookupDto = Mapper.Map <SitePersonnelLookupDto>(siteAccessModel);
                        sitePersonnelLookupDtos.Add(sitePersonnelLookupDto);
                    }

                    // send off to be saved
                    foreach (var sitePersonnelLookupDto in sitePersonnelLookupDtos)
                    {
                        _sitePersonnelLookupAppService.Create(sitePersonnelLookupDto, currentUserId);
                    }
                }

                return(Ok("User Registered"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }