public async Task <AllNewsListingViewModel> CreateAsync(NewsCreateInputModel newsCreateInputModel, string userId) { var imageUrl = await this.cloudinaryService .UploadAsync(newsCreateInputModel.Image, newsCreateInputModel.Title + Suffixes.NewsSuffix); var news = new News { Title = newsCreateInputModel.Title, Description = newsCreateInputModel.Description, ShortDescription = newsCreateInputModel.ShortDescription, ImagePath = imageUrl, UserId = userId, }; bool doesNewsExist = await this.newsRepository .All() .AnyAsync(x => x.Title == news.Title && x.Description == news.Description); if (doesNewsExist) { throw new ArgumentException( string.Format(ExceptionMessages.NewsAlreadyExists, news.Title, news.Description)); } await this.newsRepository.AddAsync(news); await this.newsRepository.SaveChangesAsync(); var viewModel = await this.GetViewModelByIdAsync <AllNewsListingViewModel>(news.Id); return(viewModel); }
public async Task Create_WithCorrectData_ShouldSuccessfullyCreate() { var list = new List <News>(); var mockRepo = new Mock <IDeletableEntityRepository <News> >(); mockRepo.Setup(x => x.All()).Returns(list.AsQueryable()); mockRepo.Setup(x => x.AddAsync(It.IsAny <News>())).Callback((News news) => list.Add(news)); var service = new NewsService(mockRepo.Object); IFormFile image = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.png"); var newsCreateInput = new NewsCreateInputModel { UserId = 6, Title = "Arsenal", Content = null, CategoryId = 6, Image = image, }; await service.CreateAsync(newsCreateInput, "12", "/img/Arsenal_FC.png"); Assert.Single(list.Where(x => x.CreatedByUserId == "12")); }
public async Task Create_WithWrongImageFormat_ShouldThrowException() { var list = new List <News>(); var mockRepo = new Mock <IDeletableEntityRepository <News> >(); mockRepo.Setup(x => x.All()).Returns(list.AsQueryable()); mockRepo.Setup(x => x.AddAsync(It.IsAny <News>())).Callback((News news) => list.Add(news)); var service = new NewsService(mockRepo.Object); IFormFile image = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.txt"); var newsCreateInput = new NewsCreateInputModel { UserId = 6, Title = "Arsenal", Content = null, CategoryId = 6, Image = image, }; await Assert.ThrowsAsync <ArgumentException>(() => service.CreateAsync(newsCreateInput, "12", "/img/Arsenal_FC.png")); }
public async Task TestAddingNews() { AllNewsListingViewModel allNewsViewModel; using (var stream = File.OpenRead(TestImagePath)) { var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = TestImageContentType, }; var model = new NewsCreateInputModel { Title = "Title", Description = "Information about Avatar movies", ShortDescription = "Just short description", Image = file, }; await this.SeedUsers(); allNewsViewModel = await this.newsService.CreateAsync(model, this.user.Id); } await this.cloudinaryService.DeleteImage(this.cloudinary, allNewsViewModel.Title + Suffixes.NewsSuffix); var count = await this.newsRepository.All().CountAsync(); Assert.Equal(1, count); }
public async Task CheckIfAddingNewsThrowsArgumentException() { this.SeedDatabase(); NewsCreateInputModel news; ArgumentException exception; using (var stream = File.OpenRead(TestImagePath)) { var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = TestImageContentType, }; news = new NewsCreateInputModel { Title = "First news title", Description = "First news description", ShortDescription = "First news short description", Image = file, }; exception = await Assert .ThrowsAsync <ArgumentException>(async() => await this.newsService.CreateAsync(news, this.user.Id)); } await this.cloudinaryService.DeleteImage(this.cloudinary, news.Title + Suffixes.NewsSuffix); Assert.Equal(string.Format(ExceptionMessages.NewsAlreadyExists, news.Title, news.Description), exception.Message); }
public IActionResult Create() { var categories = this.categoriesService.GetAll <CategoryDropDownViewModel>(); var viewModel = new NewsCreateInputModel { Categories = categories, }; return(this.View(viewModel)); }
public async Task <IActionResult> Create(NewsCreateInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var newsId = await this.newsService.CreateAsync(input.Title, input.Content, input.Url, input.Category, null, true, ""); return(this.RedirectToAction("ById", "News", new { id = newsId, area = string.Empty })); }
public async Task <IActionResult> Create(NewsCreateInputModel newsCreateInputModel) { var user = await this.userManager.GetUserAsync(this.User); if (!this.ModelState.IsValid) { return(this.View(newsCreateInputModel)); } await this.newsService.CreateAsync(newsCreateInputModel, user.Id); return(this.RedirectToAction("GetAll", "News", new { area = "Administration" })); }
public async Task <IActionResult> Create(NewsCreateInputModel input) { var user = await this.userManager.GetUserAsync(this.User); try { await this.newsService.CreateAsync( input, user.Id, $"{this.environment.WebRootPath}/images"); } catch (ArgumentException ex) { this.ModelState.AddModelError(string.Empty, ex.Message); } return(this.Redirect("/")); }
public async Task <IActionResult> CreateAsync(NewsCreateInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var user = await this.userManager.GetUserAsync(this.User); var imageUrls = await CloudinaryExtension.UploadMultipleAsync(this.cloudinary, input.Pictures); string latinTitle = Transliteration.CyrillicToLatin(input.Title, Language.Bulgarian); latinTitle = latinTitle.Replace(' ', '-'); _ = await this.newsService.CreateAsync(input.Title, input.Content, user.Id, imageUrls, latinTitle, input.Author); return(this.RedirectToAction("ByName", new { name = latinTitle })); }
public async Task <IActionResult> Create(NewsCreateInputModel input) { if (!this.ModelState.IsValid) { return(this.View(input)); } var user = await this.userManager.GetUserAsync(this.User); if (!this.ModelState.IsValid) { return(this.View(input)); } var newsId = await this.newsService.CreateAsync(input.Title, input.SecondTitle, input.Content, input.CategoryId, input.ImageUrl, user.Id); this.TempData["InfoMessage"] = "News created!"; return(this.RedirectToAction(nameof(this.ById), new { id = newsId })); }
public async Task CreateAsync(NewsCreateInputModel input, string userId, string imagePath) { var news = new News() { CategoryId = input.CategoryId, Content = input.Content, Title = input.Title, CreatedByUserId = userId, }; var extension = Path.GetExtension(input.Image.FileName).TrimStart('.'); if (!this.allowedExtensions.Any(x => extension.EndsWith(x))) { throw new ArgumentException($"Invalid image extension {extension}"); } Directory.CreateDirectory($"{imagePath}/news/"); var dbImage = new Image { News = news, Extension = extension, }; news.Image = dbImage; news.ImageId = dbImage.Id; var physicalPath = $"{imagePath}/news/{dbImage.Id}.{extension}"; using Stream fileStream = new FileStream(physicalPath, FileMode.Create); await input.Image.CopyToAsync(fileStream); await this.newsRepository.AddAsync(news); await this.newsRepository.SaveChangesAsync(); }
public async Task CheckIfAddingNewsReturnsViewModel() { await this.SeedUsers(); NewsCreateInputModel news; AllNewsListingViewModel viewModel; using (var stream = File.OpenRead(TestImagePath)) { var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name)) { Headers = new HeaderDictionary(), ContentType = TestImageContentType, }; news = new NewsCreateInputModel { Title = "Random news title", Description = "Random news description", ShortDescription = "Random short description", Image = file, }; viewModel = await this.newsService.CreateAsync(news, this.user.Id); } await this.cloudinaryService.DeleteImage(this.cloudinary, news.Title + Suffixes.NewsSuffix); var dbEntry = await this.newsRepository.All().FirstOrDefaultAsync(); Assert.Equal(dbEntry.Id, viewModel.Id); Assert.Equal(dbEntry.Title, viewModel.Title); Assert.Equal(dbEntry.Description, viewModel.Description); Assert.Equal(dbEntry.ShortDescription, viewModel.ShortDescription); Assert.Equal(dbEntry.ImagePath, viewModel.ImagePath); }
public IActionResult Create() { var viewModel = new NewsCreateInputModel(); return(this.View(viewModel)); }