private tb_configuration_limitation ToDbModel(ConfigurationLimitationModel configurationLimitation)
        {
            tb_configuration_limitation tbConfigurationLimitation = new tb_configuration_limitation();

            tbConfigurationLimitation.vl_latitude = configurationLimitation.Latitude;
            tbConfigurationLimitation.vl_longitude = configurationLimitation.Longitude;
            tbConfigurationLimitation.ds_host = configurationLimitation.Host;

            if(configurationLimitation.LimitationType != null)
                tbConfigurationLimitation.tb_limitation_type_id_limitation_type = configurationLimitation.LimitationType.Id;

            return tbConfigurationLimitation;
        }
        public ConfigurationLimitationModel Add(ConfigurationLimitationModel configurationLimitation)
        {
            using (var appontoContext = new AppontoContext())
            {
                tb_configuration_limitation tbConfiguracaoLimitacao= ToDbModel(configurationLimitation);
                appontoContext.tb_configuration_limitation.Add(tbConfiguracaoLimitacao);
                appontoContext.SaveChanges();

                ConfigurationLimitationModel ret = ToModel(tbConfiguracaoLimitacao);

                return ret;
            }
        }
        public ConfigurationLimitationModel ToModel(tb_configuration_limitation tbConfigurationLimitation)
        {
            ConfigurationLimitationModel configurationLimitation = new ConfigurationLimitationModel();

            configurationLimitation.Id = tbConfigurationLimitation.id_configuration_limitation;
            if (tbConfigurationLimitation.vl_latitude != null)
                configurationLimitation.Latitude = (double)tbConfigurationLimitation.vl_latitude;
            if (tbConfigurationLimitation.vl_longitude != null)
                configurationLimitation.Longitude = (double)tbConfigurationLimitation.vl_longitude;
            configurationLimitation.Host = tbConfigurationLimitation.ds_host;

            if (tbConfigurationLimitation.tb_limitation_type != null)
                configurationLimitation.LimitationType = new LimitationTypeService().ToModel(tbConfigurationLimitation.tb_limitation_type);

            return configurationLimitation;
        }
        public UserModel Update(int userId, ConfigurationLimitationModel configurationLimitation)
        {
            using (var appontoContext = new AppontoContext())
            {
                tb_user tbUser = appontoContext.tb_user.SingleOrDefault(x => x.id_user == userId);

                tbUser.tb_configuration.tb_configuration_limitation.tb_limitation_type_id_limitation_type = configurationLimitation.LimitationType.Id;
                if (configurationLimitation.LimitationType.Id == (int)LimitationTypeEnum.Gps)
                {
                    tbUser.tb_configuration.tb_configuration_limitation.vl_latitude = configurationLimitation.Latitude;
                    tbUser.tb_configuration.tb_configuration_limitation.vl_longitude = configurationLimitation.Longitude;
                }
                else if (configurationLimitation.LimitationType.Id == (int)LimitationTypeEnum.Wifi)
                {
                    tbUser.tb_configuration.tb_configuration_limitation.ds_host = configurationLimitation.Host;
                }

                appontoContext.SaveChanges();

                UserModel ret = new UserService().ToModel(tbUser);

                return ret;
            }
        }
Exemplo n.º 5
0
        public UserModel New(UserModel user)
        {
            using (var appontoContext = new AppontoContext())
            {
                #region Configurações iniciais do usuário
                ConfigurationModel configuration = new ConfigurationModel();
                ConfigurationLimitationModel configurationLimitation = new ConfigurationLimitationModel();

                ConfigurationService configurationService = new ConfigurationService();
                ConfigurationLimitationService configurationLimitationService = new ConfigurationLimitationService();

                configurationLimitation.LimitationType = new LimitationTypeModel() { Id = (int)LimitationTypeEnum.Nenhuma };
                configurationLimitation = configurationLimitationService.Add(configurationLimitation);

                configuration.ConfigurationLimitation = configurationLimitation;

                user.Configuration = configurationService.Add(configuration);
                user.Gmt = -3;
                #endregion

                tb_user tbUser = ToDbModel(user);

                #region Empresa
                bool newCompany = false;

                if (user.Company != null)
                {
                    CompanyService companyService = new CompanyService();

                    if (string.IsNullOrEmpty(user.Company.Token))
                    {
                        newCompany = true;
                        user.Company.Token = Guid.NewGuid().ToString().Substring(0, 10);
                        tbUser.tb_company = companyService.ToDbModel(user.Company);
                    }
                    else
                    {
                        tbUser.tb_company_id_company = companyService.Get(user.Company.Token).Id;
                    }
                }
                #endregion

                #region Permissões iniciais do usuário
                AccessLevelService accessLevelService = new AccessLevelService();
                List<AccessLevelModel> accessLevels = new List<AccessLevelModel>();

                if (user.Company != null && newCompany)
                    accessLevels.Add(new AccessLevelModel() { Id = (int)AccessLevelEnum.Administrator });
                else
                    accessLevels.Add(new AccessLevelModel() { Id = (int)AccessLevelEnum.User });

                user.AccessLevel = accessLevels;
                foreach (AccessLevelModel accessLevel in accessLevels)
                {
                    tb_access_level tbAccessLevel = accessLevelService.ToDbModel(accessLevel);
                    appontoContext.tb_access_level.Attach(tbAccessLevel);
                    tbUser.tb_access_level.Add(tbAccessLevel);
                }
                #endregion

                appontoContext.tb_user.Add(tbUser);
                appontoContext.SaveChanges();

                UserModel ret = ToModel(tbUser);

                return ret;
            }
        }