Example #1
0
        public ActionResponse AddSectorWithMapping(MappingSectorModel model)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                ActionResponse response = new ActionResponse();
                IMessageHelper mHelper;
                try
                {
                    EFSector primarySector = null;
                    var      sectorType    = unitWork.SectorTypesRepository.GetOne(s => s.Id == model.SectorTypeId);
                    if (sectorType == null)
                    {
                        mHelper          = new MessageHelper();
                        response.Message = mHelper.GetNotFound("Sector Type");
                        response.Success = false;
                        return(response);
                    }

                    if (model.SectorId != 0)
                    {
                        primarySector = unitWork.SectorRepository.GetOne(s => s.Id == model.SectorId);
                    }
                    else
                    {
                        primarySector = unitWork.SectorRepository.GetOne(s => s.SectorName.ToLower() == model.SectorName.ToLower().Trim());
                    }

                    if (primarySector != null)
                    {
                        response.ReturnedId = primarySector.Id;
                    }
                    else
                    {
                        var      parentSector = unitWork.SectorRepository.GetByID(model.ParentId);
                        EFSector newSector    = null;

                        if (parentSector != null)
                        {
                            newSector = unitWork.SectorRepository.Insert(new EFSector()
                            {
                                SectorType   = sectorType,
                                ParentSector = parentSector,
                                SectorName   = model.SectorName.Trim(),
                                IATICode     = model.IATICode,
                                TimeStamp    = DateTime.Now,
                            });
                        }
                        else
                        {
                            newSector = unitWork.SectorRepository.Insert(new EFSector()
                            {
                                SectorType = sectorType,
                                SectorName = model.SectorName,
                                IATICode   = model.IATICode,
                                TimeStamp  = DateTime.Now
                            });
                        }
                        unitWork.Save();
                        response.ReturnedId = newSector.Id;
                    }

                    EFSectorMappings mapping = unitWork.SectorMappingsRepository.Get(m => m.SectorId == response.ReturnedId && m.MappedSectorId == model.MappingSectorId);
                    if (mapping == null)
                    {
                        mapping = new EFSectorMappings()
                        {
                            SectorId       = response.ReturnedId,
                            SectorTypeId   = sectorType.Id,
                            MappedSectorId = model.MappingSectorId
                        };
                        unitWork.SectorMappingsRepository.Insert(mapping);
                        unitWork.Save();
                    }
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
                return(response);
            }
        }
Example #2
0
        public SectorMappingsView GetForSector(int id)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                string                  sectorName         = "";
                int                     sectorTypeId       = 0;
                SectorMappingsView      mappingsView       = new SectorMappingsView();
                var                     sectors            = unitWork.SectorRepository.GetManyQueryable(s => s.Id != 0);
                var                     sectorTypes        = unitWork.SectorTypesRepository.GetAll();
                var                     mappings           = unitWork.SectorMappingsRepository.GetManyQueryable(m => m.MappedSectorId == id);
                MappingSectors          mappedSectors      = null;
                List <MappingSectors>   mappingSectorsList = new List <MappingSectors>();
                List <SectorSimpleView> sectorsList        = new List <SectorSimpleView>();

                if (mappings.Count() > 0)
                {
                    mappings = (from mapping in mappings
                                orderby mapping.SectorTypeId ascending
                                select mapping);

                    EFSectorMappings lastMapping = mappings.Last();
                    foreach (var mapping in mappings)
                    {
                        var sectorType = (from sType in sectorTypes
                                          where sType.Id == mapping.SectorTypeId
                                          select sType).FirstOrDefault();
                        if (sectorTypeId != mapping.SectorTypeId)
                        {
                            sectorTypeId = mapping.SectorTypeId;
                            if (mappedSectors != null)
                            {
                                mappedSectors.Sectors = sectorsList;
                                mappingSectorsList.Add(mappedSectors);
                            }
                            mappedSectors = new MappingSectors()
                            {
                                SectorTypeId = mapping.SectorTypeId,
                                SectorType   = sectorType.TypeName
                            };
                            sectorsList = new List <SectorSimpleView>();
                        }

                        sectorName = (from s in sectors
                                      where s.Id == mapping.SectorId
                                      select s).FirstOrDefault().SectorName;
                        sectorsList.Add(new SectorSimpleView()
                        {
                            SectorId = mapping.SectorId,
                            Sector   = sectorName
                        });

                        if (mapping == lastMapping && mappedSectors != null)
                        {
                            mappedSectors.Sectors = sectorsList;
                            mappingSectorsList.Add(mappedSectors);
                        }
                    }
                    mappingsView.Sector        = sectorName;
                    mappingsView.MappedSectors = mappingSectorsList;
                }

                return(mappingsView);
            }
        }