public async Task <IActionResult> AddPost(AddPostInputModel inputModel) { if (ModelState.IsValid) { User user = await this.userManager.GetUserAsync(User); inputModel.User = user; Club club = await this.clubService.GetClubById(inputModel.ClubId); inputModel.Club = club; await this.postService.AddPost(inputModel); } return(this.Redirect($"/Club/Details/{inputModel.ClubId}")); }
public async Task CreateAsync(AddPostInputModel input) { var dbPost = new Post { Title = input.Title, Content = input.Content, UserId = input.UserId, CategoryId = input.CategoryId, }; await this.db.Posts.AddAsync(dbPost); await this.db.SaveChangesAsync(); }
public async Task <IActionResult> AddPost(AddPostInputModel input, int id) { if (!this.ModelState.IsValid) { return(this.View(input)); } var user = await this.userManager.GetUserAsync(this.User); input.CategoryId = id; input.UserId = user.Id; await this.postService.CreateAsync(input); this.TempData["Message"] = "Post added successfully"; return(this.Redirect($"/Categories/Category/{input.CategoryId}")); }
public async Task CreateMethodShouldAddCorrectNewPostToDb() { var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var dbContext = new ApplicationDbContext(optionsBuilder.Options); var postService = new PostService(dbContext); var postToAdd = new AddPostInputModel { CategoryId = 1, Content = "testContent", Title = "testTitle", UserId = "testUser", }; await postService.CreateAsync(postToAdd); Assert.NotNull(dbContext.Posts.FirstOrDefaultAsync()); Assert.Equal("testContent", dbContext.Posts.FirstAsync().Result.Content); Assert.Equal("testTitle", dbContext.Posts.FirstAsync().Result.Title); Assert.Equal(1, dbContext.Posts.FirstAsync().Result.CategoryId); }
public async Task <Post> AddPost(AddPostInputModel inputModel) { string secondaryContent = ""; PostType postType = PostType.Text; if (inputModel.FormFile != null) { secondaryContent = await this.cloudinaryService.UploadImage(inputModel.FormFile); postType = PostType.Image; } Post post = new Post() { Content = inputModel.Content, Author = inputModel.User, AuthorId = inputModel.User.Id, ClubId = inputModel.Club.Id, Club = inputModel.Club, DateTime = DateTime.Now, FileUrlOrLink = secondaryContent, PostType = postType }; await this.dbContext.Posts.AddAsync(post); await this.dbContext.SaveChangesAsync(); Club club = this.dbContext.Clubs .Include(x => x.ClubUsers) .FirstOrDefault(x => x.Id == post.ClubId); await this.notificationService.CreateNotificationForListOfUsers($"Има нова публикация в {club.Name}.", $"/Club/Details/{club.Id}", club.ClubUsers.ToList()); return(post); }