public void MapDTOToComment_ShouldReturnCountry()
        {
            //Arrange
            var cut = new CommentDTO();
            //Act
            var sut = cut.MapDTOToComment();

            //Assert
            Assert.IsInstanceOfType(sut, typeof(Comment));
        }
Beispiel #2
0
        /// <summary>
        /// Creates a comment and writes it to the database.
        /// </summary>
        /// <param name="model">Input CommentDTO object</param>
        /// <returns>Returns the reevaluated input object</returns>
        public async Task <CommentDTO> CreateAsync(CommentDTO model)
        {
            var comment = model.MapDTOToComment();

            comment.Review = await _context.Reviews.FindAsync(model.ReviewID);

            comment.User = await _context.Users.FindAsync(model.UserID);

            comment.Beer = await _context.Beers.FindAsync(comment.Review.BeerID);

            if (comment.Description == null)
            {
                return(null);
            }
            #region Check if exists
            var theComment = await this._context.Comments
                             .FirstOrDefaultAsync(c => c.ReviewID == comment.ReviewID && c.UserID == comment.UserID);

            if (theComment == null)
            {
                comment.CreatedOn = DateTime.UtcNow;
                await this._context.Comments.AddAsync(comment);

                await this._context.SaveChangesAsync();
            }
            else
            {
                theComment.IsDeleted  = false;
                theComment.DeletedOn  = null;
                theComment.ModifiedOn = DateTime.UtcNow;
                _context.Comments.Update(theComment);
                await this._context.SaveChangesAsync();
            }
            #endregion
            var returnModel = await this._context.Comments
                              .FirstOrDefaultAsync(c => c.ReviewID == model.ReviewID && c.UserID == model.UserID);

            model.ID = returnModel.ID;
            return(model);
        }
        public void MapDTOToComment_ShouldReturnCorrectDescription()
        {
            //Arrange
            var cut = new CommentDTO
            {
                ID          = 1,
                Description = "Gotham",
                User        = new UserDTO()
                {
                    Name = "Batman"
                },
                Review = new ReviewDTO()
                {
                    Description = "Description"
                }
            };
            //Act
            var sut = cut.MapDTOToComment();

            //Assert
            Assert.AreEqual(sut.Description, "Gotham");
        }