public BeerTypeDTO UpdateBeerType(int id, string name)
        {
            var beerType = _context.BeerTypes
                           .Where(r => r.IsDeleted == false)
                           .FirstOrDefault(r => r.Id == id);

            if (beerType == null)
            {
                throw new ArgumentNullException("BeerType not found.");
            }

            var beerTypeExists = _context.BeerTypes
                                 .FirstOrDefault(b => b.Name == name);

            if (beerTypeExists != null)
            {
                throw new ArgumentException($"BeerType {beerTypeExists.Name} already exists");
            }

            beerType.Name = name;
            var beerTypeDTO = new BeerTypeDTO
            {
                Name = beerType.Name
            };

            _context.SaveChanges();
            return(beerTypeDTO);
        }
Example #2
0
        public async Task <BeerTypeDTO> GetBeerTypeAsync(int id)
        {
            var beerType = await _beerOverflowContext.BeerTypes
                           .FirstOrDefaultAsync(t => t.Id == id);

            if (beerType == null)
            {
                throw new ArgumentNullException();
            }
            var beerTypeDTO = new BeerTypeDTO(beerType.Id, beerType.Type, beerType.Description);

            return(beerTypeDTO);
        }
Example #3
0
        public BeerTypeDTO GetBeerType(int id)
        {
            var beerType = _beerOverflowContext.BeerTypes
                           .FirstOrDefault(t => t.Id == id);

            if (beerType == null)
            {
                throw new ArgumentNullException();
            }
            var beerTypeDTO = new BeerTypeDTO(beerType.Id, beerType.Type, beerType.Description);

            return(beerTypeDTO);
        }
Example #4
0
        public async Task <IActionResult> CreateBeerType([FromBody] BeerTypeViewModel model)
        {
            try
            {
                var beerTypeDto = new BeerTypeDTO(model.Type, model.Description);
                var beerType    = await this.beerTypeServices.CreateBeerTypeAsync(beerTypeDto);

                return(Created("Post", beerType));
            }
            catch (Exception)
            {
                throw;
            }
        }
        public BeerTypeDTO GetBeerType(int id)
        {
            var beerType = _context.BeerTypes
                           .FirstOrDefault(bt => bt.Id == id);

            if (beerType == null)
            {
                throw new ArgumentNullException("BeerType not found.");
            }
            var beerTypeDTO = new BeerTypeDTO
            {
                Id   = beerType.Id,
                Name = beerType.Name
            };

            return(beerTypeDTO);
        }
Example #6
0
        public BeerTypeDTO CreateBeerType(BeerTypeDTO beerTypeDTO)
        {
            try
            {
                var beerType = new BeerType
                {
                    Type        = beerTypeDTO.Type,
                    Description = beerTypeDTO.Description,
                };
                _beerOverflowContext.BeerTypes.Add(beerType);
                _beerOverflowContext.SaveChanges();

                return(beerTypeDTO);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #7
0
        public IActionResult Post([FromBody] BeerTypeViewModel beerTypeViewModel)
        {
            if (beerTypeViewModel == null)
            {
                return(BadRequest());
            }
            if (BeerTypeExists(beerTypeViewModel.Name))
            {
                return(ValidationProblem($"Beer Type with name {beerTypeViewModel.Name} already exists."));
            }
            var beerTypeDTO = new BeerTypeDTO
            {
                Id   = beerTypeViewModel.Id,
                Name = beerTypeViewModel.Name,
            };
            var beerType = _beerTypeServices.CreateBeerType(beerTypeDTO);

            return(Created("Post", beerTypeViewModel));
        }
Example #8
0
        public async Task <BeerTypeDTO> CreateBeerTypeAsync(BeerTypeDTO beerTypeDTO)
        {
            try
            {
                var beerType = new BeerType
                {
                    Type        = beerTypeDTO.Type,
                    Description = beerTypeDTO.Description,
                };
                await _beerOverflowContext.BeerTypes.AddAsync(beerType);

                await _beerOverflowContext.SaveChangesAsync();

                return(beerTypeDTO);
            }
            catch (Exception)
            {
                throw;
            }
        }
Example #9
0
        public void Create_Correct_BeerType()
        {
            var options  = Utils.GetOptions(nameof(Create_Correct_BeerType));
            var beerType = new BeerType
            {
                Id          = 1,
                Description = "Type of beer conditioned at low temperatures.",
                Type        = "Lager",
            };

            using (var arrangeContext = new BeerOverflowContext(options))
            {
                var sut         = new BeerTypesService(arrangeContext);
                var beerTypeDTO = new BeerTypeDTO(beerType.Id, beerType.Type, beerType.Description);
                var result      = sut.CreateBeerType(beerTypeDTO);

                Assert.AreEqual(beerType.Id, result.Id);
                Assert.AreEqual(beerType.Type, result.Type);
                Assert.AreEqual(beerType.Description, result.Description);
            }
        }