public static ServiceCommunityAssociation toServiceCommunityAssociation(this ServiceCommunityAssociationV1DTO item)
        {
            if (item == null)
            {
                throw new ArgumentException(nameof(item));
            }

            ServiceCommunityAssociation serviceCommunityAssociation = new ServiceCommunityAssociation();

            serviceCommunityAssociation.CopyFromServiceCommunityAssociationV1DTO(item);
            return(serviceCommunityAssociation);
        }
示例#2
0
        public async Task PostAsync(ServiceV1DTO serviceDTO)
        {
            #region Handle one-to-one relations

            #region Check to see if attempting to add duplicate contact/department/division/program with different ID

            /*
             * We are checking to see if the incoming Service is attempting to
             * re-create an existing department/division using a different ID.
             * This should be corrected to reference the existing department/division.
             * We choose the passed DTO over the passed ID number, and change the ID
             * to match the deperment/division pulled from the database context by "...Code".
             */
            await EnsureNoContactDuplicate(serviceDTO);
            await EnsureNoDepartmentDuplicate(serviceDTO);
            await EnsureNoDivisionDuplicate(serviceDTO);
            await EnsureNoProgramDuplicate(serviceDTO);

            #endregion

            #region Check existence of one-to-one items
            await EnsureContactExistence(serviceDTO);
            await EnsureDepartmentExistence(serviceDTO);
            await EnsureDivisionExistence(serviceDTO);
            await EnsureProgramExistence(serviceDTO);

            #endregion

            #endregion
            var service = new Service();

            _context.Service.Add(service);
            _context.Entry(service).CurrentValues.SetValues(serviceDTO);

            await _context.SaveChangesAsync();

            #region Handle many-to-many relations
            foreach (var cDTO in serviceDTO.CommunityDTOs)
            {
                var community = await EnsureCommunityExistence(cDTO);

                var sca = new ServiceCommunityAssociation {
                    ServiceId = service.ServiceId, CommunityId = community.CommunityId
                };
                _context.ServiceCommunityAssociation.Add(sca);
            }

            foreach (var lDTO in serviceDTO.LanguageDTOs)
            {
                var language = await EnsureLanguageExistence(lDTO);

                var sla = new ServiceLanguageAssociation {
                    ServiceId = service.ServiceId, LanguageId = language.LanguageId
                };
                _context.ServiceLanguageAssociation.Add(sla);
            }

            foreach (var lDTO in serviceDTO.LocationDTOs)
            {
                var location = await EnsureLocationExistence(lDTO);

                var sla = new ServiceLocationAssociation {
                    ServiceId = service.ServiceId, LocationId = location.LocationId
                };
                _context.ServiceLocationAssociation.Add(sla);
            }

            #endregion
            await _context.SaveChangesAsync();
        }
示例#3
0
        public async Task PutAsync(ServiceV1DTO serviceDTO)
        {
            var service = await _context.Service
                          .Where(s => s.ServiceId == serviceDTO.ServiceId)
                          .SingleOrDefaultAsync();

            #region Handle one-to-one relations
            if (service.ContactId != serviceDTO.ContactId)
            {
                await EnsureNoContactDuplicate(serviceDTO);
                await EnsureContactExistence(serviceDTO);
            }
            if (service.DepartmentId != serviceDTO.DepartmentId)
            {
                await EnsureNoDepartmentDuplicate(serviceDTO);
                await EnsureDepartmentExistence(serviceDTO);
            }
            if (service.DivisionId != serviceDTO.DivisionId)
            {
                await EnsureNoDivisionDuplicate(serviceDTO);
                await EnsureDivisionExistence(serviceDTO);
            }
            if (service.ProgramId != serviceDTO.ProgramId)
            {
                await EnsureNoProgramDuplicate(serviceDTO);
                await EnsureProgramExistence(serviceDTO);
            }
            #endregion

            #region Handle many-to-many relations

            var communityIds = new HashSet <int>();
            var scas         = await _context.ServiceCommunityAssociation
                               .Where(sca => sca.ServiceId == serviceDTO.ServiceId)
                               .ToListAsync();

            foreach (var cDTO in serviceDTO.CommunityDTOs)
            {
                //Build community if it needs to be built.
                var community = await EnsureCommunityExistence(cDTO);

                communityIds.Add(community.CommunityId);
            }
            foreach (var id in communityIds)
            {
                var sca = scas.FirstOrDefault(s => s.CommunityId == id);

                // If this is a new entry in SCA table, we need to build the entry and add to table.
                if (sca == null)
                {
                    var scaEntry = new ServiceCommunityAssociation {
                        ServiceId = serviceDTO.ServiceId, CommunityId = id
                    };
                    _context.ServiceCommunityAssociation.Add(scaEntry);
                }
                else
                {
                    scas.Remove(sca);
                }
            }
            foreach (var sca in scas)
            {
                _context.ServiceCommunityAssociation.Remove(sca);
            }


            var languageIds = new HashSet <int>();
            var slas        = await _context.ServiceLanguageAssociation
                              .Where(sla => sla.ServiceId == serviceDTO.ServiceId)
                              .ToListAsync();

            foreach (var lDTO in serviceDTO.LanguageDTOs)
            {
                var language = await EnsureLanguageExistence(lDTO);

                languageIds.Add(language.LanguageId);
            }
            foreach (var id in languageIds)
            {
                var sla = slas.FirstOrDefault(l => l.LanguageId == id);

                if (sla == null)
                {
                    var slaEntry = new ServiceLanguageAssociation {
                        ServiceId = serviceDTO.ServiceId, LanguageId = id
                    };
                    _context.ServiceLanguageAssociation.Add(slaEntry);
                }
                else
                {
                    slas.Remove(sla);
                }
            }
            foreach (var sla in slas)
            {
                _context.ServiceLanguageAssociation.Remove(sla);
            }


            var locationIds = new HashSet <int>();
            var slocs       = await _context.ServiceLocationAssociation
                              .Where(sla => sla.ServiceId == serviceDTO.ServiceId)
                              .ToListAsync();

            foreach (var lDTO in serviceDTO.LocationDTOs)
            {
                var location = await EnsureLocationExistence(lDTO);

                locationIds.Add(location.LocationId);
            }
            foreach (var id in locationIds)
            {
                var sla = slocs.FirstOrDefault(l => l.LocationId == id);

                if (sla == null)
                {
                    var slaEntry = new ServiceLocationAssociation {
                        ServiceId = service.ServiceId, LocationId = id
                    };
                    _context.ServiceLocationAssociation.Add(slaEntry);
                }
                else
                {
                    slocs.Remove(sla);
                }
            }
            foreach (var sla in slocs)
            {
                _context.ServiceLocationAssociation.Remove(sla);
            }

            #endregion
            _context.Entry(service).CurrentValues.SetValues(serviceDTO);
            await _context.SaveChangesAsync();
        }
 public static void CopyFromServiceCommunityAssociation(this ServiceCommunityAssociationV1DTO to, ServiceCommunityAssociation from)
 {
     to.ServiceCommunityAssociationId = from.ServiceCommunityAssociationId;
     to.ServiceId   = from.ServiceId;
     to.CommunityId = from.CommunityId;
 }