/// <summary> /// User comments a cocktail /// </summary> /// <param name="commentDTO">Comment DTO </param> /// <param name="cocktailId">Cocktail Id that is commented</param> /// <param name="userId">User Id that leaves the comment</param> /// <returns>The comment, DTO</returns> public async Task <CocktailUserCommentDTO> AddCocktailComment(CocktailUserCommentDTO commentDTO, Guid cocktailId, Guid userId) { var cocktail = await this._context.Cocktails .FirstOrDefaultAsync(x => x.Id == cocktailId); commentDTO.UserId = userId; var comment = this._cocktailCommentMapper.MapDTOToEntity(commentDTO); await this._context.CocktailComments.AddAsync(comment); await this._context.SaveChangesAsync(); return(this._cocktailCommentMapper.MapEntityToDTO(comment)); }
public CocktailUserCommentDTO MapViewToDTO(CocktailUserCommentVM view) { var dto = new CocktailUserCommentDTO { CocktailId = view.CocktailId, CocktailName = view.CocktailName, UserId = view.UserId, UserName = view.UserName, Text = view.Text, IsFlagged = view.IsFlagged, }; return(dto); }
public CocktailUserCommentVM MapDTOToView(CocktailUserCommentDTO dto) { var view = new CocktailUserCommentVM { CocktailId = dto.CocktailId, CocktailName = dto.CocktailName, UserId = dto.UserId, UserName = dto.UserName, Text = dto.Text, IsFlagged = dto.IsFlagged, }; return(view); }
public CocktailUserComment MapDTOToEntity(CocktailUserCommentDTO dto) { try { return(new CocktailUserComment { CocktailId = dto.CocktailId, //Cocktail = dto.CocktailName, UserId = dto.UserId, //User = dto.UserName, Text = dto.Text, IsFlagged = dto.IsFlagged, }); } catch (Exception) { return(new CocktailUserComment()); } }
public async Task <CocktailUserCommentDTO> UpdateAsync(CocktailUserCommentDTO commentDTO) { try { var comment = await this._context.CocktailComments .Include(c => c.Cocktail) .Include(c => c.User) .FirstOrDefaultAsync(c => c.CocktailId == commentDTO.CocktailId && c.UserId == commentDTO.UserId); comment.Text = commentDTO.Text; comment.IsFlagged = commentDTO.IsFlagged; _context.Update(comment); await _context.SaveChangesAsync(); return(commentDTO); } catch (Exception) { return(new CocktailUserCommentDTO()); } }
public async Task <CocktailUserCommentDTO> CreateAsync(CocktailUserCommentDTO commentDTO) { try { if (await CombinationExistAsync(commentDTO.CocktailId, commentDTO.UserId)) { return(new CocktailUserCommentDTO()); } else { var comment = this._mapper.MapDTOToEntity(commentDTO); this._context.CocktailComments.Add(comment); await _context.SaveChangesAsync(); return(commentDTO); } } catch (Exception) { return(new CocktailUserCommentDTO()); } }
public async Task DeleteCocktailComment_Success() { var testCocktailName = "TestCocktailName"; var testUserName = "******"; var options = Utils.GetOptions(nameof(DeleteCocktailComment_Success)); var testUser = new User() { Id = Utils.MySampleGuid(), UserName = testUserName, }; var testCocktail = new Cocktail() { Id = Utils.MySampleGuid3(), Name = testCocktailName, }; var testComment = new CocktailUserComment() { UserId = testUser.Id, User = testUser, CocktailId = testCocktail.Id, Cocktail = testCocktail, Text = "Comment text here.", }; var dto = new CocktailUserCommentDTO() { UserId = testUser.Id, CocktailId = testCocktail.Id, Text = "Comment text here.", }; var mockMapper = new Mock <ICocktailCommentMapper>(); mockMapper.Setup(x => x.MapDTOToEntity(It.IsAny <CocktailUserCommentDTO>())) .Returns((CocktailUserCommentDTO x) => new CocktailUserComment() { CocktailId = x.CocktailId, UserId = x.UserId, Text = x.Text }); mockMapper.Setup(x => x.MapEntityToDTO(It.IsAny <CocktailUserComment>())) .Returns((CocktailUserComment x) => new CocktailUserCommentDTO() { CocktailId = x.CocktailId, CocktailName = x.Cocktail.Name, UserId = x.UserId, UserName = x.User.UserName, Text = x.Text }); using (var arrangeContext = new BCcontext(options)) { await arrangeContext.Users.AddAsync(testUser); await arrangeContext.Cocktails.AddAsync(testCocktail); await arrangeContext.CocktailComments.AddAsync(testComment); await arrangeContext.SaveChangesAsync(); } //Act & Assert using (var context = new BCcontext(options)) { var sut = new CocktailCommentsService(context, mockMapper.Object); Assert.IsNotNull(context.Cocktails .Include(x => x.Comments) .FirstOrDefault(x => x.Name == testCocktailName) .Comments.FirstOrDefault()); Assert.AreEqual(1, context.CocktailComments.Count()); //Act var result = await sut.DeleteAsync(testCocktail.Id, testUser.Id); //Assert Assert.IsTrue(result); Assert.IsNotNull(context.Cocktails.FirstOrDefault()); Assert.IsNotNull(context.Users.FirstOrDefault()); } }
public async Task CreateCocktailComment_whenValid() { var testCocktailName = "TestCocktailName"; var testUserName = "******"; var options = Utils.GetOptions(nameof(CreateCocktailComment_whenValid)); var testUser = new User() { Id = Utils.MySampleGuid(), UserName = testUserName, }; var testCocktail = new Cocktail() { Id = Utils.MySampleGuid3(), Name = testCocktailName, }; var dto = new CocktailUserCommentDTO() { UserId = testUser.Id, CocktailId = testCocktail.Id, Text = "Comment text here.", }; var mockMapper = new Mock <ICocktailCommentMapper>(); mockMapper.Setup(x => x.MapDTOToEntity(It.IsAny <CocktailUserCommentDTO>())) .Returns((CocktailUserCommentDTO x) => new CocktailUserComment() { CocktailId = x.CocktailId, UserId = x.UserId, Text = x.Text }); mockMapper.Setup(x => x.MapEntityToDTO(It.IsAny <CocktailUserComment>())) .Returns((CocktailUserComment x) => new CocktailUserCommentDTO() { CocktailId = x.CocktailId, CocktailName = x.Cocktail.Name, UserId = x.UserId, UserName = x.User.UserName, Text = x.Text }); using (var arrangeContext = new BCcontext(options)) { await arrangeContext.Users.AddAsync(testUser); await arrangeContext.Cocktails .AddAsync(testCocktail); await arrangeContext.SaveChangesAsync(); } using (var actContext = new BCcontext(options)) { var sut = new CocktailCommentsService(actContext, mockMapper.Object); var comment = sut.CreateAsync(dto); await actContext.SaveChangesAsync(); } using (var assertContext = new BCcontext(options)) { Assert.AreEqual(1, assertContext.CocktailComments.Count()); var cocktail = await assertContext.Cocktails .Include(x => x.Comments) .FirstOrDefaultAsync(x => x.Name == testCocktailName); var comment = cocktail.Comments.FirstOrDefault(); Assert.IsNotNull(comment); Assert.AreEqual(dto.Text, comment.Text); } }