protected override IEnumerable <BrewerySocial> ResolveCore(BreweryDto source)
        {
            var brewerySocials   = new List <BrewerySocial>();
            var dbBrewerySocials = _breweryRepository.GetBrewerySocials(source.Id);

            if (source.Socials == null)
            {
                return(brewerySocials);
            }
            foreach (var social in source.Socials)
            {
                var brewerySocial = dbBrewerySocials.SingleOrDefault(s => s.Site == social.Key);
                if (brewerySocial != null)
                {
                    if (brewerySocial.Url != social.Value)
                    {
                        brewerySocial.Url = social.Value;
                    }
                    brewerySocials.Add(brewerySocial);
                }
                else
                {
                    brewerySocial = new BrewerySocial {
                        BreweryId = source.Id, Site = social.Key, Url = social.Value
                    };
                    brewerySocials.Add(brewerySocial);
                }
            }
            return(brewerySocials);
        }
        /// <summary>
        /// Updates a brewery entity from the given DTO and id.
        /// </summary>
        /// <param name="id">Entity primary ID</param>
        /// <param name="breweryDto">Brewery entity</param>
        /// <exception cref="BreweryNotFoundException"></exception>
        public void UpdateBrewery(int id, BreweryDto breweryDto)
        {
            Brewery originalBrewery = null;
            var     updatedBrewery  = _mapper.Map <Brewery>(breweryDto);

            // Set the brewery id on the updatedBrewery before passing to validation
            updatedBrewery.BreweryId = id;

            try
            {
                originalBrewery = GetBrewery(id);
            }
            catch (BrewdoggerException brewdoggerException)
            {
                Log.Error("BreweryService::UpdateBrewery - No breweries found");
            }

            if (!ValidateBreweryExists(updatedBrewery))
            {
                throw new BreweryNotFoundException("Brewery does not exist");
            }

            if (originalBrewery != null)
            {
                _breweryRepository.UpdateBrewery(originalBrewery, updatedBrewery);
                Log.Information("BreweryService::UpdateBrewery - Brewery [{0}] successfully updated", id);
            }
        }
Exemple #3
0
        public async Task UpdateAsync(BreweryDto breweryDto)
        {
            await _client.MapAsync <BreweryDto>(d => d.Properties(p => p
                                                                  .String(s => s.Name(n => n.Name).Analyzer("autocomplete"))
                                                                  ));

            await _client.IndexAsync(breweryDto, idx => idx.Index(_elasticSearchSettings.Index));
        }
        public async Task UpdateAsync(BreweryDto breweryDto)
        {
            await _client.MapAsync <BreweryDto>(d => d.Properties(p => p
                                                                  .String(s => s.Name(n => n.Name).Analyzer("autocomplete"))
                                                                  ));

            await _client.IndexAsync(breweryDto);
        }
        public async Task UpdateAsync(BreweryDto breweryDto)
        {
            var brewery = AutoMapper.Mapper.Map <BreweryDto, Brewery>(breweryDto);
            await _breweryRepository.UpdateAsync(brewery);

            var result = await _breweryRepository.GetSingleAsync(breweryDto.Id);

            var mappedResult = AutoMapper.Mapper.Map <Brewery, BreweryDto>(result);
            //if (brewery.Members.Any()) await _userService.ReIndexBreweryRelationElasticSearch(mappedResult);
            await _breweryElasticsearch.UpdateAsync(mappedResult);
        }
        public async Task <BreweryDto> AddAsync(BreweryDto breweryDto)
        {
            var brewery = AutoMapper.Mapper.Map <BreweryDto, Brewery>(breweryDto);
            await _breweryRepository.AddAsync(brewery);

            var result = await _breweryRepository.GetSingleAsync(brewery.BreweryId);

            var mappedResult = AutoMapper.Mapper.Map <Brewery, BreweryDto>(result);
            await _breweryElasticsearch.UpdateAsync(mappedResult);

            return(mappedResult);
        }
Exemple #7
0
        public async Task <BreweryDto> AddAsync(BreweryDto breweryDto)
        {
            var brewery = Mapper.Map <BreweryDto, Brewery>(breweryDto);
            await _breweryRepository.AddAsync(brewery);

            var result = await _breweryRepository.GetSingleAsync(brewery.BreweryId, "Members.Member", "Origin", "Beers", "Socials", "Beers.Beer.IBU", "Beers.Beer.ABV", "Beers.Beer.SRM", "Beers.Beer.BeerStyle");

            var mappedResult = Mapper.Map <Brewery, BreweryDto>(result);
            await _breweryElasticsearch.UpdateAsync(mappedResult);

            return(mappedResult);
        }
Exemple #8
0
        public async Task ReIndexBreweryRelationElasticSearch(BreweryDto brewerDto)
        {
            foreach (var memberDto in brewerDto.Members)
            {
                var member = memberDto;
                var result = await _userRepository.GetSingleAsync(member.Username, _userInclude);

                if (result != null)
                {
                    await _userElasticsearch.UpdateAsync(Mapper.Map <User, UserDto>(result));
                }
            }
        }
        public async Task <ActionResult <BreweryDto> > GetBrewery(int id)
        {
            var brewery = await _service.GetAsync(id);

            if (brewery == null)
            {
                return(NotFound());
            }

            BreweryDto result = _mapper.Map <BreweryDto>(brewery);

            return(Ok(result));
        }
        public IActionResult CreateBrewery([FromBody] BreweryDto breweryDto)
        {
            try
            {
                _breweryService.CreateBrewery(breweryDto);
            }
            catch (BrewdoggerException be)
            {
                return(BadRequest("Brewery could not be created"));
            }

            return(Created("Brewery created", breweryDto));
        }
        public IActionResult UpdateBreweryById(int id, [FromBody] BreweryDto breweryDto)
        {
            try
            {
                _breweryService.UpdateBrewery(id, breweryDto);
            }
            catch (BreweryNotFoundException breweryNotFoundException)
            {
                Log.Error("BreweryController::UpdateBrewery - Could not find any breweries to update");
                return(BadRequest($"Could not update brewery with id [{id}]"));
            }

            return(Ok($"Brewery [{id}] updated successfully"));
        }
        public async Task <IActionResult> PostBrewery([FromBody] BreweryDto brewery)
        {
            if (brewery == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var result = await _breweryService.AddAsync(brewery);

            return(CreatedAtRoute(new { controller = "breweries" }, result));
        }
Exemple #13
0
        public async Task UpdateAsync(BreweryDto breweryDto)
        {
            var brewery = Mapper.Map <BreweryDto, Brewery>(breweryDto);
            await _breweryRepository.UpdateAsync(brewery);

            var result = await _breweryRepository.GetSingleAsync(breweryDto.Id, "Members.Member", "Origin", "Beers", "Socials", "Beers.Beer.IBU", "Beers.Beer.ABV", "Beers.Beer.SRM", "Beers.Beer.BeerStyle");

            var mappedResult = Mapper.Map <Brewery, BreweryDto>(result);

            if (brewery.Members.Any())
            {
                await _userService.ReIndexBreweryRelationElasticSearch(mappedResult);
            }
            await _breweryElasticsearch.UpdateAsync(mappedResult);
        }
        public async Task <IActionResult> PutBrewery(int id, [FromBody] BreweryDto breweryDto)
        {
            // Checks if login user is allowed to change brewery.
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != breweryDto.Id)
            {
                return(BadRequest());
            }
            await _breweryService.UpdateAsync(breweryDto);

            return(new StatusCodeResult((int)HttpStatusCode.NoContent));
        }
        protected override IList <BreweryMember> ResolveCore(BreweryDto source)
        {
            var members = new List <BreweryMember>();

            if (source.Members == null)
            {
                return(members);
            }

            foreach (var memberDto in source.Members)
            {
                var member = new BreweryMember()
                {
                    BreweryId      = source.Id,
                    MemberUsername = memberDto.Username,
                    //Role = memberDto.Role,
                };
                members.Add(member);
            }
            return(members);
        }
        public async Task <IHttpActionResult> PostBrewery(BreweryDto brewery)
        {
            if (brewery == null)
            {
                return(BadRequest());
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            try
            {
                var result = await _breweryService.AddAsync(brewery);

                return(CreatedAtRoute("DefaultApi", new { controller = "breweries" }, result));
            }
            catch (DbEntityValidationException exception)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = exception.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join("; ", errorMessages);

                // Combine the original exception message with the new one.
                return(BadRequest(string.Concat(exception.Message, " The validation errors are: ", fullErrorMessage)));
            }
            catch (DbUpdateException exception)
            {
                if (exception.ToString().Contains("IX_BreweryName"))
                {
                    ModelState.AddModelError("Name", "Duplicate brewery name not allowed.");
                }

                return(BadRequest(ModelState));
            }
        }
        public async Task <IHttpActionResult> PutBrewery(int id, BreweryDto breweryDto)
        {
            // Checks if login user is allowed to change brewery.
            var isAllowed = ClaimsAuthorization.CheckAccess("Post", "BreweryId", id.ToString());

            if (!isAllowed)
            {
                return(StatusCode(HttpStatusCode.Unauthorized));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != breweryDto.Id)
            {
                return(BadRequest());
            }
            await _breweryService.UpdateAsync(breweryDto);

            return(StatusCode(HttpStatusCode.NoContent));
        }
        /// <summary>
        /// Creates a brewery from a brewery DTO to begin tracking.
        /// </summary>
        /// <param name="breweryDto">Brewery data transfer object</param>
        public void CreateBrewery(BreweryDto breweryDto)
        {
            // Map breweryDto to brewery object
            var newBrewery = _mapper.Map <BreweryDto, Brewery>(breweryDto);

            if (ValidateBreweryExists(newBrewery))
            {
                throw new BrewdoggerException("BreweryService::CreateBrewery - Brewery [" + newBrewery.BreweryName + "] already exists");
            }

            // Validate brewery
            var breweryValidator = new BreweryValidator();
            var validatedBrewery = breweryValidator.Validate(newBrewery, ruleSet: "InputFields,StateRules");

            if (validatedBrewery.IsValid && validatedBrewery.Errors.Count == 0)
            {
                _breweryRepository.SaveBrewery(newBrewery);
                Log.Information("BreweryService::CreateBrewery - Brewery [{0}] saved successfully", newBrewery.BreweryName);
                return;
            }

            // Return validation error if any
            if (!validatedBrewery.IsValid)
            {
                Log.Error("BreweryService::CreateBrewery - Brewery is not valid, creation failed");

                if (validatedBrewery.Errors.Count > 0)
                {
                    foreach (var error in validatedBrewery.Errors)
                    {
                        Log.Information("BreweryService::CreateBrewery - Error code [{0}] validation error: {1}", error.ErrorCode, error.ErrorMessage);
                    }
                }

                throw new BrewdoggerException("Validation error while attempting to add brewery to database");
            }
        }
Exemple #19
0
        //readonly IBreweryRepository _breweryRepository = new BreweryDapperRepository();

        protected override IEnumerable <BrewerySocial> ResolveCore(BreweryDto source)
        {
            var brewerySocials = new List <BrewerySocial>();

            // var dbBrewerySocials = _breweryRepository.GetBrewerySocials(source.Id);
            // if (source.Socials == null) return brewerySocials;
            // foreach (var social in source.Socials)
            // {
            //     var brewerySocial = dbBrewerySocials.SingleOrDefault(s => s.Site == social.Key);
            //     if (brewerySocial != null)
            //     {
            //         if (brewerySocial.Url != social.Value)
            //             brewerySocial.Url = social.Value;
            //         brewerySocials.Add(brewerySocial);
            //     }
            //     else
            //     {
            //         brewerySocial = new BrewerySocial {BreweryId = source.Id, Site = social.Key, Url = social.Value};
            //         brewerySocials.Add(brewerySocial);
            //     }
            //
            // }
            return(brewerySocials);
        }
 public Task ReIndexBreweryRelationElasticSearch(BreweryDto breweryDto)
 {
     throw new NotImplementedException();
 }
 public BlobStorageMultipartStreamProvider(BreweryDto breweryDto)
 {
     _breweryDto = breweryDto;
 }
Exemple #22
0
        public void SetUp()
        {
            _brewery1 = new Brewery
            {
                BreweryId   = 1,
                BreweryName = "Fall River Brewery",
                City        = "Redding",
                State       = "CA"
            };

            _brewery2 = new Brewery
            {
                BreweryId   = 2,
                BreweryName = "Sierra Nevada Brewing Company",
                City        = "Chico",
                State       = "CA"
            };

            _brewery3 = new Brewery
            {
                BreweryId   = 3,
                BreweryName = "Coors Brewing Company",
                City        = "Golden",
                State       = "CO"
            };

            _breweryDto = new BreweryDto
            {
                BreweryName = "Coors Brew Company",
                City        = "Golden",
                State       = "CO"
            };

            _breweryToUpdate = new BreweryDto
            {
                BreweryName = "Sierra Nevada Brewing Company",
                City        = "Mills River",
                State       = "NC"
            };

            _updatedBrewery = new Brewery
            {
                BreweryId   = 2,
                BreweryName = "Sierra Nevada Brewing Company",
                City        = "Mills River",
                State       = "NC"
            };

            _breweries = new Collection <Brewery> {
                _brewery1, _brewery2
            };

            // Mock repository
            _breweryRepository = new Mock <IBreweryRepository>();

            // Mock reads
            _breweryRepository.Setup(b => b.FindBreweryById(1)).Returns(_brewery1);
            _breweryRepository.Setup(b => b.FindBreweryById(2)).Returns(_brewery2);
            _breweryRepository.Setup(b => b.FindAllBreweries()).Returns(_breweries);

            // Mock creates
            _breweryRepository.Setup(b => b.SaveBrewery(It.IsAny <Brewery>())).Callback(() => _breweries.Add(_brewery2));
            _breweryRepository.Verify(b => b.SaveBrewery(It.IsAny <Brewery>()), Times.Once);

            // Mock deletes
            _breweryRepository.Setup(b => b.DeleteBreweryById(It.IsAny <Brewery>())).Callback(() => _breweries.Remove(_brewery2));

            // Mock updates
//            _breweryRepository.Setup(b => b.UpdateBrewery(_updatedBrewery))
//                .Callback(() => _brewery2 = _updatedBrewery);

            // Mock mapper
            _mapper = new Mock <IMapper>();
            _mapper.Setup(m => m.Map <BreweryDto, Brewery>(_breweryDto)).Returns(_brewery3);

            // Mock validator
            _breweryValidator = new Mock <BreweryValidator>();

//            _breweryService = new BreweryService(_breweryRepository.Object, _mapper.Object, _breweryValidator.Object);
        }