public ActionResult Create(AdsCategory entity, ICollection <AdsCategoryCulture> adsCategoryCulture) { if (!ModelState.IsValid) { this.LoadViewBagData(); return(View()); } else { var purposeVinculated = (Request.Form["IDPurposeVinculated"] != null) ? Request.Form["IDPurposeVinculated"].Split(',').Select(n => int.Parse(n)).ToArray() : new int[] { }; var categoryVinculated = (Request.Form["IDHierarchyStructureCategoryVinculated"] != null) ? Request.Form["IDHierarchyStructureCategoryVinculated"].Split(',').Select(n => int.Parse(n)).ToArray() : new int[] { }; var typeVinculated = (Request.Form["IDHierarchyStructureTypeVinculated"] != null) ? Request.Form["IDHierarchyStructureTypeVinculated"].Split(',').Select(n => int.Parse(n)).ToArray() : new int[] { }; if (!entity.IDAdsCategory.HasValue) { entity.CreateDate = DateTime.Now; entity.CreatedBy = SiteContext.ActiveUserName; entity.ModifyDate = DateTime.Now; entity.ModifiedBy = SiteContext.ActiveUserName; entity.Status = (short)SpongeSolutions.ServicoDireto.Model.InfraStructure.Enums.StatusType.Active; ServiceContext.AdsCategoryService.Insert(entity, adsCategoryCulture, purposeVinculated, categoryVinculated, typeVinculated); this.TempData["Message"] = Internationalization.Message.Record_Inserted_Successfully; } else { entity.ModifyDate = DateTime.Now; entity.ModifiedBy = SiteContext.ActiveUserName; ServiceContext.AdsCategoryService.Update(entity, adsCategoryCulture, purposeVinculated, categoryVinculated, typeVinculated); this.TempData["Message"] = Internationalization.Message.Record_Updated_Successfully; } this.LoadViewBagData(); return(RedirectToAction("create", new { id = entity.IDAdsCategory.Value })); } }
public void Update(AdsCategory entity) { using (var context = new ImobeNetContext()) { var adsCategoryRepository = new BaseRepository <AdsCategory>(context); adsCategoryRepository.Update(entity); adsCategoryRepository.SaveChanges(); } }
public void InsertUpdate(AdsCategory entity, ICollection <AdsCategoryCulture> adsCategoryCulture, int[] purposeVinculated, int[] categoryVinculated, int[] typeVinculated, Enums.ActionType actionType) { using (var context = new ImobeNetContext()) { using (TransactionScope scopeOfTransaction = new TransactionScope()) { try { var adsCategoryRelationRepository = new BaseRepository <AdsCategoryRelation>(context); var adsCategoryRepository = new BaseRepository <AdsCategory>(context); var adsCategoryCultureRepository = new BaseRepository <AdsCategoryCulture>(context); if (actionType == Enums.ActionType.Insert) { adsCategoryRepository.Insert(entity); } else if (actionType == Enums.ActionType.Update) { adsCategoryRepository.Update(entity); } context.SaveChanges(); //Buscando os valores de culturas var cultureValues = (from acc in context.AdsCategoryCulture where acc.IDAdsCategory == entity.IDAdsCategory.Value select acc); //Deletando as culturas if (cultureValues != null && cultureValues.Count() > 0) { foreach (var item in cultureValues) { adsCategoryCultureRepository.Delete(item); } context.SaveChanges(); } // Inserindo os novos valores da cultura foreach (var item in adsCategoryCulture) { item.IDAdsCategory = entity.IDAdsCategory; adsCategoryCultureRepository.Insert(item); } context.SaveChanges(); //Excluindo todos os vínculos var vinculatedValues = (from ac in context.AdsCategoryRelation where ac.IDAdsCategory == entity.IDAdsCategory.Value select ac); if (vinculatedValues != null && vinculatedValues.Count() > 0) { foreach (var item in vinculatedValues) { adsCategoryRelationRepository.Delete(item); } context.SaveChanges(); } //Inserindo vínculos com propositos if (purposeVinculated != null && purposeVinculated.Count() > 0) { foreach (var item in purposeVinculated) { adsCategoryRelationRepository.Insert(new AdsCategoryRelation() { IDAdsCategory = entity.IDAdsCategory.Value, IDPurpose = item }); } context.SaveChanges(); } //Inserindo vínculos com categorias if (categoryVinculated != null && categoryVinculated.Count() > 0) { foreach (var item in categoryVinculated) { adsCategoryRelationRepository.Insert(new AdsCategoryRelation() { IDAdsCategory = entity.IDAdsCategory.Value, IDHierarchyStructure = item }); } context.SaveChanges(); } //Inserindo vínculos com tipo if (typeVinculated != null && typeVinculated.Count() > 0) { foreach (var item in typeVinculated) { adsCategoryRelationRepository.Insert(new AdsCategoryRelation() { IDAdsCategory = entity.IDAdsCategory.Value, IDHierarchyStructure = item }); } context.SaveChanges(); } if (entity.IDCustomer.HasValue) { adsCategoryRelationRepository.Insert(new AdsCategoryRelation() { IDAdsCategory = entity.IDAdsCategory.Value, IDCustomer = entity.IDCustomer.Value }); context.SaveChanges(); } scopeOfTransaction.Complete(); } catch (Exception ex) { throw ex; } finally { scopeOfTransaction.Dispose(); } } } }
public void Update(AdsCategory entity, ICollection <AdsCategoryCulture> adsCategoryCulture, int[] purposeVinculated, int[] categoryVinculated, int[] typeVinculated) { this.InsertUpdate(entity, adsCategoryCulture, purposeVinculated, categoryVinculated, typeVinculated, Enums.ActionType.Update); }