public IActionResult Create([Bind("Title,CategoryId,File,Content,CreationTime,isMain")] News news) { if (news.File != null) { var fileName = Path.GetFileNameWithoutExtension(news.File.FileName) + "-" + DateTime.Now.ToString("MM-dd-yyyy") + Path.GetExtension(news.File.FileName); news.FileName = fileName; if (ModelState.IsValid) { var rootPath = Path.Combine(_webHost.WebRootPath, "images", news.FileName); using (FileStream fileStream = new FileStream(rootPath, FileMode.Create)) { news.File.CopyTo(fileStream); } news.CreationTime = DateTime.Now; _newsRepository.AddNews(news); return(RedirectToAction(nameof(Index))); } } ViewBag.Categories = _categoryRepository.GetSelectItems(); return(View(news)); }
public bool SaveNews(string username, string title, string summary, string mainContent, string category, HttpPostedFileBase NewsImage, INewsRepository repository) { var user = repository.Users.FirstOrDefault(x => x.UserName == username); if (user == null) { return(false); } var category_of_news = repository.Categories.FirstOrDefault(x => x.Title == category); if (category_of_news == null) { return(false); } var new_item = new DAL.DataBase.News { MainContent = mainContent, Summary = summary, Title = title, Staus = (int)NewsStatusEnum.Waiting, UserId = user.Id, CategoryId = category_of_news.Id, }; new_item.ImageData = new byte[NewsImage.ContentLength]; NewsImage.InputStream.Read(new_item.ImageData, 0, NewsImage.ContentLength); new_item.ImageMimeType = NewsImage.ContentType; repository.AddNews(new_item); repository.SaveChanges(); return(true); }
public IHttpActionResult SaveNews() { try { string body = HttpUtility.UrlDecode(HttpContext.Current.Request.Params.Get("Body")); string url = HttpContext.Current.Request.Params["BannerUrl"]; if (string.IsNullOrEmpty(url)) { url = SaveFile(); } else { url = LoadImage(url); } News news = new News() { Id = int.Parse(HttpContext.Current.Request.Params["Id"]), Body = body, Title = HttpContext.Current.Request.Params["Title"], Publication = int.Parse(HttpContext.Current.Request.Params["publication"]), Category = int.Parse(HttpContext.Current.Request.Params["category"]), BannerUrl = url }; newsRepository.AddNews(news); } catch (Exception ex) { return(BadRequest()); } return(Ok()); }
string INewsService.CreateNews(string title, string author, string site, string language, string imgUrl, string contents) { var idNewNews = _newsRepository.AddNews(new News(title, author, site, language, imgUrl, contents)); CheckAddition(idNewNews); _subscribeService.AddedNewNews(idNewNews); return(idNewNews); }
public IActionResult AddNews(News news) { if (ModelState.IsValid) { _repository.AddNews(news); return(RedirectToAction("Index")); } return(View()); }
/// <summary> /// Method to add news /// </summary> /// <param name="news">The news object that is to be added</param> /// <returns>The added news</returns> public async Task <News> AddNews(News news) { bool newsExists = await repository.IsNewsExist(news); if (!newsExists) { return(await repository.AddNews(news)); } throw new NewsAlreadyExistsException("This news is already added"); }
/// <summary> /// Adds news to database. /// </summary> /// <param name="news">News view model.</param> /// <returns></returns> public async Task AddNews(NewsVM news) { await _newsRepository.AddNews(new News { Title = news.Title, Annotation = news.Annotation, Text = news.Text, }); await _newsRepository.SaveChangesAsync(); }
public async Task <ActionResult> AddNews(string editorId, [FromBody] News news) { string tempNewsMainPicture = null; List <Paragraph> tempNewsParagraphs = null; if (!string.IsNullOrEmpty(news.MainPicturePath)) { tempNewsMainPicture = news.MainPicturePath; news.MainPicturePath = null; } foreach (Paragraph p in news.Paragraphs) { if (!string.IsNullOrEmpty(p.PicturePath)) { tempNewsParagraphs = news.Paragraphs; news.Paragraphs = null; break; } } string newsId = await _repository.AddNews(news); if (!string.IsNullOrEmpty(tempNewsMainPicture) || tempNewsParagraphs != null) { if (!string.IsNullOrEmpty(tempNewsMainPicture)) { news.MainPicturePath = FileManagerService.SaveImageToFile(tempNewsMainPicture, "mainPicture" + news.Id); } if (tempNewsParagraphs != null) { int i = 0; news.Paragraphs = tempNewsParagraphs; foreach (Paragraph p in tempNewsParagraphs) { if (!string.IsNullOrEmpty(p.PicturePath)) { news.Paragraphs.ElementAt(i).PicturePath = FileManagerService.SaveImageToFile(p.PicturePath, "paragraphPicture" + i + news.Id); } i++; } } await _repository.AddNewsPictures(newsId, news.MainPicturePath, news.Paragraphs); } if (newsId != null && newsId != "") { await _editorRepository.AddNews(editorId, newsId); } return(Ok()); }
public void AddNews_ShouldThrowException_forDuplicateTitle() { //arrange News newNews = new News { Title = "news 1 title" }; //act Action act = () => _repository.AddNews(newNews); //assert Assert.Throws <ArgumentException>(act); }
public async Task <News> AddNews(News news) { var _news = await repository.IsNewsExist(news); if (_news) { throw new NewsAlreadyExistsException($"This news is already added"); } else { return(await repository.AddNews(news)); } }
public bool AddNews(string title, string description) { DAL.Tables.News model = new DAL.Tables.News { Title = title, Description = description, InsertTime = DateTime.Now }; bool result = _newsRespository.AddNews(model); return(result); }
public ActionResult Create(FormCollection collection, HttpPostedFileBase file) { try { string pathimage = null; EfDbContext _context = new EfDbContext(); int count = 0; foreach (News n in _context.Newsfeed) { count = int.Parse(n.NewsID); } count++; string id = count.ToString(); if (file != null) { //string pic = System.IO.Path.GetFileName(file.FileName); string path = System.IO.Path.Combine( Server.MapPath("~/Content/Images/News/")); // file is uploaded string extension = System.IO.Path.GetExtension(file.FileName); string fileName = id; file.SaveAs(System.IO.Path.Combine(path, fileName + extension)); //file.SaveAs(path); pathimage = "~/Content/Images/News/" + fileName + extension; } var title = collection["NewsTitle"]; var descrip = collection["NewsDescription"]; News news = new News(); news.NewsID = id; news.NewsTitle = title; news.NewsImages = pathimage; news.NewsDescription = descrip; repository.AddNews(news); return(RedirectToAction("Index")); } catch { return(View()); } }
public void AddCheck(string newsId, Comment comment) { if (newsRepo.GetByNewsAppRef(newsId) != null) { var oldNews = newsRepo.GetByNewsAppRef(newsId); commentRepo.AddComment(oldNews, comment); } else { News news = new News() { NewsAppRef = newsId }; newsRepo.AddNews(news); commentRepo.AddComment(news, comment); } }
public async Task <ActionResult <News> > AddNews(NewsPostRequest data) { var user = await _userRepository.GetUserByTokenId(data.TokenId); if (user == null || user.Role == "user") { return(Unauthorized()); } var result = await _repository.AddNews(user.Id, data.Description, DateTime.Now); if (result > 0) { return(Created("New feedback added", result)); } return(BadRequest()); }
public IActionResult ProcessInPut(NewsInPut newsInPut) { if (ModelState.IsValid) { newsInPut.UserId = Int32.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)); NewsInfo news = new NewsInfo(); news.NewsTitle = newsInPut.NewsTitle; news.Date = System.DateTime.Today.ToShortDateString(); news.Content = newsInPut.Content; news.ImageThumbnailUrl = newsInPut.ImageThumbnailUrl; news.ShortDescription = newsInPut.ShortDescription; _newsRepository.AddNews(news); return RedirectToAction("NewsInputComplete"); } return View(); }
public async Task <IActionResult> Create(Entity.News entity, IFormFile file) { if (ModelState.IsValid) { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\img", file.FileName); using (var stream = new FileStream(path, FileMode.Create)) { await file.CopyToAsync(stream); } entity.IsActiv = true; entity.ViewCount = 0; entity.NewsImg = file.FileName; newsRepository.AddNews(entity); return(RedirectToAction("Index")); } return(View(entity)); }
public async Task <IActionResult> Index(News news) { //if(ModelState.IsValid) //{ var newsId = news.NewsId; if (newsId == 0) { var newsList = await _newsRepository.GetAllNews(null); for (int x = 101; x < 10000; x++) { var find = newsList?.Find(c => c.NewsId == x); if (find == null) { newsId = x; break; } } } if (news != null) { if (System.IO.File.Exists(news.UrlToImage)) { FileInfo fi = new FileInfo(news.UrlToImage); var fileContent = System.IO.File.ReadAllBytes(news.UrlToImage); var path = Path.GetFullPath("wwwroot/images"); news.UrlToImage = $"/images/{newsId + 1}{fi.Extension}"; System.IO.File.WriteAllBytes($"{path}\\{newsId + 1}{fi.Extension}", fileContent); } else { news.UrlToImage = "dummy"; } news.NewsId = newsId; news.UserId = TempData.Peek("uId").ToString(); await _newsRepository.AddNews(news); } return(RedirectToAction("Index")); }
public async Task <IActionResult> Index(News news) { string prevUserId = news.UserId; string filepath = string.Empty; if (ModelState.IsValid) { if (Request?.Form?.Files?.Any() ?? false) { filepath = Path.GetFullPath("wwwroot/images/"); if (!Directory.Exists(filepath)) { Directory.CreateDirectory(filepath); } filepath = Path.Combine(filepath, Request.Form.Files.First().FileName); if (System.IO.File.Exists(filepath)) { System.IO.File.Delete(filepath); } using (var stream = System.IO.File.Create(filepath)) { await Request.Form.Files.First().CopyToAsync(stream); } news.UrlToImage = Request.Form.Files.First().FileName; } news = await newsRepository.AddNews(news); } if (news == null) { if (!string.IsNullOrEmpty(filepath)) { if (System.IO.File.Exists(filepath)) { System.IO.File.Delete(filepath); } } ModelState.AddModelError("OperationFailed", "Something went wrong while adding the news!!!"); } TempData["uId"] = prevUserId; return(RedirectToAction("Index")); }
public void AddNews([FromBody] News news) { _newsRepository.AddNews(news); }
/// <summary> /// method to add news to the news db collection /// </summary> /// <param name="news"></param> /// <returns></returns> public News AddNews(News news) { return(_newsRepository.AddNews(news)); }
public async Task <IActionResult> AddNews(News news) { await _repository.AddNews(news); return(Created($"/api/News/news.id", new { news.Id })); }