Ejemplo n.º 1
0
        /// <summary>
        /// Creates a review and writes it to the database.
        /// </summary>
        /// <param name="model">Input ReviewDTO object</param>
        /// <returns>Returns the reevaluated input object</returns>
        public async Task <ReviewDTO> CreateAsync(ReviewDTO model)
        {
            var review = model.MapDTOToReview();

            review.Beer = await _context.Beers.FindAsync(model.BeerID);

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

            if (review.Beer == null || review.User == null)
            {
                return(null);
            }
            #region Check if exists
            var theReview = await _context.Reviews
                            .Include(r => r.Beer)
                            .Include(r => r.User)
                            .FirstOrDefaultAsync(r => r.BeerID == review.BeerID || r.UserID == review.UserID);

            if (theReview == null)
            {
                review.CreatedOn = DateTime.UtcNow;
                this._context.Reviews.Add(review);
                await this._context.SaveChangesAsync();
            }
            else if (theReview.User.IsDeleted == false && theReview.Beer.IsDeleted == false)
            {
                theReview.IsDeleted  = false;
                theReview.DeletedOn  = null;
                theReview.ModifiedOn = DateTime.UtcNow;
                _context.Reviews.Update(theReview);
                var commentsOfReview = await _context.Comments
                                       .Include(c => c.User)
                                       .Include(c => c.Review)
                                       .ToListAsync();

                foreach (var item in commentsOfReview)
                {
                    await new CommentService(_context).CreateAsync(item.MapCommentToDTO());
                }
                await this._context.SaveChangesAsync();
            }
            #endregion
            return(review.MapReviewToDTO());
        }
Ejemplo n.º 2
0
        public void MapDTOToReview_ShouldReturnBrewery()
        {
            //Arrange
            var cut = new ReviewDTO
            {
                ID          = 1,
                Description = "Great",
                Beer        = new BeerDTO()
                {
                    Name = "Carlsberg"
                },
                User = new UserDTO()
                {
                    Name = "SuperMan"
                }
            };
            //Act
            var sut = cut.MapDTOToReview();

            //Assert
            Assert.IsInstanceOfType(sut, typeof(Review));
        }
Ejemplo n.º 3
0
        public void MapDTOToReview_ShouldReturnCorrectID()
        {
            //Arrange
            var cut = new ReviewDTO
            {
                ID          = 1,
                Description = "Great",
                Beer        = new BeerDTO()
                {
                    Name = "Carlsberg"
                },
                User = new UserDTO()
                {
                    Name = "SuperMan"
                }
            };
            //Act
            var sut = cut.MapDTOToReview();

            //Assert
            Assert.AreEqual(1, sut.ID);
        }
Ejemplo n.º 4
0
        public void MapDTOToReview_ShouldReturnCorrectNameAndCountry()
        {
            //Arrange
            var cut = new ReviewDTO
            {
                ID          = 1,
                Description = "Great",
                Beer        = new BeerDTO()
                {
                    Name = "Carlsberg"
                },
                User = new UserDTO()
                {
                    Name = "SuperMan"
                }
            };
            //Act
            var sut = cut.MapDTOToReview();

            //Assert
            Assert.AreEqual(sut.Beer.Name, "Carlsberg");
            Assert.AreEqual(sut.User.Name, "SuperMan");
            Assert.AreEqual(sut.Description, "Great");
        }