public async Task CreatePostWithTagsTest() { await this.AddTwoCategorysAsync(); var fileName = "Img"; IFormFile file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, fileName, "dummy.png"); var postViewModel = new AddPostsModel() { File = file, Tittle = "Pesho", Category = "Funny", Tags = "#SoFunny Funny", }; await this.postsService.CreatePostAsync(postViewModel, "userId1"); var actual = await this.dbContext.Posts.FirstOrDefaultAsync(x => x.Tittle == "Pesho"); var expectedTittle = "Pesho"; var expectedCategory = "Funny"; var expectedUrl = fileName; var expectedTag = "SoFunny"; Assert.IsTrue(expectedTittle == actual.Tittle && expectedCategory == actual.Category.Name && expectedUrl == actual.ImgUrl && actual.PostTags.Any(x => x.Tag.Value == expectedTag)); }
public async Task <IActionResult> Add() { var model = new AddPostsModel() { Categorys = await this.categoryService.GetAllListItemsAsync(), }; return(this.View(model)); }
public async Task CreatePostAsync(AddPostsModel post, string userId) { var urlTest = this.cloudinary.SaveCloudinaryAsync(post.File); var url = urlTest.Result; if (url != null) { var newPost = new Post() { ApplicationUserId = userId, Tittle = post.Tittle, ImgUrl = url, CategoryId = await this.categoryService.GetIdAsync(post.Category), }; var tagIds = new HashSet <string>(); if (!string.IsNullOrWhiteSpace(post.Tags)) { var matches = Regex.Matches(post.Tags, GlobalConstants.TagValidationRegex); foreach (Match match in matches) { var tag = match.Groups[0].Value.TrimStart('#'); if (await this.tagService.ExistsAsync(tag)) { tagIds.Add(await this.tagService.GetIdAsync(tag)); } else { var tagId = await this.tagService.CreateAsync(tag); tagIds.Add(tagId); } } } var postTags = tagIds.Select(tag => new PostTag() { PostId = newPost.Id, TagId = tag, }).ToList(); await this.postRepository.AddAsync(newPost); await this.postTagRepository.AddRangeAsync(postTags); await this.postRepository.SaveChangesAsync(); await this.postTagRepository.SaveChangesAsync(); } }
public async Task TestCreatePostsWithSameTags() { await this.AddTwoCategorysAsync(); IFormFile file = new FormFile(new MemoryStream(Encoding.UTF8.GetBytes("This is a dummy file")), 0, 0, "Data", "dummy.png"); var postViewModel = new AddPostsModel() { File = file, Tittle = "Pesho", Category = "Funny", Tags = "#SoFunny Funny", }; await this.postsService.CreatePostAsync(postViewModel, "userId1"); await this.postsService.CreatePostAsync(postViewModel, "userId1"); Assert.IsTrue(this.dbContext.Tags.Count(x => x.Value == "SoFunny") == 1); }
public async Task <IActionResult> Add(AddPostsModel post) { if (post.File != null && post.Tittle != null) { var checkCategory = await this.categoryService.IsThereAnyAsync(post.Category); if (checkCategory && this.ModelState.IsValid) { var user = this.User.FindFirstValue(ClaimTypes.NameIdentifier); await this.postsService.CreatePostAsync(post, user); return(this.Redirect("/")); } } post.Categorys = await this.categoryService.GetAllListItemsAsync(); return(this.View(post)); }