private tb_configuration ToDbModel(ConfigurationModel configuration)
        {
            tb_configuration tbConfiguration = new tb_configuration();

            if (configuration.ConfigurationLimitation != null)
                tbConfiguration.tb_configuration_limitation_id_configuration_limitation = configuration.ConfigurationLimitation.Id;

            return tbConfiguration;
        }
        public ConfigurationModel ToModel(tb_configuration tbConfiguration)
        {
            ConfigurationModel configuration = new ConfigurationModel();

            configuration.Id = tbConfiguration.id_configuration;

            if(tbConfiguration.tb_configuration_limitation != null)
                configuration.ConfigurationLimitation = new ConfigurationLimitationService().ToModel(tbConfiguration.tb_configuration_limitation);

            return configuration;
        }
        public ConfigurationModel Add(ConfigurationModel configuration)
        {
            using (var appontoContext = new AppontoContext())
            {
                tb_configuration tbConfiguration = ToDbModel(configuration);
                appontoContext.tb_configuration.Add(tbConfiguration);
                appontoContext.SaveChanges();

                ConfigurationModel ret = ToModel(tbConfiguration);

                return ret;
            }
        }
Exemplo n.º 4
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;
            }
        }