public IActionResult AddSectorWithMapping([FromBody] MappingSectorModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var response = sectorService.AddSectorWithMapping(model); if (!response.Success) { return(BadRequest(response.Message)); } return(Ok(response.ReturnedId)); }
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); } }