public string CreateNew(string title) { using (var db = new NewsDB()) { if (String.IsNullOrEmpty(title)) { throw new LogicException("Title can't be empty!"); } // 1. Topic title should be unique var sameTitle = db.Topics.FirstOrDefault(t => t.Title.ToLower() == title.ToLower()); if (sameTitle != null) { throw new LogicException("Topic already exists!"); } db.Topics.Add(new Topic() { Title = title, }); db.SaveChanges(); return(null); } }
public Topic GetTopic(int id) { using (var db = new NewsDB()) { return(db.Topics.FirstOrDefault(t => t.Id == id)); } }
static void Main() { var migrationStrategy = new MigrateDatabaseToLatestVersion <NewsDB, Configuration>(); Database.SetInitializer(migrationStrategy); var context = new NewsDB(); var news = context.News.First(); Console.WriteLine(news.NewsContent); Console.WriteLine("Please add news update: "); //string input = Console.ReadLine(); news.NewsContent = Console.ReadLine(); Console.ReadLine(); try { context.SaveChanges(); } catch (DbUpdateConcurrencyException e) { Console.WriteLine("A concurrency poblem occuret. It seems the data has been changed. Please try again"); Main(); } }
public Article GetById(int id) { using (var db = new NewsDB()) { return(db.Articles.FirstOrDefault(a => a.Id == id)); } }
public async Task <ActionResult> CreateNews(CorrectNewsForm news) { var user = await _userManager.GetUserAsync(HttpContext.User); if (ModelState.IsValid) { var newsDB = new NewsDB { Id = _rand.Next(), Title = news.Title, Body = news.Body, AuthorId = user.Id, CategotyId = news.CategoryId, Date = DateTime.Now }; _newsProvider.AddNewsDB(newsDB); var path = Configuration.GetValue <String>("Path1") + newsDB.Id.ToString(); if (news.Picture != null && news.Picture.Length > 0 && ImageUtils.IsImage(news.Picture)) { using (var stream = new FileStream(path, FileMode.OpenOrCreate)) { await news.Picture.CopyToAsync(stream); } } } else { ModelState.AddModelError(string.Empty, "Не удалось сохранить новость"); } return(RedirectToAction("UserProfile", "User")); }
public List <Article> GetLatestNews(int count = 6) { using (var db = new NewsDB()) { return(db.Articles.OrderByDescending(a => a.PublishedOn).Take(count).ToList()); } }
public int DeleteNew(int Id) { if (Id > 0) { return(NewsDB.Delete(Id)); } return(-1); }
public List <News> GetMoreNews(String search) { if (search == "") { search = null; } return(NewsDB.GetNews(search)); }
public List <News> GetNewsOfCategory(int numberStart, int CategoryId) { if (CategoryId > 0) { return(NewsDB.GetNewsOfCategory(numberStart, numberStart + this.numNewsOfPage, CategoryId)); } return(null); }
public Tuple <List <News>, int> GetNewsOfPageCategory(int category, int pageIndex, int pageSize) { if (pageIndex > 0 && pageSize > 0) { return(NewsDB.GetNewsPageCategory(category, pageIndex, pageSize)); } return(null); }
public List <Article> GetByTopic(int topicId) { using (var db = new NewsDB()) { return(db.Articles.Where(a => a.TopicId == topicId) .OrderByDescending(a => a.PublishedOn) .ToList()); } }
public List <Topic> GetAllTopics() { //returns Topics, ordered by Title ASC using (var db = new NewsDB()) { // SELECT * FROM Topics ORDER BY Title return(db.Topics.OrderBy(t => t.Title).ToList()); } }
public News GetNew(int Id) { if (Id > 0) { return(NewsDB.GetNew(Id)); } else { return(null); } }
// GET: MainPage public ActionResult Main() { if (Session["User"] != null) { NewsDB db = new NewsDB(); string s = Session["User"].ToString(); ViewBag.user = s; int id = db.Tbl_User.Where(x => x.User_Name.Equals(s)).SingleOrDefault().User_id;; ViewBag.id = id; } return(View()); }
public ActionResult View_Post(int id) { Rep_posts post = new Rep_posts(); Tbl_Posts posts = post.Get_Post(id); if (Session["User"] != null) { NewsDB db = new NewsDB(); string s = Session["User"].ToString(); ViewBag.user = s; int ids = db.Tbl_User.Where(x => x.User_Name.Equals(s)).SingleOrDefault().User_id;; ViewBag.id = ids; } return(View(posts)); }
private async Task <NewsInfo> ToNewsInfo(NewsDB news) { var comments = await _commentsService.GetCommenstsByNewsId(news.Id); var commentsList = new List <Comment>(); if (comments != null) { commentsList = comments.OrderBy(c => c.Date).ToList(); } CategoryDB category = _categoriesProvider.GetCategory(news.CategotyId); AuthorInfo author = await _usersService.GetAuthor(news.AuthorId); return(new NewsInfo(news.Id, category, author, commentsList, news.Date, news.Title, news.Body)); }
public int Create(News news, HttpPostedFileBase fileAvatar) { if (fileAvatar != null && fileAvatar.ContentLength > 0) { String name = fileAvatar.FileName.ToString(); if (name.IndexOf(".png") == name.Length - 4 || name.IndexOf(".jpg") == name.Length - 4) { if (news.Name != null && news.Sapo != null && news.Description != null && news.CategoryId > 0) { news.Avatar = name; return(NewsDB.Create(news)); } } } return(-1); }
public ActionResult call_with_us(Tbl_Call u) { NewsDB db = new NewsDB(); Tbl_Call user = new Tbl_Call(); user.name = u.name; user.email = u.email; user.request = u.request; db.Tbl_Call.Add(user); if (Convert.ToBoolean(db.SaveChanges() > 0)) { TempData["message"] = "ثبت درخواست با موفقیت انجام شد"; TempData["info"] = "done"; } else { TempData["message"] = "ثبت درخواست با شکست مواجه شد."; TempData["info"] = "error"; } return(RedirectToAction("Message", "Register")); }
public void LoadData() { try { List <tblNew> list = null; //int pageNum = Int32.Parse(string.IsNullOrEmpty(Request.QueryString["page"]) ? "0" : Request.QueryString["page"].ToString()); list = new NewsDB().GetAllNews(); if (list.Count != 0) { int iRowCount = list.Count; const int iPageSize = 2; const int iItemInPage = 5; int iCurrentPage = Int32.Parse(string.IsNullOrEmpty(Request.QueryString["page"]) ? "1" : Request.QueryString["page"].ToString()); repGetAllUser.DataSource = new NewsDB().GetByPage(list, iPageSize, iCurrentPage); repGetAllUser.DataBind(); CreateListPage(iRowCount, iPageSize, iCurrentPage, iItemInPage, listpage); } } catch (Exception) { } }
public int getNumPage() { int numberNews = NewsDB.GetNews(null).Count; return(numberNews / this.numNewsOfPage); }
public async Task <NewsInfo> GetNews(long id) { NewsDB news = _newsProvider.GetNewsDB(id); return(await ToNewsInfo(news)); }
public Category(long Id, string Name, NewsDB news) { this.Id = Id; this.Name = Name; this.PopularNews = news; }
public void AddNewsDB(NewsDB news) { _context.News.Add(news); _context.SaveChanges(); }
public void UpdateNewsDB(long newsId, NewsDB news) { _context.News.Update(news); _context.SaveChanges(); }