예제 #1
0
        public ACDomainDTO GetById(int id)
        {
            var companyAcServer = CompanyAcServerModel.GetOneById(id).Value;
            var result          = new ACDomainDTO()
            {
                companyId = companyAcServer.Company.Id,
                password  = companyAcServer.Password,
                domainId  = companyAcServer.Id,
                isDefault = companyAcServer.IsDefault,
                path      = companyAcServer.AcServer,
                user      = companyAcServer.Username
            };

            return(result);
        }
예제 #2
0
        public ACDomainDTO Save(ACDomainDTO acDomain)
        {
            //if (acDomain.isDefault)
            //{
            //    var defaultDomain =
            //        CompanyAcServerModel.GetAllByCompany(acDomain.companyId).FirstOrDefault(x => x.IsDefault);
            //    if (defaultDomain != null)
            //    {
            //        defaultDomain.IsDefault = false;
            //        CompanyAcServerModel.RegisterSave(defaultDomain, true);
            //    }
            //}
            //else
            //{
            //    // for update
            //    if (acDomain.domainId != 0)
            //    {
            //        // you can't uncheck default if there is only one record
            //        if (CompanyAcServerModel.GetAllByCompany(acDomain.companyId).Count() == 1)
            //            return acDomain;

            //        if (CompanyAcServerModel.GetAllByCompany(acDomain.companyId).Count() > 1)
            //        {
            //            var firstAcServer =
            //                CompanyAcServerModel.GetAllByCompany(acDomain.companyId)
            //                    .FirstOrDefault(x => x.Id != acDomain.domainId);
            //            if (firstAcServer != null)
            //            {
            //                firstAcServer.IsDefault = true;
            //                CompanyAcServerModel.RegisterSave(firstAcServer, true);
            //            }
            //        }
            //    }
            //    else
            //    {
            //        var existing = CompanyAcServerModel.GetAllByCompany(acDomain.companyId).FirstOrDefault();
            //        if (existing != null)
            //        {
            //            existing.IsDefault = true;
            //            CompanyAcServerModel.RegisterSave(existing, true);
            //        }
            //    }
            //}
            var             company = CompanyModel.GetOneById(acDomain.companyId).Value;
            CompanyAcServer companyAcServer;

            if (acDomain.domainId != 0)
            {
                companyAcServer = CompanyAcServerModel.GetOneById(acDomain.domainId).Value;
            }
            else
            {
                companyAcServer = new CompanyAcServer()
                {
                };
            }
            companyAcServer.Company   = company;
            companyAcServer.IsDefault = acDomain.isDefault;
            companyAcServer.Username  = acDomain.user;
            companyAcServer.AcServer  = acDomain.path;

            if (!string.IsNullOrEmpty(acDomain.password))
            {
                companyAcServer.Password = acDomain.password;
            }
            CompanyAcServerModel.RegisterSave(companyAcServer, true);

            //uncheck if another isDefault=true exists
            if (acDomain.isDefault)
            {
                var otherDefaultServers =
                    CompanyAcServerModel.GetAllByCompany(acDomain.companyId)
                    .Where(x => x.Id != acDomain.domainId && x.IsDefault).ToList();
                foreach (var acServer in otherDefaultServers)
                {
                    acServer.IsDefault = false;
                    CompanyAcServerModel.RegisterSave(acServer, true);
                }
            }

            //select first isDefault if no default at all
            if (!acDomain.isDefault)
            {
                var firstAcServer =
                    CompanyAcServerModel.GetAllByCompany(acDomain.companyId)
                    .FirstOrDefault(x => x.IsDefault);
                if (firstAcServer == null)
                {
                    companyAcServer.IsDefault = true;
                    CompanyAcServerModel.RegisterSave(companyAcServer, true);
                }
            }
            return(acDomain);
        }