public async Task <IActionResult> CreateStory(StoryInputModel inputModel) { bool imageNotNull = inputModel.StoryImage != null; bool wrongType = false; if (imageNotNull) { var fileType = inputModel.StoryImage.ContentType.Split('/')[1]; wrongType = GlobalConstants.imageFormat.Contains(fileType); } if (!ModelState.IsValid) { return(this.View(inputModel)); } if (!wrongType && imageNotNull) { this.ViewData[GlobalConstants.Error] = GlobalConstants.WrongFileType; return(this.View(inputModel)); } var id = await this.StoryService.CreateStory(inputModel); return(RedirectToAction("Details", "Stories", new { id })); }
public void CreateStory_Should_Work_Correct_With_Null_Iform_File() { //arrange var author = new FanFictionUser { Id = "AuthorId", Nickname = "StoryAuthor", UserName = "******", }; var genre = new StoryType { Id = 1, Name = "fantasy" }; var usermanager = this.Provider.GetRequiredService <UserManager <FanFictionUser> >(); usermanager.CreateAsync(author).GetAwaiter(); this.Context.StoryTypes.Add(genre); this.Context.SaveChanges(); var newStory = new StoryInputModel { Author = author.UserName, StoryImage = null, CreatedOn = DateTime.Now, Genre = "fantasy", Summary = "someSummary", Title = "NewStoryTitle", }; //act var storyService = GetService(); int result = storyService.CreateStory(newStory).GetAwaiter().GetResult(); var story = this.Context.FictionStories.First(); //assert result.Should().BePositive().And.Subject.Should().Be(1); story.Should().NotBeNull().And.Subject.Should().BeEquivalentTo(new { Id = 1, ImageUrl = GlobalConstants.DefaultNoImage, newStory.Title, Type = new StoryTypeOutputModel { Id = 1, Type = newStory.Genre } }, options => options.ExcludingMissingMembers()); }
public async Task <IActionResult> PutAsync(int id, [FromBody] StoryInputModel value) { if (value == null) { return(BadRequest("Invalid input")); } var result = await _storiesService.UpdateAsync(id, value); if (result == null) { return(BadRequest($"Update of story {id} failed")); } return(Ok(result)); }
public async Task <int> CreateStory(StoryInputModel inputModel) { var accloudinary = SetCloudinary(); var url = await UploadImage(accloudinary, inputModel.StoryImage, inputModel.Title); var newStory = Mapper.Map <FanFictionStory>(inputModel); newStory.Author = await this.UserManager.FindByNameAsync(inputModel.Author); newStory.Type = this.Context.StoryTypes.First(x => x.Name == inputModel.Genre); newStory.ImageUrl = url ?? GlobalConstants.DefaultNoImage; this.Context.FictionStories.Add(newStory); await this.Context.SaveChangesAsync(); return(newStory.Id); }
public void CreateStory_Should_Return_InvalidModel() { var story = new StoryInputModel { Author = "some", CreatedOn = DateTime.Now, Genre = "someGenre", StoryImage = null, Summary = null, Title = null }; var controller = new StoriesController(storyService.Object); controller.ModelState.AddModelError("Title", "StringLength"); var result = controller.CreateStory(story).GetAwaiter().GetResult(); result.Should().BeOfType <ViewResult>().Which.Model.Should().BeOfType <StoryInputModel>(); }
public async Task <IActionResult> PostAsync([FromBody] StoryInputModel value) { if (value == null) { return(BadRequest("Invalid input")); } var result = await _storiesService.AddAsync(value); if (result == null) { return(BadRequest("Story not inserted")); } return(CreatedAtRoute("StoryGetAsync", new { id = result.Id }, result)); }