public CourseProjectNews SaveNews(CourseProjectNews news) { var cpEditNews = Context.CourseProjectNewses.FirstOrDefault(x => x.Id == news.Id && x.SubjectId == news.SubjectId); if (cpEditNews != null) { CourseProjectNews cpnews = new CourseProjectNews(); cpEditNews.Body = news.Body; cpEditNews.Disabled = news.Disabled; cpEditNews.EditDate = news.EditDate; cpEditNews.SubjectId = news.SubjectId; cpEditNews.Title = news.Title; } else { Context.CourseProjectNewses.Add(news); } Context.SaveChanges(); return news; }
public void DeleteNews(CourseProjectNews news) { var cpnews = Context.CourseProjectNewses.Single(x => x.Id == news.Id && x.SubjectId==news.SubjectId); Context.CourseProjectNewses.Remove(cpnews); Context.SaveChanges(); }
public CourseProjectNews SaveNews(CourseProjectNews news, IList<Attachment> attachments, Int32 userId) { using (var repositoriesContainer = new LmPlatformRepositoriesContainer()) { if (!string.IsNullOrEmpty(news.Attachments)) { var deleteFiles = repositoriesContainer.AttachmentRepository.GetAll( new Query<Attachment>(e => e.PathName == news.Attachments)).ToList().Where(e => attachments.All(x => x.Id != e.Id)).ToList(); foreach (var attachment in deleteFiles) { FilesManagementService.DeleteFileAttachment(attachment); } } else { news.Attachments = GetGuidFileName(); } FilesManagementService.SaveFiles(attachments.Where(e => e.Id == 0), news.Attachments); foreach (var attachment in attachments) { if (attachment.Id == 0) { attachment.PathName = news.Attachments; repositoriesContainer.AttachmentRepository.Save(attachment); } } } var cpEditNews = Context.CourseProjectNewses.FirstOrDefault(x => x.Id == news.Id && x.SubjectId == news.SubjectId); if (cpEditNews != null) { CourseProjectNews cpnews = new CourseProjectNews(); cpEditNews.Body = news.Body; cpEditNews.Disabled = news.Disabled; cpEditNews.EditDate = news.EditDate; cpEditNews.SubjectId = news.SubjectId; cpEditNews.Attachments = news.Attachments; cpEditNews.Title = news.Title; } else { Context.CourseProjectNewses.Add(news); } Context.SaveChanges(); return news; }