コード例 #1
0
        public void MapDTOToBeer_ShouldReturnCorrectID()
        {
            //Arrange
            var cut = new BeerDTO
            {
                ID      = 1,
                ABV     = 4.5f,
                Rating  = 2,
                Name    = "Carlsberg",
                Country = new CountryDTO()
                {
                    Name = "Bulgaria"
                },
                Brewery = new BreweryDTO()
                {
                    Name = "Brewery", Country = "Germany"
                },
                Style = new BeerStyleDTO()
                {
                    Name = "Ale"
                }
            };
            //Act
            var sut = cut.MapDTOToBeer();

            //Assert
            Assert.AreEqual(sut.ID, 1);
        }
コード例 #2
0
        public void MapDTOToBeer_ShouldReturnBrewery()
        {
            //Arrange
            var cut = new BeerDTO
            {
                ID      = 1,
                ABV     = 4.5f,
                Rating  = 2,
                Name    = "Carlsberg",
                Country = new CountryDTO()
                {
                    Name = "Bulgaria"
                },
                Brewery = new BreweryDTO()
                {
                    Name = "Brewery", Country = "Germany"
                },
                Style = new BeerStyleDTO()
                {
                    Name = "Ale"
                }
            };
            //Act
            var sut = cut.MapDTOToBeer();

            //Assert
            Assert.IsInstanceOfType(sut, typeof(Beer));
        }
コード例 #3
0
        public void MapDTOToBeer_ShouldReturnCorrectNameAndCountryAndBreweryAndStyle()
        {
            //Arrange
            var cut = new BeerDTO
            {
                ABV     = 4.5f,
                Rating  = 2,
                Name    = "Carlsberg",
                Country = new CountryDTO()
                {
                    Name = "Bulgaria"
                },
                Brewery = new BreweryDTO()
                {
                    Name = "Brewery", Country = "Germany"
                },
                Style = new BeerStyleDTO()
                {
                    Name = "Ale"
                }
            };
            //Act
            var sut = cut.MapDTOToBeer();

            //Assert
            Assert.AreEqual(sut.Name, "Carlsberg");
            Assert.AreEqual(sut.Country.Name, "Bulgaria");
            Assert.AreEqual(sut.Brewery.Name, "Brewery");
            Assert.AreEqual(sut.Brewery.Country.Name, "Germany");
            Assert.AreEqual(sut.Style.Name, "Ale");
            Assert.AreEqual(sut.ABV, 4.5f);
            Assert.AreEqual(sut.Rating, 2);
        }
コード例 #4
0
        /// <summary>
        /// Creates a beer and writes it to the database.
        /// </summary>
        /// <param name="model">Input BeerDTO object</param>
        /// <returns>Returns the reevaluated input object</returns>
        public async Task <BeerDTO> CreateAsync(BeerDTO model)
        {
            var beer = model.MapDTOToBeer();

            if (beer.Name == null)
            {
                return(null);
            }
            #region Check if exists
            var theBeer = await this._context.Beers
                          .FirstOrDefaultAsync(b => b.Name == model.Name);

            if (theBeer == null)
            {
                beer.CreatedOn = DateTime.UtcNow;
                await this._context.Beers.AddAsync(beer);

                await this._context.SaveChangesAsync();
            }
            else
            {
                theBeer.IsDeleted  = false;
                theBeer.DeletedOn  = null;
                theBeer.ModifiedOn = DateTime.UtcNow;
                _context.Beers.Update(theBeer);
                var reviewsOfBeer = await _context.Reviews
                                    .Include(r => r.Beer)
                                    .Include(r => r.User)
                                    .Include(r => r.Comments)
                                    .Where(r => r.BeerID == theBeer.ID).ToListAsync();

                foreach (var item in reviewsOfBeer)
                {
                    await new ReviewsService(_context).CreateAsync(item.MapReviewToDTO());
                }
                var wishListOfBeer = await _context.WishLists
                                     .Where(w => w.BeerID == theBeer.ID)
                                     .ToListAsync();

                foreach (var item in wishListOfBeer)
                {
                    _context.WishLists.Remove(item);
                }
                var drinkListOfBeer = await _context.DrankLists
                                      .Where(d => d.BeerID == theBeer.ID)
                                      .ToListAsync();

                foreach (var item in drinkListOfBeer)
                {
                    _context.DrankLists.Remove(item);
                }
                await this._context.SaveChangesAsync();
            }
            #endregion
            var returnModel = await this._context.Beers
                              .Include(b => b.Country)
                              .Include(b => b.Style)
                              .Include(b => b.Brewery)
                              .FirstOrDefaultAsync(c => c.Name == model.Name);

            //model.ID = returnModel.ID;
            return(returnModel.MapBeerToDTO());
        }