public void CreateSuggestion_Success_Test() { var suggestRepository = new Mock <ISuggestRepository>(); var suggestService = new Mock <ISuggestService>(); var createSuggestionReturn = new CreateSuggestionReturnModel { CreatedSuggestion = new Suggestion(), IsSuccessful = true }; var createSuggestionInputModel = new CreateSuggestionInputModel { Name = "Persona Name", Content = "Test content", Email = "*****@*****.**" }; suggestService.Setup(x => x.CreateSuggestion(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(createSuggestionReturn); var result = new SuggestController(suggestRepository.Object, suggestService.Object).CreateSuggestion(createSuggestionInputModel); Assert.IsType <OkObjectResult>(result); }
public void CreateSuggestion_InvalidParameter_BadRequest_Test() { var suggestRepository = new Mock <ISuggestRepository>(); var suggestService = new Mock <ISuggestService>(); var createSuggestionReturn = new CreateSuggestionReturnModel { CreatedSuggestion = new Suggestion(), ErrorType = Suggest.Services.Enums.ReturnErrorType.InvalidParameters, IsSuccessful = false }; var createSuggestionInputModel = new CreateSuggestionInputModel { Name = null, Content = "Test content", Email = "*****@*****.**" }; suggestService.Setup(x => x.CreateSuggestion(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>())) .Returns(createSuggestionReturn); var result = new SuggestController(suggestRepository.Object, suggestService.Object).CreateSuggestion(createSuggestionInputModel); Assert.IsType <BadRequestObjectResult>(result); }
public IActionResult CreateSuggestion([FromBody] CreateSuggestionInputModel createSuggestionModel) { var createdSuggestionReturnModel = _suggestServices.CreateSuggestion(createSuggestionModel.Name, createSuggestionModel.Email, createSuggestionModel.Content); if (createdSuggestionReturnModel.IsSuccessful && createdSuggestionReturnModel.CreatedSuggestion != null) { return(Ok(createdSuggestionReturnModel.CreatedSuggestion)); } return(BadRequest("Invalid parameters")); }
public async Task <IActionResult> Create(CreateSuggestionInputModel model, IFormFile file) { var user = await this.userManager.GetUserAsync(this.User); var userId = await this.userManager.GetUserIdAsync(user); model.UserId = userId; if (!this.ModelState.IsValid || model.UserId == null || file == null) { return(this.View(model)); } var doesItExist = await this.suggestionsService.DoesSuggestionExist(model.Title); if (doesItExist) { this.TempData["SuggestionExist"] = "This game is already suggested! :)"; return(this.View(model)); } var doesGameExist = await this.gamesService.DoesGameExist(model.Title); if (doesGameExist) { this.TempData["GameExist"] = "This game is already added!"; return(this.View(model)); } var imageUrl = await this.cloudinaryHelper.UploadSuggestionImageAsync(this.cloudinary, file); if (imageUrl == null) { this.TempData["SuggestionExist"] = "You are trying to upload a non-image file."; return(this.View(model)); } model.ImageUrl = imageUrl; var suggestionId = await this.suggestionsService.CreateSuggestionAsync(model); var result = await this.votesService.VoteOnSuggestionGameAsync(suggestionId, userId); return(this.RedirectToAction("All")); }
public async Task DoesSuggestionExistShouldReturnTrue() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var context = new ApplicationDbContext(options.Options); var suggestionGameRepository = new EfDeletableEntityRepository <SuggestionGame>(context); var service = new SuggestionsService(suggestionGameRepository); var model = new CreateSuggestionInputModel() { Description = "SomeDescSomeDescSomeDescSomeDescSomeDescSomeDescSomeDescSomeDescSomeDescSomeDesc", ImageUrl = "google.com", Title = "New SuggestedGame", UserId = "1", }; await service.CreateSuggestionAsync(model); var actual = await service.DoesSuggestionExist(model.Title); Assert.True(actual); }
public async Task GetByIdShouldReturnModel() { var options = new DbContextOptionsBuilder <ApplicationDbContext>() .UseInMemoryDatabase(Guid.NewGuid().ToString()); var context = new ApplicationDbContext(options.Options); var suggestionGameRepository = new EfDeletableEntityRepository <SuggestionGame>(context); var service = new SuggestionsService(suggestionGameRepository); var model = new CreateSuggestionInputModel() { Description = "SomeDescSomeDescSomeDescSomeDescSomeDescSomeDescSomeDescSomeDescSomeDescSomeDesc", ImageUrl = "google.com", Title = "TestingMethod", UserId = "1", }; var id = await service.CreateSuggestionAsync(model); var actual = await service.GetById <SuggestionViewModel>(id); Assert.IsType <SuggestionViewModel>(actual); }