Exemple #1
0
        public async Task <OrganizationDTO> Register(OrganizationTransferDTO dto)
        {
            var organizationEntity = DataContext.NetworkEntities.Add(new NetworkEntity {
                NetworkID = dto.NetworkID.Value, NetworkEntityID = dto.ID, EntityType = EntityType.Organization
            });
            await DataContext.SaveChangesAsync();

            Data.Organization organization = DataContext.Organizations.Add(new Data.Organization()
            {
                ID               = organizationEntity.ID,
                Name             = dto.Name,
                NetworkID        = dto.NetworkID.Value,
                Acronym          = dto.Acronym,
                ContactEmail     = dto.ContactEmail,
                ContactPhone     = dto.ContactPhone,
                ContactFirstName = dto.ContactFirstName,
                ContactLastName  = dto.ContactLastName
            });

            if (dto.ParentOrganizationID.HasValue)
            {
                organization.ParentOrganizationID = dto.ParentOrganizationID.Value;
            }

            await DataContext.SaveChangesAsync();

            if (dto.Metadata != null && dto.Metadata.Count() > 0)
            {
                IList <Data.OrganizationDomainData> metadata = new List <Data.OrganizationDomainData>();
                foreach (var meta in dto.Metadata)
                {
                    var orgMeta = new OrganizationDomainData()
                    {
                        OrganizationID = organization.ID,
                        DomainUseID    = meta.DomainUseID,
                        Value          = meta.Value,
                        SequenceNumber = meta.SequenceNumber,
                    };
                    if (meta.DomainReferenceID.HasValue)
                    {
                        orgMeta.DomainReferenceID = meta.DomainReferenceID.Value;
                    }
                    metadata.Add(orgMeta);
                }
                if (metadata.Count > 0)
                {
                    DataContext.DomainDatas.AddRange(metadata);
                }
                await DataContext.SaveChangesAsync();
            }

            return(organization.Map <Organization, OrganizationDTO>());
        }
Exemple #2
0
        public async Task <OrganizationDTO> Update(OrganizationTransferDTO dto)
        {
            var organizationMetadata = await DataContext.DomainDatas.OfType <OrganizationDomainData>().Where(x => x.OrganizationID == dto.ID).ToArrayAsync();

            var organization = await DataContext.Organizations.FindAsync(dto.ID);

            if (organization.Deleted)
            {
                throw new Exception("The specified Organization was not found.");
            }
            organization.Name             = dto.Name;
            organization.Acronym          = dto.Acronym;
            organization.ContactEmail     = dto.ContactEmail;
            organization.ContactPhone     = dto.ContactPhone;
            organization.ContactFirstName = dto.ContactFirstName;
            organization.ContactLastName  = dto.ContactLastName;

            if (dto.Metadata != null && dto.Metadata.Count() > 0)
            {
                IList <OrganizationDomainData> metaDataToAdd = new List <OrganizationDomainData>();
                foreach (var meta in dto.Metadata.Where(m => !m.ID.HasValue))
                {
                    var orgMeta = new OrganizationDomainData()
                    {
                        OrganizationID = organization.ID,
                        DomainUseID    = meta.DomainUseID,
                        Value          = meta.Value,
                        SequenceNumber = meta.SequenceNumber,
                    };
                    if (meta.DomainReferenceID.HasValue)
                    {
                        orgMeta.DomainReferenceID = meta.DomainReferenceID.Value;
                    }
                    metaDataToAdd.Add(orgMeta);
                }
                if (metaDataToAdd.Count > 0)
                {
                    DataContext.DomainDatas.AddRange(metaDataToAdd);
                }

                foreach (var meta in organizationMetadata.Where(org => dto.Metadata.Any(m => m.ID == org.ID && (org.Value != m.Value || org.SequenceNumber != m.SequenceNumber || org.DomainReferenceID != m.DomainReferenceID))))
                {
                    var diff = dto.Metadata.Where(m => m.ID == meta.ID).FirstOrDefault();
                    DataContext.DomainDatas.Attach(meta);
                    if (meta.Value != diff.Value)
                    {
                        meta.Value = diff.Value;
                    }
                    if (meta.SequenceNumber != diff.SequenceNumber)
                    {
                        meta.SequenceNumber = diff.SequenceNumber;
                    }
                    if (meta.DomainReferenceID != diff.DomainReferenceID)
                    {
                        meta.DomainReferenceID = diff.DomainReferenceID;
                    }
                }
                await DataContext.SaveChangesAsync();
            }

            var metadataIDs = dto.Metadata.Where(x => x.ID.HasValue).Select(x => x.ID.Value);
            var remove      = (from d in organizationMetadata
                               where !metadataIDs.Contains(d.ID) && d.OrganizationID == dto.ID
                               select d.ID).ToArray();


            if (remove.Count() > 0)
            {
                //Have to do this cause of trigger
                var removeIDs = String.Join(",", remove.Select(x => String.Format("'{0}'", x)));
                await DataContext.Database.ExecuteSqlCommandAsync(string.Format("delete from OrganizationDomainData where ID IN ({0})", removeIDs));
            }

            return(organization.Map <Organization, OrganizationDTO>());
        }