public IActionResult Create(int subjectId) { var topicModel = new CreateTopicInputModel(); topicModel.SubjectId = subjectId; return(this.View("Create", topicModel)); }
public async Task GetTopicTotalDisikes_WithCorrectTopicId_ShouldReturnCorrectTotalLikes() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var topicRepository = new EfDeletableEntityRepository <Topic>(context); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context); var topicsService = new TopicsService(topicRepository, userRepository); var inputModel = new CreateTopicInputModel() { Title = "TestTitle", Content = "TestContent_TestContent_TestContent_TestContent_TestContent_TestContent", }; await topicsService.CreateAsync(inputModel); var topic = topicRepository.All().FirstOrDefault(t => t.Title == "TestTitle"); topic.Dislikes = 15; topicRepository.Update(topic); await topicRepository.SaveChangesAsync(); // Act var expectedTotalTopicDislikes = 15; var actualTotalTopicDislikes = topicsService.GetTopicTotalDislikes(topic.Id); // Assert Assert.Equal(expectedTotalTopicDislikes, actualTotalTopicDislikes); }
public async Task DeleteByIdAsync_WithCorrectData_ShouldSuccessfullyDelete() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var topicRepository = new EfDeletableEntityRepository <Topic>(context); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context); var topicsService = new TopicsService(topicRepository, userRepository); var inputModel = new CreateTopicInputModel() { Title = "TestTitle", Content = "TestContent_TestContent_TestContent_TestContent_TestContent_TestContent", }; await topicsService.CreateAsync(inputModel); var topic = topicRepository.All().FirstOrDefault(t => t.Title == "TestTitle"); // Act var expectedTopicsCount = 0; await topicsService.DeleteByIdAsync(topic.Id); // Assert Assert.Equal(expectedTopicsCount, topicRepository.All().Count()); }
public async Task CancelVoteAsync_WithIsLikeFalse_ShouldSuccessfullyDecreaseTopicDislikesWithOne() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var topicRepository = new EfDeletableEntityRepository <Topic>(context); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context); var topicsService = new TopicsService(topicRepository, userRepository); var inputModel = new CreateTopicInputModel() { Title = "TestTitle", Content = "TestContent_TestContent_TestContent_TestContent_TestContent_TestContent", }; await topicsService.CreateAsync(inputModel); var topic = topicRepository.All().FirstOrDefault(t => t.Title == "TestTitle"); await topicsService.VoteTopicAsync(topic.Id, false); // Act var expectedTopicDislikes = 0; await topicsService.CancelVoteAsync(topic.Id, false); var actualTopicDislikes = topicRepository.All().FirstOrDefault(t => t.Title == "TestTitle").Dislikes; // Assert Assert.Equal(expectedTopicDislikes, actualTopicDislikes); }
public async Task IncreaseViewsAsync_ShouldIncreaseTopicViews() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var topicRepository = new EfDeletableEntityRepository <Topic>(context); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context); var topicsService = new TopicsService(topicRepository, userRepository); var inputModel = new CreateTopicInputModel() { Title = "TestTitle", Content = "TestContent_TestContent_TestContent_TestContent_TestContent_TestContent", }; await topicsService.CreateAsync(inputModel); var topicId = topicRepository.All().FirstOrDefault(t => t.Title == "TestTitle").Id; // Act var expectedViews = 1; await topicsService.IncreaseViewsAsync(topicId); var actualViews = topicRepository.All().FirstOrDefault(t => t.Title == "TestTitle").Views; // Assert Assert.Equal(expectedViews, actualViews); }
public IActionResult Create(CreateTopicInputModel model) { if (ModelState.IsValid) { this.topicService.CreateAsync(model); } return(RedirectToAction("Details", new RouteValueDictionary(new { controller = "Subjects", action = "Details", Id = model.SubjectId }))); }
public async Task CreateAsync(CreateTopicInputModel input) { var topic = new Topic { Title = input.Title, Content = input.Content, PictureUrl = input.PictureUrl, AuthorId = input.AuthorId, CategoryId = input.CategoryId, }; await this.topicRepository.AddAsync(topic); await this.topicRepository.SaveChangesAsync(); }
public void CreateAsync(CreateTopicInputModel input) { // Async methods don't save object to db var topic = new Topic() { Title = input.Title, Description = input.Description, SubjectId = input.SubjectId, Requirements = input.Requirements, }; this.context.Topics.Add(topic); this.context.SaveChanges(); }
public async Task <IActionResult> Create() { var categories = await this.categoriesService.GetAllAsync <CategoryInfoViewModel>(); if (this.User.IsInRole(GlobalConstants.BannedRoleName)) { return(this.View("Banned")); } var viewModel = new CreateTopicInputModel() { Categories = categories, }; return(this.View(viewModel)); }
public async Task <IActionResult> Create(CreateTopicInputModel input) { if (this.User.IsInRole(GlobalConstants.BannedRoleName)) { return(this.RedirectToAction(nameof(this.Create))); } if (!this.ModelState.IsValid) { input.Categories = await this.categoriesService.GetAllAsync <CategoryInfoViewModel>(); return(this.View(input)); } var userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); var remainingMinutesToCreateTopic = this.topicsService.GetRemainingMinutesToCreateTopic(userId); if (remainingMinutesToCreateTopic != GlobalConstants.MinutesAllowingTopicCreation) { this.TempData["Danger"] = $"You cannot create a new topic before {GlobalConstants.MinutesBetweenTwoTopicsCreations} minutes have elapsed after the last topic creation! Remaining time: {remainingMinutesToCreateTopic} minutes."; input.Categories = await this.categoriesService.GetAllAsync <CategoryInfoViewModel>(); return(this.View(input)); } input.AuthorId = userId; input.CategoryId = this.categoriesService.GetIdByName(input.CategoryName); if (input.Picture != null) { var pictureUrl = await this.cloudinaryService.UploadPhotoAsync( input.Picture, $"{userId}-{input.Title}"); input.PictureUrl = pictureUrl; } await this.topicsService.CreateAsync(input); var newTopicId = this.topicsService.GetIdByTitle(input.Title); return(this.Redirect($"/Topics/Details?topicId={newTopicId}")); }
public async Task CreateAsync_ShouldSuccessfullyCreate() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var topicRepository = new EfDeletableEntityRepository <Topic>(context); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context); var topicsService = new TopicsService(topicRepository, userRepository); var inputModel = new CreateTopicInputModel() { Title = "TestTitle", Content = "TestContent_TestContent_TestContent_TestContent_TestContent_TestContent", }; // Act var expectedTopicsCount = 1; await topicsService.CreateAsync(inputModel); // Assert Assert.Equal(expectedTopicsCount, topicRepository.All().Count()); }
public async Task GetIdByTitle_WithCorrectData_ShouldReturnCorrectResult() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var topicRepository = new EfDeletableEntityRepository <Topic>(context); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context); var topicsService = new TopicsService(topicRepository, userRepository); var inputModel = new CreateTopicInputModel() { Title = "TestTitle", Content = "TestContent_TestContent_TestContent_TestContent_TestContent_TestContent", }; await topicsService.CreateAsync(inputModel); // Act var expectedId = topicRepository.All().FirstOrDefault(t => t.Title == "TestTitle").Id; var actualId = topicsService.GetIdByTitle(inputModel.Title); // Assert Assert.Equal(expectedId, actualId); }
public async Task EditAsync_WithCorrectData_ShouldSuccessfullyEdit() { // Arrange var context = ApplicationDbContextInMemoryFactory.InitializeContext(); var topicRepository = new EfDeletableEntityRepository <Topic>(context); var userRepository = new EfDeletableEntityRepository <ApplicationUser>(context); var topicsService = new TopicsService(topicRepository, userRepository); var createInputModel = new CreateTopicInputModel() { Title = "TestTitle", Content = "TestContent_TestContent_TestContent_TestContent_TestContent_TestContent", }; await topicsService.CreateAsync(createInputModel); var topic = topicRepository.All().FirstOrDefault(t => t.Title == "TestTitle"); var editInputModel = new TopicEditViewModel() { Id = topic.Id, Title = "Edited_TestTitle", Content = "Edited_TestContent_TestContent_TestContent_TestContent_TestContent_TestContent", }; // Act var expectedTopicTitle = "Edited_TestTitle"; var expectedTopicContent = "Edited_TestContent_TestContent_TestContent_TestContent_TestContent_TestContent"; await topicsService.EditAsync(editInputModel); // Assert topic = await topicRepository.GetByIdWithDeletedAsync(editInputModel.Id); Assert.Equal(expectedTopicTitle, topic.Title); Assert.Equal(expectedTopicContent, topic.Content); }