public async Task CreateComment_Throws_When_NoUserFound() { //Arrange var options = Utils.GetOptions(nameof(CreateComment_Throws_When_NoUserFound)); var cocktail = new Cocktail { Id = Guid.NewGuid(), Name = "Manhattan" }; var userId = Guid.NewGuid(); var cocktailCommentDTO = new CocktailCommentsDTO { CocktailId = cocktail.Id, UserId = userId }; using (var arrangeContext = new CMContext(options)) { await arrangeContext.Cocktails.AddRangeAsync(cocktail); await arrangeContext.SaveChangesAsync(); } //Act, Assert using (var assertContext = new CMContext(options)) { var sut = new CocktailCommentServices(assertContext); await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.CreateComment(cocktailCommentDTO)); } }
/// <summary> /// Creates a new instance of type CocktailComment /// </summary> /// <param name="comment">The comment that should be created</param> /// <returns>Created comment for the cocktail as data transfer object</returns> public async Task <CocktailCommentsDTO> CreateComment(CocktailCommentsDTO comment) { var user = await this.context.Users.FirstOrDefaultAsync(u => u.Id == comment.UserId); if (user == null) { throw new ArgumentNullException(Exceptions.NullEntityId); } var cocktail = await this.context.Cocktails.FirstOrDefaultAsync(c => c.Id == comment.CocktailId); if (String.IsNullOrWhiteSpace(comment.Comments)) { throw new ArgumentNullException(Exceptions.CommentRequired); } var newCocktailComment = comment.GetEntity(); this.context.Users.Update(user); this.context.Cocktails.Update(cocktail); await this.context.CocktailComments.AddAsync(newCocktailComment); await this.context.SaveChangesAsync(); return(newCocktailComment.GetDTO()); }
public static CocktailCommentViewModel GetViewModel(this CocktailCommentsDTO item) { if (item == null) { throw new ArgumentNullException(); } return(new CocktailCommentViewModel { CocktailId = item.CocktailId, UserId = item.UserId, Comment = item.Comments, Name = item.User.UserName }); }
public static CocktailComment GetEntity(this CocktailCommentsDTO item) { if (item == null) { throw new ArgumentNullException(); } return(new CocktailComment { Comments = item.Comments, User = item.User, UserId = item.UserId, Cocktail = item.Cocktail, CocktailId = item.CocktailId, }); }
public async Task CreateComment_Creates_Correct() { //Arrange var options = Utils.GetOptions(nameof(CreateComment_Creates_Correct)); var user = new User { Id = Guid.NewGuid(), UserName = "******" }; var cocktail = new Cocktail { Id = Guid.NewGuid(), Name = "Manhattan" }; var cocktailCommentDTO = new CocktailCommentsDTO { CocktailId = cocktail.Id, UserId = user.Id, Comments = "Great" }; using (var arrangeContext = new CMContext(options)) { await arrangeContext.Users.AddRangeAsync(user); await arrangeContext.Cocktails.AddRangeAsync(cocktail); await arrangeContext.SaveChangesAsync(); } //Act, Assert using (var assertContext = new CMContext(options)) { var sut = new CocktailCommentServices(assertContext); var result = await sut.CreateComment(cocktailCommentDTO); Assert.AreEqual(cocktail.Id, result.CocktailId); Assert.AreEqual(user.Id, result.UserId); Assert.AreEqual(1, assertContext.CocktailComments.Count()); Assert.IsInstanceOfType(result, typeof(CocktailCommentsDTO)); } }