Пример #1
0
        public async Task<BeerDto> AddAsync(BeerDto beerDto)
        {
            var beer = Mapper.Map<BeerDto, Beer>(beerDto);
            if (beerDto.Recipe != null)
            {
                BeerCalculations(beer);
            }
            beer.BeerStyle = null;
            beer.CreatedDate = DateTime.Now;
            beer.UpdatedDate = DateTime.Now;

            await _beerRepository.AddAsync(beer);
            var result = await _beerRepository.GetSingleAsync(beer.BeerId, _include);
            var mappedResult = Mapper.Map<Beer, BeerDto>(result);
            await _beerElasticsearch.UpdateAsync(mappedResult);
            // elasticsearch relation managing
            if (mappedResult.ForkOfId != null)
                await ReIndexSingleElasticSearchAsync((int)mappedResult.ForkOfId);
            if (mappedResult.Brewers.Any())
                await _userService.ReIndexBeerRelationElasticSearch(beerDto);
            if (mappedResult.Breweries.Any())
                await _breweryService.ReIndexBeerRelationElasticSearch(beerDto);
            return mappedResult;

        }
        public async void AddBeer_Gets_Added()
        {
            var beer = new BeerDto
            {
                Name = "Test Beer",
                BeerStyle = new BeerStyleSimpleDto {Id = 1, Name = "American-Style Wheat Beer"},
                Brewers = new List<DTOUser> { new DTOUser { Username = "******"} },
            };

            var recipe = new RecipeDto
            {
                MashSteps = new List<MashStepDto>{new MashStepDto{StepNumber = 1}},
                //SpargeStep = new List<SpargeStepDto>{new SpargeStepDto{StepNumber = 2,Amount = 1, Temperature = 80}},
                SpargeStep = new SpargeStepDto{StepNumber = 2,Amount = 1, Temperature = 80},
                BoilSteps = new List<BoilStepDto> {new BoilStepDto {StepNumber = 3}},
                FermentationSteps = new List<FermentationStepDto> {new FermentationStepDto{StepNumber = 4}},
                Volume = 20,
                Efficiency = 75,
            };

            beer.Recipe = recipe;
            var beerDto = await _beerService.AddAsync(beer);
            Assert.NotNull(beerDto);
        }
 public async Task<IIndexResponse> UpdateAsync(BeerDto beerDto)
 {
     // Adds an analayzer to the name property in FermentableDto object.
     await _client.MapAsync<BeerDto>(d => d.Properties(p => p.String(s => s.Name(n => n.Name).Analyzer("autocomplete"))));
     return await _client.IndexAsync<BeerDto>(beerDto);
 }
Пример #4
0
        public async Task<IHttpActionResult> PutBeer(int id, BeerDto beerDto)
        {
            var isAllowed = ClaimsAuthorization.CheckAccess("Put", "BeerId", id.ToString());
            if (!isAllowed)
            {
                return StatusCode(HttpStatusCode.Unauthorized);
            }
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != beerDto.Id)
            {
                return BadRequest();
            }
            Log.Debug(JsonConvert.SerializeObject(beerDto));
            await _beerService.UpdateAsync(beerDto);
            return StatusCode(HttpStatusCode.NoContent);
        }
Пример #5
0
 public async Task<IHttpActionResult> PostBeer(BeerDto beerDto)
 {
     if (beerDto == null) return BadRequest("Missing data");
     if (!ModelState.IsValid)
     {
         return BadRequest(ModelState);
     }
     var username = HttpContext.Current.User.Identity.Name;
     if (username == null) return BadRequest("Missing user");
     var beer = await _beerService.AddAsync(beerDto, username);
     if (beer == null) return BadRequest();
     return CreatedAtRoute("DefaultApi", new { controller = "beers" }, beer);
 }
Пример #6
0
        public async Task UpdateAsync(BeerDto beerDto)
        {
            if (beerDto.Recipe != null) beerDto.Recipe.Id = beerDto.Id;
            var beer = Mapper.Map<BeerDto, Beer>(beerDto);

            if (beer.Recipe != null)
            {
                BeerCalculations(beer);
            }
            //Log.Debug(JsonConvert.SerializeObject(beer));
            await _beerRepository.UpdateAsync(beer);
            var result = await _beerRepository.GetSingleAsync(beerDto.Id, _include);
            var mappedResult = Mapper.Map<Beer, BeerDto>(result);
            await _beerElasticsearch.UpdateAsync(mappedResult);
            // elasticsearch relation managing
            if (mappedResult.Brewers.Any())
                await _userService.ReIndexBeerRelationElasticSearch(beerDto);
            if (mappedResult.Breweries.Any())
                await _breweryService.ReIndexBeerRelationElasticSearch(beerDto);
        }
Пример #7
0
 public async Task<BeerDto> AddAsync(BeerDto beerDto, string username)
 {
     if (beerDto.Brewers != null && beerDto.Brewers.All(b => b.Username != username))
     {
         if (beerDto.Breweries.Any())
         {
             var breweryMemberships = _breweryService.GetMemberships(username);
             if (beerDto.Breweries.Any(brewery => breweryMemberships.Any(b => b.BreweryId != brewery.Id)))
                 return null;
         }
     }
     else
     {
         if (beerDto.Brewers == null) beerDto.Brewers = new List<DTOUser>();
         if (beerDto.Brewers.Any(b => b.Username != username))
             beerDto.Brewers.Add(new DTOUser { Username = username });
     }
     var returnBeer = await AddAsync(beerDto);
     await _userService.UpdateNotification(username, new NotificationDto { Id = returnBeer.Id, Type = "UserBeer", Value = true });
     return returnBeer;
 }
Пример #8
0
 public async Task ReIndexBeerRelationElasticSearch(BeerDto beerDto)
 {
     foreach (var dtoUser in beerDto.Brewers)
     {
         var user = dtoUser;
         var result = await _userRepository.GetSingleAsync(user.Username, _userInclude);
         await _userElasticsearch.UpdateAsync(Mapper.Map<User, UserDto>(result));
     }
 }
Пример #9
0
 public async Task ReIndexBeerRelationElasticSearch(BeerDto beerDto)
 {
     foreach (var dtoBrewery in beerDto.Breweries)
     {
         var brewery = dtoBrewery;
         var result = await _breweryRepository.GetSingleAsync(brewery.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);
         await _breweryElasticsearch.UpdateAsync(mappedResult);
     }
 }