예제 #1
0
        public async Task <IActionResult> OnPostAddedPhotoAsync(IFormFile uploadedFile)
        {
            if (uploadedFile != null)
            {
                if (!uploadedFile.ContentType.Contains("image"))
                {
                    StatusMessage = "Ошибка. Выбранный файл, не изображение.";
                    return(Page());
                }

                string path = "/img/upload/photo/" + uploadedFile.FileName;

                if (System.IO.File.Exists(_appEnvironment.WebRootPath + path))
                {
                    path = "/img/upload/photo/" + new Random().Next(999999999) + uploadedFile.FileName;
                }

                using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
                {
                    await uploadedFile.CopyToAsync(fileStream);
                }
                AspNetFile photo = new AspNetFile()
                {
                    Path    = path,
                    Photo   = true,
                    Title   = Input.Title,
                    DateAdd = DateTime.Now
                };
                _context.AspNetFiles.Add(photo);
                _context.SaveChanges();
                StatusMessage = "Фото успешно загружено!";
                return(RedirectToPage("./Files"));
            }
            return(Page());
        }
예제 #2
0
        public async Task <IActionResult> OnPostSaveChangeNewsAsync(string editor, IFormFile inputImage)
        {
            Random rand = new Random();

            if (_context.AspNetNews.Where(newsDb => newsDb.Url == Input.Url).ToList().Count != 0)
            {
                Input.Url = Input.Url + rand.Next(9999999);
            }
            if (inputImage != null)
            {
                string path = "/images/posts-images/" + Input.Url + inputImage.FileName;
                using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create))
                {
                    await inputImage.CopyToAsync(fileStream);
                }
                _context.AspNetNews.FirstOrDefault(post => post.Id == Input.Id).PreviewPhoto = _context.AspNetFiles.FirstOrDefault(p => p.Title == path).Id;
            }
            _context.AspNetNews.FirstOrDefault(post => post.Id == Input.Id).Author      = Input.Author;
            _context.AspNetNews.FirstOrDefault(post => post.Id == Input.Id).Date        = DateTime.Now;
            _context.AspNetNews.FirstOrDefault(post => post.Id == Input.Id).Description = editor;
            _context.AspNetNews.FirstOrDefault(post => post.Id == Input.Id).Header      = Input.Header;
            _context.AspNetNews.FirstOrDefault(post => post.Id == Input.Id).PreviewText = Input.PreviewText;
            _context.AspNetNews.FirstOrDefault(post => post.Id == Input.Id).Url         = Input.Url;
            _context.SaveChanges();
            return(RedirectToPage("./Posts"));
        }
예제 #3
0
 public IActionResult NewsDetails(string url)
 {
     if (!_context.AspNetNews.Any(news => news.Url == url) || _context.AspNetNews.FirstOrDefault(news => news.Url == url).Blocked)
     {
         Response.StatusCode = 404;
         return(View("NewsNotFound"));
     }
     _context.AspNetNews.FirstOrDefault(newsItem => newsItem.Url == url).Watching++;
     _context.SaveChanges();
     return(View(_context.AspNetNews.Where(newsIdItem => newsIdItem.Url == url).ToList()[0]));
 }
예제 #4
0
 public IActionResult OnGetDeleteNews(int newsId)
 {
     _context.AspNetNews.Remove(_context.AspNetNews.FirstOrDefault(post => post.Id == newsId));
     foreach (var comments in _context.AspNetNewsComments.Where(comment => comment.NewsId == newsId))
     {
         _context.AspNetNewsComments.Remove(comments);
     }
     _context.SaveChanges();
     StatusMessage = "Новость успешно удалена!";
     return(RedirectToPage("./Posts"));
 }
예제 #5
0
 public IActionResult OnGetDeleteFile(int id)
 {
     if (_context.AspNetFiles.Any(p => p.Id == id) && _context.AspNetNews.Count(n => n.PreviewPhoto == id) == 0)
     {
         _context.AspNetFiles.Remove(_context.AspNetFiles.First(p => p.Id == id));
         _context.SaveChanges();
         StatusMessage = "Изображение успешно удалено!";
         return(Page());
     }
     StatusMessage = "Ошибка. Изображение не найдено, либо не удалено!";
     return(Page());
 }
예제 #6
0
 public async Task <IActionResult> OnPostCreateAsync(string editor, string photo)
 {
     if (ModelState.IsValid)
     {
         try
         {
             AspNetNews news = new AspNetNews()
             {
                 Author       = Input.Author,
                 PreviewPhoto = _context.AspNetFiles.FirstOrDefault(p => p.Title == photo).Id,
                 Blocked      = false,
                 Date         = DateTime.Now,
                 Description  = editor,
                 Header       = Input.Header,
                 Likes        = 0,
                 PreviewText  = Input.PreviewText,
                 Share        = 0,
                 Url          = Input.Url,
                 Watching     = 0
             };
             _context.AspNetNews.Add(news);
             _context.SaveChanges();
             StatusMessage = "Новость успешно добавлена на сайт!";
             return(RedirectToPage("./Posts"));
         }
         catch (Exception ex)
         {
             ErrorMessage = "Произошла ошибка, новость не добавлена!";
             return(RedirectToPage("./Posts"));
         }
     }
     else
     {
         return(Page());
     }
 }