private static void UpdateCatalogValue(CatalogueValueModel model, string userName, VestalisEntities context, bool existCatalogueValue) { //get the current catalogue CatalogueValue catalogueValueModified = context.CatalogueValues.FirstOrDefault(data => data.IsDeleted == false && data.CatalogueValueId == model.CatalogValId); //if the current catalogue is not null, the system continues with the process if (catalogueValueModified != null && catalogueValueModified.CatalogueValueData == model.CatalogValData) { //set the fields to update the information catalogueValueModified.CatalogueValueData = model.CatalogValData; catalogueValueModified.CatalogueValueDescription = model.CatalogDesc; //set auditory fields catalogueValueModified.ModificationBy = userName; catalogueValueModified.ModificationDate = DateTime.UtcNow; //save all changes context.SaveChanges(); } else if (catalogueValueModified != null && catalogueValueModified.CatalogueValueData != model.CatalogValData) { if (!existCatalogueValue) { //set the fields to update the information catalogueValueModified.CatalogueValueData = model.CatalogValData; catalogueValueModified.CatalogueValueDescription = model.CatalogDesc; //set auditory fields catalogueValueModified.ModificationBy = userName; catalogueValueModified.ModificationDate = DateTime.UtcNow; //save all changes context.SaveChanges(); } else { model.Errors.Add(LanguageResource.CatalogueValueExists); } } }
/// <summary> /// Perform the save operation for the current catalogue value /// </summary> /// <param name="model">CatalogueValue</param> /// <returns>ActionResult</returns> public ActionResult SaveCatalogueValue(CatalogueValueModel model) { if (string.IsNullOrEmpty(model.CatalogValData)) { model.Errors.Add("Please write a valid value"); } if (!model.HasErrors) { model = CatalogueBusiness.SaveCatalogueValue(model, UserName); return Json(model); } else { return Json(model); } }
public static CatalogueValueModel SaveCatalogueValue(CatalogueValueModel model, string userName) { using (VestalisEntities context = new VestalisEntities()) { bool existCatalogueValue = context.CatalogueValues.Any(data => data.IsDeleted == false && data.CatalogueId == model.CatalogueId && data.CatalogueValueData == model.CatalogValData); //if the CatalogueValueId is empty, the system will perform the insert operation //otherwise the system will perform the edit operation if (model.CatalogValId == null) { //if the current catalogue is not exists, the system continues with the process if (!existCatalogueValue) { CatalogueValue catalogueValue = new CatalogueValue(); catalogueValue.CatalogueValueData = model.CatalogValData; catalogueValue.CatalogueValueDescription = model.CatalogDesc; catalogueValue.CatalogueId = model.CatalogueId.GetValueOrDefault(); //set auditory fields catalogueValue.CreationBy = userName; catalogueValue.CreationDate = DateTime.UtcNow; //add the new object context.CatalogueValues.AddObject(catalogueValue); //save all changes context.SaveChanges(); } else { model.Errors.Add(LanguageResource.CatalogueValueExists); } } else { UpdateCatalogValue(model, userName, context, existCatalogueValue); } //Remove from the cache the catalogue Catalogue catalogue = GetCatalogueCategory(model.CatalogueId.GetValueOrDefault()); CacheHandler.Remove(String.Format("{0}{1}", catalogue.CatalogueCategoryName, catalogue.BusinessApplicationId)); } return model; }