コード例 #1
0
        public async Task <IActionResult> Add(SectorMappingsModel model)
        {
            var response = await mappingService.AddAsync(model);

            if (!response.Success)
            {
                return(BadRequest(response.Message));
            }
            return(Ok(true));
        }
コード例 #2
0
        public async Task <ActionResponse> AddAsync(SectorMappingsModel model)
        {
            using (var unitWork = new UnitOfWork(context))
            {
                ActionResponse response = new ActionResponse();
                IMessageHelper mHelper;
                try
                {
                    var sectorType = unitWork.SectorTypesRepository.GetByID(model.SectorTypeId);
                    if (sectorType == null)
                    {
                        mHelper          = new MessageHelper();
                        response.Success = false;
                        response.Message = mHelper.GetNotFound("Sector Type");
                        return(await Task <ActionResponse> .Run(() => response).ConfigureAwait(false));
                    }

                    if (sectorType.IsPrimary == true)
                    {
                        mHelper          = new MessageHelper();
                        response.Success = false;
                        response.Message = mHelper.InvalidSectorMapping(sectorType.TypeName);
                        return(await Task <ActionResponse> .Run(() => response).ConfigureAwait(false));
                    }

                    var sectors     = unitWork.SectorRepository.GetManyQueryable(s => (model.MappingIds.Contains(s.Id) || s.Id == model.SectorId));
                    int sectorCount = sectors.Count() - 1;
                    if (sectorCount < model.MappingIds.Count)
                    {
                        mHelper          = new MessageHelper();
                        response.Success = false;
                        response.Message = mHelper.GetNotFound("Sector/s");
                        return(await Task <ActionResponse> .Run(() => response).ConfigureAwait(false));
                    }
                    var sector = (from s in sectors
                                  where s.Id == model.SectorId
                                  select s).FirstOrDefault();

                    var strategy = context.Database.CreateExecutionStrategy();
                    await strategy.ExecuteAsync(async() =>
                    {
                        using (var transaction = context.Database.BeginTransaction())
                        {
                            var sectorMappings = await unitWork.SectorMappingsRepository.GetManyQueryableAsync(m => m.SectorId == model.SectorId);
                            List <MappingsKeyView> mappingsView = (from m in sectorMappings
                                                                   select new MappingsKeyView
                            {
                                SectorId = m.SectorId,
                                MappingId = m.MappedSectorId
                            }).ToList <MappingsKeyView>();

                            List <EFSectorMappings> mappingsList = new List <EFSectorMappings>();
                            MappingsKeyView mappingView          = null;
                            foreach (var id in model.MappingIds)
                            {
                                mappingView = (from m in mappingsView
                                               where m.SectorId == sector.Id && id == m.MappingId
                                               select m).FirstOrDefault();

                                if (mappingView == null)
                                {
                                    mappingsList.Add(new EFSectorMappings()
                                    {
                                        SectorId       = id,
                                        SectorTypeId   = sectorType.Id,
                                        MappedSectorId = sector.Id
                                    });
                                }
                            }

                            if (mappingsList.Count > 0)
                            {
                                unitWork.SectorMappingsRepository.InsertMultiple(mappingsList);
                                await unitWork.SaveAsync();
                                transaction.Commit();
                            }
                        }
                    });
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Message = ex.Message;
                }
                return(await Task <ActionResponse> .Run(() => response).ConfigureAwait(false));
            }
        }