public async Task AddStory_ShouldThrow_IfDestinationNotExist() { var destRepoBuilder = new DestinationsRepositoryBuilder(); var destRepo = destRepoBuilder .WithAll() .Build(); var storiesRepoBuilder = new StoriesRepositoryBuilder(); var storiesRepo = storiesRepoBuilder .WithAll() .Build(); var sut = new StoriesService(storiesRepo, destRepo, null, null, Mapper); var createStoryViewModel = new CreateStoryViewModel { Content = "Niki otiva na ski", Title = "Niki", DestinationId = "11", }; var user = new GoUser { Id = "7" }; var ex = await Assert.ThrowsAsync <ArgumentException>(async() => await sut.AddStory(createStoryViewModel, "11", user)); Assert.Equal("Destination not exist!", ex.Message); storiesRepoBuilder.StoriesRepoMock.Verify(r => r.AddAsync(It.IsAny <Story>()), Times.Never); storiesRepoBuilder.StoriesRepoMock.Verify(r => r.SaveChangesAsync(), Times.Never); }
public async Task AddStory_ShouldAddNewStoryCorrectly() { var destRepoBuilder = new DestinationsRepositoryBuilder(); var destRepo = destRepoBuilder .WithAll() .Build(); var storiesRepoBuilder = new StoriesRepositoryBuilder(); var storiesRepo = storiesRepoBuilder .WithAll() .Build(); var sut = new StoriesService(storiesRepo, destRepo, null, null, Mapper); var createStoryViewModel = new CreateStoryViewModel { Content = "Niki otiva na ski", Title = "Niki", DestinationId = "2", }; var user = new GoUser { Id = "7" }; await sut.AddStory(createStoryViewModel, "2", user); storiesRepoBuilder.StoriesRepoMock.Verify(r => r.AddAsync(It.IsAny <Story>()), Times.Once); storiesRepoBuilder.StoriesRepoMock.Verify(r => r.SaveChangesAsync(), Times.Once); }
public JsonResult Post([FromBody] CreateStoryViewModel viewModel) { var story = Mapper.Map <CreateStoryViewModel, Story>(viewModel); story.CreationTime = DateTime.Now; story = Context.Stories.Add(story).Entity; Context.SaveChanges(); return(Json(story)); }
public async Task <IActionResult> Create(CreateStoryViewModel model, string id) { if (!ModelState.IsValid) { return(this.View()); } var user = await userManager.GetUserAsync(HttpContext.User); model.DestinationId = id; await this.storiesService.AddStory(model, id, user); return(Redirect($"/Destinations/Details/{id}")); }
public async Task AddStory(CreateStoryViewModel model, string id, GoUser user) //id(destinationId) { var destination = this.destinationsRepository.All().FirstOrDefault(x => x.Id == id); if (destination == null) { throw new ArgumentException(DestinationNotExist); } model.AuthorId = user.Id; var story = mapper.Map <Story>(model); story.Author = user; await this.storiesRepository.AddAsync(story); await this.storiesRepository.SaveChangesAsync(); }
public IHttpActionResult PostStory([FromBody] CreateStoryViewModel data) { using (var db = new DataContext()) { var ViewStories = db.Stories.Include(i => i.User).Include(i => i.Category); var thisAuthor = db.Users.SingleOrDefault(s => s.UserName == data.UserName); var thisCategory = db.Categories.SingleOrDefault(s => s.Name == data.Category); if (thisCategory == null) { thisCategory = new Category { Name = data.Category }; db.Categories.Add(thisCategory); db.SaveChanges(); } var newStory = new Story { Headline = data.Headline, BodyContent = data.BodyContent, Timestamp = DateTime.Now, UserID = thisAuthor?.ID, Category = thisCategory }; db.Stories.Add(newStory); db.SaveChanges(); return(Ok(newStory)); } }