/// <summary> /// Delete a news /// </summary> /// <param name="NewsId"></param> /// <returns></returns> public bool DeleteNews(int NewsId) { bool result = false; try { News news = GetNewsById(NewsId); if (news != null && news.Id > 0) { int?ScheduledTaskId = news.ScheduledTasks?.FirstOrDefault()?.Id; if (ScheduledTaskId != null) { _scheduledTaskService.CancelTaskById(ScheduledTaskId.Value); } List <Tuple <string, object> > Parameters = new List <Tuple <string, object> >(); Parameters.Add(new Tuple <string, object>("@NewsId", NewsId)); result = _newsRepo.ExecuteStoredProcedure("DeleteNewsById", Parameters); } } catch (Exception e) { result = false; Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "NewsId = " + NewsId); } return(result); }
public ActionResult EditNews(NewsEditViewModel Model) { bool _success = false; string _Error = ""; bool _isCreation = false; try { if (User.Identity.IsAuthenticated) { if (ModelState.IsValid) { Model.PublishDate = Model.PublishDate.ToUniversalTime(); Model.LastModificationUserId = UserSession.UserId; if (Model.TypeId == CommonsConst.NewsType.PublishOnly) { Model.MailSubject = null; Model.TypeUserMailingId = null; } if (Model.Id <= 0) { _isCreation = true; int NewsId = _newsService.CreateNews(Model); Model.Id = NewsId; if (NewsId > 0) { _success = true; } } else { _success = _newsService.EditNews(Model); } // Scehdule if (_success) { if (!Model.HasScheduledTaskBeenExecuted && Model.ScheduledTaskId.HasValue) { _success = _scheduledTaskService.CancelTaskById(Model.ScheduledTaskId.Value); } if (_success && !Model.HasScheduledTaskBeenExecuted && Model.TypeId != CommonsConst.NewsType.PublishOnly && Model.Active) { if (Model.PublishDate < DateTime.UtcNow) { Model.PublishDate = DateTime.UtcNow.AddSeconds(5); } _success = _scheduledTaskService.ScheduleNews(Model.Id, Model.PublishDate - DateTime.UtcNow); } } } else { _Error = ModelStateHelper.GetModelErrorsToDisplay(ModelState); } } else { _Error = "[[[You are not logged in.]]]"; } if (!_success && String.IsNullOrWhiteSpace(_Error)) { _Error = "[[[Error while saving the update.]]]"; } } catch (Exception e) { _success = false; Commons.Logger.GenerateError(e, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Id = " + Model.Id); } return(Json(new { Result = _success, Error = _Error, IsCreation = _isCreation })); }