public ResultDTO postNew([FromBody] NewsCreateDTO model)
 {
     try
     {
         _context.News.Add(new tblNews()
         {
             DatePost    = model.DatePost,
             Description = model.Description,
             LinkImage   = model.LinkImage,
             Title       = model.Title
         });
         _context.SaveChanges();
         return(new ResultDTO
         {
             Status = 200,
             Message = "OK"
         });
     }
     catch (Exception e)
     {
         return(new ResultDTO
         {
             Status = 500,
             Message = e.Message
         });
     }
 }
        public ResultDTO editNews([FromRoute] int id, [FromBody] NewsCreateDTO model)
        {
            try
            {
                var this_news = _context.News.FirstOrDefault(t => t.Id == id);
                if (this_news == null)
                {
                    return(new ResultDTO
                    {
                        Status = 504,
                        Message = "not found news"
                    });
                }

                this_news.Title       = model.Title;
                this_news.LinkImage   = model.LinkImage;
                this_news.Description = model.Description;
                this_news.DatePost    = model.DatePost;
                _context.SaveChanges();
                return(new ResultDTO
                {
                    Status = 200,
                    Message = "OK"
                });
            }
            catch (Exception e)
            {
                return(new ResultDTO
                {
                    Status = 500,
                    Message = e.Message
                });
            }
        }
Пример #3
0
 public IActionResult Create(NewsCreateDTO news)
 {
     if (ModelState.IsValid)
     {
         News addedNews = _newsRepository.Create(news);
         return(RedirectToAction("details", new { id = addedNews.Id }));
     }
     else
     {
         return(View());
     }
 }
Пример #4
0
        private string UploadedImage(NewsCreateDTO news)
        {
            string fileName = null;

            if (news.ImageFile != null)
            {
                string imagesFolder = Path.Combine(_hostingEnvironment.WebRootPath, "pictures");
                fileName = Guid.NewGuid().ToString() + "_" + news.ImageFile.FileName;
                string filePath = Path.Combine(imagesFolder, fileName);
                news.ImageFile.CopyTo(new FileStream(filePath, FileMode.Create));
            }

            return(fileName);
        }
Пример #5
0
        public News Create(NewsCreateDTO news)
        {
            string fileName  = UploadedImage(news);
            News   addedNews = new News
            {
                Title            = news.Title,
                ShortDescription = news.ShortDescription,
                Content          = news.Content,
                Image            = fileName
            };

            _dbContext.News.Add(addedNews);
            _dbContext.SaveChanges();
            return(addedNews);
        }