Пример #1
0
        public async Task CreateAsync_ShouldRecordBreweryIfNotOnRecordAsync()
        {
            //Arrange
            var options = InMemory.GetOptions("CreateAsync_ShouldRecordBreweryIfNotOnRecordAsync");

            using (var context = new BOContext(options))
            {
                var country = new Country()
                {
                    Name = "Bulgaria"
                };
                context.Countries.Add(country);
                await context.SaveChangesAsync();
            }

            using (var context = new BOContext(options))
            {
                var breweryDTO = new BreweryDTO()
                {
                    Name    = "Brewery",
                    Country = "Bulgaria"
                };
                //Act
                var sut = new BreweryServices(context);
                await sut.CreateAsync(breweryDTO);

                var dbresult = await context.Breweries.Include(b => b.Country).FirstOrDefaultAsync(c => c.Name == "Brewery");

                //Assert
                Assert.AreEqual(dbresult.Name, "Brewery");
            }
        }
Пример #2
0
 public static Brewery MapDTOToBrewery(this BreweryDTO dto)
 {
     try
     {
         var brewery = new Brewery()
         {
             ID        = dto.ID,
             Name      = dto.Name,
             CountryID = dto.CountryID,
             IsDeleted = dto.IsDeleted
         };
         if (dto.Beers != null)
         {
             brewery.Beers = dto.Beers.Select(b => b.MapDTOToBeer()).ToList();
         }
         else
         {
             brewery.Beers = null;
         }
         return(brewery);
     }
     catch (Exception)
     {
         return(new Brewery());
     }
 }
Пример #3
0
        public async Task <BreweryDTO> CreateBreweryAsync(BreweryDTO breweryDTO)
        {
            var check = await this.context.Breweries
                        .FirstOrDefaultAsync(x => x.Name == breweryDTO.Name);

            if (check != null)
            {
                throw new ArgumentNullException("Brewery already existing");
            }

            var country = await this.context.Countries
                          .Where(x => x.Name == breweryDTO.Country)
                          .Where(x => x.isDeleted == false)
                          .FirstOrDefaultAsync();

            if (country == null)
            {
                throw new ArgumentNullException("Country does not exist.");
            }

            var brewery = new Brewery
            {
                Name      = breweryDTO.Name,
                CountryId = country.Id,
                CreatedOn = this.dateTimeProvider.GetDateTime(),
            };

            await this.context.Breweries.AddAsync(brewery);

            await this.context.SaveChangesAsync();

            breweryDTO.Id = brewery.Id;
            return(breweryDTO);
        }
Пример #4
0
        public async Task Create_When_ParamsAreValid()
        {
            var options      = TestUtils.GetOptions(nameof(Create_When_ParamsAreValid));
            var mockDateTime = new Mock <IDateTimeProvider>();

            var country = new Country
            {
                Name = "Romania"
            };

            var breweryDTO = new BreweryDTO
            {
                Name    = "Ariana",
                Country = "Romania"
            };

            using (var arrangeContext = new BeeroverflowContext(options))
            {
                await arrangeContext.Countries.AddAsync(country);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new BeeroverflowContext(options))
            {
                var sut    = new BreweryService(assertContext, mockDateTime.Object);
                var result = await sut.CreateBreweryAsync(breweryDTO);

                var comparison = assertContext.Breweries.Where(x => x.Id == 1).FirstOrDefault();

                Assert.AreEqual("Ariana", comparison.Name);
            }
        }
Пример #5
0
        /// <summary>
        /// Updates the Brewery's Name and Country
        /// </summary>
        /// <param name="id">ID of the Brewery to be updated.</param>
        /// <param name="model">Input object with update information</param>
        /// <returns>Returns the reevaluated input object</returns>
        public async Task <BreweryDTO> UpdateAsync(int?id, BreweryDTO model)
        {
            var brewery = await _context.Breweries.Include(b => b.Country).Include(b => b.Beers).FirstOrDefaultAsync(b => b.ID == id);

            if (brewery == null)
            {
                return(null);
            }
            brewery.Name    = model.Name;
            brewery.Country = await _context.Countries.FirstOrDefaultAsync(c =>
                                                                           c.Name == model.Country);

            brewery.ModifiedOn = DateTime.UtcNow;
            _context.Breweries.Update(brewery);
            model.ID = brewery.ID;

            try
            {
                await this._context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BreweryExists(id))
                {
                    return(null);
                }
            }
            return(model);
        }
Пример #6
0
        public async Task ThrowNullExc_If_BreweryAlreadyExists()
        {
            var options      = TestUtils.GetOptions(nameof(ThrowNullExc_If_BreweryAlreadyExists));
            var mockDateTime = new Mock <IDateTimeProvider>();

            var brewery = new Brewery
            {
                Name = "Ariana"
            };

            var breweryDTO = new BreweryDTO
            {
                Name = "Ariana"
            };

            using (var arrangeContext = new BeeroverflowContext(options))
            {
                await arrangeContext.Breweries.AddAsync(brewery);

                await arrangeContext.SaveChangesAsync();
            }

            using (var assertContext = new BeeroverflowContext(options))
            {
                var sut = new BreweryService(assertContext, mockDateTime.Object);

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.CreateBreweryAsync(breweryDTO));
            }
        }
Пример #7
0
        public BreweryDTO UpdateBrewery(int id, string name, string breweryCountry)
        {
            var brewery = _context.Breweries
                          .Include(br => br.Country)
                          .Where(br => br.IsDeleted == false)
                          .FirstOrDefault(r => r.Id == id);

            if (brewery == null)
            {
                throw new ArgumentNullException("Brewery not found.");
            }

            var breweryExists = _context.Breweries
                                .FirstOrDefault(b => b.Name == name && b.Country.Name == breweryCountry);

            if (breweryExists != null)
            {
                throw new ArgumentException($"Brewery {brewery.Name} already exists in {brewery.Country.Name}");
            }

            brewery.Name    = name;
            brewery.Country = _context.Countries
                              .FirstOrDefault(c => c.Name == breweryCountry) ?? throw new ArgumentNullException("Brewery country not found.");

            var breweryDTO = new BreweryDTO
            {
                Name    = brewery.Name,
                Country = brewery.Country.Name,
            };

            _context.SaveChanges();
            return(breweryDTO);
        }
Пример #8
0
 public static BreweryDTO MapBreweryToDTO(this Brewery brewery)
 {
     try
     {
         var breweryDTO = new BreweryDTO()
         {
             ID        = brewery.ID,
             Name      = brewery.Name,
             Country   = brewery.Country.Name,
             CountryID = brewery.CountryID,
             IsDeleted = brewery.IsDeleted
         };
         if (brewery.Beers != null)
         {
             breweryDTO.Beers = brewery.Beers.Select(b => b.MapBeerToDTO()).ToList();
         }
         else
         {
             breweryDTO.Beers = null;
         }
         return(breweryDTO);
     }
     catch (Exception)
     {
         return(new BreweryDTO());
     }
 }
        public async Task <IActionResult> Put(int id, [FromBody] BreweryDTO model)
        {
            if (id == 0 || model == null)
            {
                return(BadRequest());
            }

            var brewery = await this.breweryService.UpdateBreweryAsync(model.Id, model.Name);

            return(Ok(brewery));
        }
Пример #10
0
        public void MapDTOToBrewery_ShouldReturnBrewery()
        {
            //Arrange
            var cut = new BreweryDTO()
            {
                Name = "Bulgaria"
            };
            //Act
            var sut = cut.MapDTOToBrewery();

            //Assert
            Assert.IsInstanceOfType(sut, typeof(Brewery));
        }
Пример #11
0
        public void MapDTOToBrewery_ShouldReturnCorrectID()
        {
            //Arrange
            var cut = new BreweryDTO
            {
                ID      = 1,
                Country = "Bulgaria"
            };
            //Act
            var sut = cut.MapDTOToBrewery();

            //Assert
            Assert.AreEqual(sut.ID, 1);
        }
Пример #12
0
        public async Task <IActionResult> Post([FromBody] BreweryViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            var breweryDTO = new BreweryDTO(model.Id, model.Name, model.Description, model.CountryId);


            var newBrewery = this.breweryService.CreateBreweryAsync(breweryDTO);

            return(Created("Post", newBrewery));
        }
Пример #13
0
        /// <summary>
        /// Creates a brewery and writes it to the database.
        /// </summary>
        /// <param name="model">Input BreweryDTO object</param>
        /// <returns>Returns the reevaluated input object</returns>
        public async Task <BreweryDTO> CreateAsync(BreweryDTO model)
        {
            var brewery = model.MapDTOToBrewery();

            if (brewery.Name == null)
            {
                return(null);
            }
            brewery.Country = await this._context.Countries.FirstOrDefaultAsync(c => c.Name == model.Country);

            #region Check if exists
            var theBrewery = await this._context.Breweries
                             .FirstOrDefaultAsync(b => b.Name == model.Name);

            if (theBrewery == null)
            {
                brewery.CreatedOn = DateTime.UtcNow;
                await _context.Breweries.AddAsync(brewery);

                await this._context.SaveChangesAsync();
            }
            else
            {
                theBrewery.IsDeleted  = false;
                theBrewery.DeletedOn  = null;
                theBrewery.ModifiedOn = DateTime.UtcNow;
                _context.Breweries.Update(theBrewery);
                var beersOfBrewery = await _context.Beers
                                     .Include(b => b.Country)
                                     .Include(b => b.Brewery)
                                     .Include(b => b.Style)
                                     .Include(b => b.Reviews)
                                     .Where(b => b.BreweryID == theBrewery.ID)
                                     .Where(b => b.Style.IsDeleted == false)
                                     .ToListAsync();

                foreach (var item in beersOfBrewery)
                {
                    await new BeerService(_context).CreateAsync(item.MapBeerToDTO());
                }
                await this._context.SaveChangesAsync();
            }
            #endregion
            var returnModel = await this._context.Breweries.Include(b => b.Country)
                              .FirstOrDefaultAsync(b => b.Name == model.Name);

            model.ID = returnModel.ID;
            return(model);
        }
        public async Task <IActionResult> Put(int id, [FromBody] BreweryDTO model)
        {
            if (id <= 0 || model == null)
            {
                return(BadRequest());
            }

            var returnModel = await this._service.UpdateAsync(id, model);

            if (returnModel == null)
            {
                return(NotFound());
            }
            return(Ok(returnModel));
        }
Пример #15
0
        public async Task <BreweryDTO> CreateBreweryAsync(BreweryDTO breweryDTO)
        {
            var brewery = new Brewery
            {
                Id          = breweryDTO.Id,
                Name        = breweryDTO.Name,
                Description = breweryDTO.Description,
                CountryId   = breweryDTO.CountryId,
            };

            _beerOverflowContext.Breweries.Add(brewery);
            await _beerOverflowContext.SaveChangesAsync();

            return(breweryDTO);
        }
Пример #16
0
        public BreweryDTO CreateBrewery(BreweryDTO breweryDTO)
        {
            var brewery = new Brewery
            {
                Id          = breweryDTO.Id,
                Name        = breweryDTO.Name,
                Description = breweryDTO.Description,
                CountryId   = breweryDTO.CountryId,
            };

            _beerOverflowContext.Breweries.Add(brewery);
            _beerOverflowContext.SaveChanges();

            return(breweryDTO);
        }
Пример #17
0
        public void MapDTOToBrewery_ShouldReturnCorrectNameAndCountry()
        {
            //Arrange
            var cut = new BreweryDTO
            {
                Name    = "Brewery",
                Country = "Bulgaria"
            };
            //Act
            var sut = cut.MapDTOToBrewery();

            //Assert
            Assert.AreEqual(sut.Name, "Brewery");
            //Assert.AreEqual(sut.Country.Name, "Bulgaria");
        }
Пример #18
0
 public static BreweryViewModel GetViewModel(this BreweryDTO dto)
 {
     if (dto == null)
     {
         throw new ArgumentException();
     }
     ;
     return(new BreweryViewModel
     {
         Id = dto.Id,
         Name = dto.Name,
         CountryId = dto.CountryId,
         Country = dto.Country,
         Beers = dto.Beers.GetViewModels()
     });
 }
        public async Task <IActionResult> Post([FromBody] BreweryDTO model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            var theNewBrewery = await this._service.CreateAsync(model);

            if (theNewBrewery.ID == default)
            {
                return(BadRequest());
            }

            return(Created("Post", theNewBrewery));
        }
Пример #20
0
        public async Task <BreweryDTO> GetBreweryByIdAsync(int id)
        {
            var brewery = await _beerOverflowContext.Breweries.
                          Where(b => b.Id == id).
                          Include(b => b.Country).
                          FirstOrDefaultAsync();

            if (brewery == null)
            {
                throw new ArgumentNullException();
            }

            var breweryDTO = new BreweryDTO(brewery.Id, brewery.Name,
                                            brewery.Description, brewery.CountryId, brewery.Country.Name);

            return(breweryDTO);
        }
        public async Task <IActionResult> Post([FromBody] BreweryViewModel model)
        {
            if (model == null)
            {
                return(BadRequest());
            }

            var breweryDTO = new BreweryDTO
            {
                Id      = model.Id,
                Name    = model.Name,
                Country = model.Country
            };
            var newBrewery = await this.breweryService.CreateBreweryAsync(breweryDTO);

            return(Created("Post", newBrewery));
        }
Пример #22
0
        public static BreweryViewModel MapBreweryDTOToView(this BreweryDTO dto)
        {
            var breweryView = new BreweryViewModel()
            {
                ID        = dto.ID,
                Name      = dto.Name,
                CountryID = dto.CountryID,
                Country   = dto.Country,
                IsDeleted = dto.IsDeleted
            };

            if (dto.Beers != null)
            {
                breweryView.Beers = dto.Beers.Select(b => b.Name).ToList();
            }
            return(breweryView);
        }
Пример #23
0
        public async Task UpdateAsync_ShouldChangeNameAndCountryOfBreweryAsync()
        {
            //Arrange
            var options = InMemory.GetOptions("UpdateAsync_ShouldChangeNameOfBreweryAsync");

            using (var context = new BOContext(options))
            {
                var country = new Country()
                {
                    Name      = "Bulgaria",
                    Breweries = new List <Brewery>()
                    {
                        new Brewery()
                        {
                            Name = "Brewery"
                        }
                    }
                };
                var country2 = new Country()
                {
                    Name = "Belgium"
                };
                context.Countries.Add(country);
                context.Countries.Add(country2);
                await context.SaveChangesAsync();
            }

            using (var context = new BOContext(options))
            {
                var breweryDTO = new BreweryDTO()
                {
                    Name    = "Brewery2",
                    Country = "Belgium"
                };
                //Act
                var sut = new BreweryServices(context);
                await sut.UpdateAsync(1, breweryDTO);

                var dbresult = await context.Breweries.Include(b => b.Country).FirstOrDefaultAsync(b => b.ID == 1);

                //Assert
                Assert.AreEqual(dbresult.Name, "Brewery2");
                Assert.AreEqual(dbresult.Country.Name, "Belgium");
            }
        }
Пример #24
0
        public static BreweryDTO MapBreweryViewToDTO(this BreweryViewModel view)
        {
            var breweryDTO = new BreweryDTO()
            {
                ID        = view.ID,
                Name      = view.Name,
                CountryID = view.CountryID,
                IsDeleted = view.IsDeleted
            };

            if (view.Beers != null)
            {
                breweryDTO.Beers = view.Beers.Select(b => new BeerDTO {
                    Name = b
                }).ToList();
            }
            return(breweryDTO);
        }
Пример #25
0
        public IBreweryDTO GetBrewery(int id)
        {
            var brewery = _context.Breweries.Include(b => b.Country).FirstOrDefault(i => i.Id == id);

            if (brewery == null)
            {
                throw new ArgumentNullException("Brewery not found.");
            }

            var breweryDTO = new BreweryDTO
            {
                Id      = brewery.Id,
                Name    = brewery.Name,
                Country = brewery.Country.Name
            };

            return(breweryDTO);
        }
Пример #26
0
        public async Task ThrowNullExc_If_CountryDoesNotExist()
        {
            var options      = TestUtils.GetOptions(nameof(ThrowNullExc_If_CountryDoesNotExist));
            var mockDateTime = new Mock <IDateTimeProvider>();

            var breweryDTO = new BreweryDTO
            {
                Name    = "Ariana",
                Country = "Romania"
            };

            using (var assertContext = new BeeroverflowContext(options))
            {
                var sut = new BreweryService(assertContext, mockDateTime.Object);

                await Assert.ThrowsExceptionAsync <ArgumentNullException>(() => sut.CreateBreweryAsync(breweryDTO));
            }
        }
Пример #27
0
        public async Task CreateAsync_ShouldUndeleteRecordBreweryIfExist()
        {
            //Arrange
            var options = InMemory.GetOptions("CreateAsync_ShouldUndeleteRecordIfExist");

            using (var context = new BOContext(options))
            {
                var country = new Country()
                {
                    Name = "Bulgaria"
                };
                context.Countries.Add(country);
                await context.SaveChangesAsync();

                var brewery = new Brewery()
                {
                    Name      = "Brewery",
                    Country   = await context.Countries.FirstOrDefaultAsync(c => c.Name == "Bulgaria"),
                    DeletedOn = DateTime.UtcNow,
                    IsDeleted = true
                };
                context.Breweries.Add(brewery);
                await context.SaveChangesAsync();
            }

            using (var context = new BOContext(options))
            {
                var breweryDTO = new BreweryDTO()
                {
                    Name    = "Brewery",
                    Country = "Bulgaria"
                };
                //Act
                var sut = new BreweryServices(context);
                await sut.CreateAsync(breweryDTO);

                var dbresult = await context.Breweries.FirstOrDefaultAsync(c => c.Name == "Brewery");

                //Assert
                Assert.AreEqual(dbresult.Name, "Brewery");
                Assert.AreEqual(dbresult.DeletedOn, null);
                Assert.AreEqual(dbresult.IsDeleted, false);
            }
        }
Пример #28
0
        public IActionResult Post([FromBody] BreweryViewModel breweryViewModel)
        {
            if (breweryViewModel == null)
            {
                return(BadRequest());
            }
            if (BreweryExists(breweryViewModel.Name))
            {
                return(ValidationProblem($"Brewery with name {breweryViewModel.Name} already exists"));
            }
            var breweryDTO = new BreweryDTO
            {
                Name    = breweryViewModel.Name,
                Country = breweryViewModel.Country
            };
            var brewery = _breweryServices.CreateBrewery(breweryDTO);

            return(Created("Post", brewery));
        }
Пример #29
0
        public async Task CreateAsync_ShouldReturnModifiedBreweryDTOAsync()
        {
            //Arrange
            var options = InMemory.GetOptions("CreateAsync_ShouldReturnModifiedBreweryDTOAsync");

            using (var context = new BOContext(options))
            {
                var country = new Country()
                {
                    Name      = "Bulgaria",
                    Breweries = new List <Brewery>()
                    {
                        new Brewery()
                        {
                            Name = "Brewery"
                        }
                    }
                };
                context.Countries.Add(country);
                await context.SaveChangesAsync();
            }

            using (var context = new BOContext(options))
            {
                var breweryDTO = new BreweryDTO()
                {
                    Name    = "Brewery",
                    Country = "Bulgaria"
                };
                //Act
                var sut    = new BreweryServices(context);
                var result = await sut.CreateAsync(breweryDTO);

                var dbresult = await context.Breweries.FirstOrDefaultAsync(c => c.Name == "Brewery");

                //Assert
                Assert.AreEqual(result.ID, dbresult.ID);
                Assert.AreEqual(result.Name, dbresult.Name);
            }
        }
Пример #30
0
        public async Task <BreweryDTO> UpdateBreweryAsync(int id, string newName, string newDescrip, int newCountryId)
        {
            var brewery = await _beerOverflowContext.Breweries
                          .FirstOrDefaultAsync(brewery => brewery.Id == id);

            if (brewery == null)
            {
                throw new ArgumentNullException();
            }

            brewery.Name        = newName;
            brewery.Description = newDescrip;
            brewery.CountryId   = newCountryId;

            var breweryDTO = new BreweryDTO(brewery.Id, brewery.Name,
                                            brewery.Description, brewery.CountryId);

            _beerOverflowContext.Breweries.Update(brewery);
            await _beerOverflowContext.SaveChangesAsync();

            return(breweryDTO);
        }