示例#1
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);
            }
        }
        public async Task <IActionResult> PostBeer([FromBody] BeerDto beerDto)
        {
            BeerDto beer;

            _logger.LogInformation("Beer isCommercial: " + beerDto.IsCommercial);
            if (beerDto == null)
            {
                return(BadRequest("Missing data"));
            }
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (beerDto.IsCommercial)
            {
                beer = await _beerService.AddAsync(beerDto);
            }
            else
            {
                var username = HttpContext.User.Identity.Name;
                if (username == null)
                {
                    return(BadRequest("Missing user"));
                }
                beer = await _beerService.AddAsync(beerDto, username);
            }
            if (beer == null)
            {
                return(BadRequest());
            }

            return(CreatedAtRoute(new { controller = "beers" }, beer));
        }
示例#3
0
        public async Task <BeerDto> AddAsync(BeerDto beerDto)
        {
            var beer = AutoMapper.Mapper.Map <BeerDto, Beer>(beerDto);

            if (beerDto.Recipe != null && beerDto.Recipe.Steps != null)
            {
                BeerCalculations(beer);
            }

            await _beerRepository.AddAsync(beer);

            var result = await _beerRepository.GetSingleAsync(beer.BeerId);

            var mappedResult = AutoMapper.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 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));
        }
示例#5
0
        public async Task <IActionResult> Get(string beerId)
        {
            if (Guid.TryParse(beerId, out Guid id))
            {
                var beer = await _beersService.Get(id);

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

                var data = new BeerDto
                {
                    BeerId        = beer.Id,
                    Title         = beer.Title,
                    Volume        = beer.Volume,
                    NonAlcohol    = beer.NonAlcohol,
                    DrankQuantity = beer.DrankQuantity
                };

                return(new OkObjectResult(data));
            }

            return(NotFound());
        }
示例#6
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);
        }
示例#7
0
 public async void OnItemTapped(object sender, ItemTappedEventArgs e)
 {
     BeerDto beer = e.Item as BeerDto;
     var     page = new Page1(beer);
     //this.Navigation.PushAsync(page);
     await Navigation.PushAsync(new Page1(beer));
 }
示例#8
0
        /// <summary>
        /// This method creates database.json
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="ratings"></param>
        /// <param name="comments"></param>
        /// <returns>success or failure</returns>
        public static bool CreateJsonFile(string userName, string ratings, string comments)
        {
            try
            {
                BeerDto beerDto = new BeerDto();
                beerDto.UserName = userName;
                //beerDto.Id = Convert.ToInt32(Request.Form["Id"]);
                beerDto.Rating   = Convert.ToInt32(ratings);
                beerDto.Comments = comments;

                List <BeerDto> listBeerDtos = new List <BeerDto>();
                listBeerDtos.Add(beerDto);
                //string json = JsonConvert.SerializeObject(beerDto);
                string json = JsonConvert.SerializeObject(listBeerDtos);
                //TODO: Remove hard coding and move this path to appsettings.

                System.IO.File.AppendAllTextAsync(jsonFile, json);
                string jsonTxt = File.ReadAllText(jsonFile);
                //The generated json was not a valid json structure. So replaced.
                System.IO.File.WriteAllText(jsonFile, jsonTxt.Replace("][", ","));
                return(true);
            }
            catch
            {
                return(false);
            }
        }
示例#9
0
        public async Task <bool> Add(BeerDto beer, long userId)
        {
            try
            {
                var user = await _beerContext.
                           User.
                           SingleOrDefaultAsync(u => u.Id == userId);

                var beerModel = new BeerEntity {
                    Producer = beer.Producer,
                    Title    = beer.Title,
                    Creater  = user
                };

                var created = await _beerContext.Beer.AddAsync(beerModel);

                var resultCount = await _beerContext.SaveChangesAsync();

                return(resultCount != 0);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return(false);
            }
        }
示例#10
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);
        }
示例#11
0
        public static SelectListItem ToSelectListItem(this BeerDto source)
        {
            var result = new SelectListItem();

            result.Value = source.Code.ToString();
            result.Text  = source.Description;

            return(result);
        }
示例#12
0
        public HttpResponseMessage Get(string beerId)
        {
            var beer = new BeerDto()
            {
                Id   = 1,
                Name = "Hopwired IPA"
            };

            return(Request.CreateResponse(HttpStatusCode.OK, beer));
        }
示例#13
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));
            }
        }
示例#14
0
        public async Task <IActionResult> Put(int id, [FromBody] BeerDto beer)
        {
            var updatedBeer = await _repository.Update(id, beer);

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

            return(Ok(updatedBeer));
        }
示例#15
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);
            }
        }
        public async Task ReIndexBeerRelationElasticSearch(BeerDto beerDto)
        {
            foreach (var dtoBrewery in beerDto.Breweries)
            {
                var brewery = dtoBrewery;
                var result  = await _breweryRepository.GetSingleAsync(brewery.Id);

                var mappedResult = AutoMapper.Mapper.Map <Brewery, BreweryDto>(result);
                await _breweryElasticsearch.UpdateAsync(mappedResult);
            }
        }
示例#17
0
 public async Task Add(BeerDto dto)
 {
     Beer entity = new Beer()
     {
         Name  = dto.Name,
         Style = dto.Style,
         Logo  = dto.Logo,
         SuggestedGlassType = dto.SuggestedGlassType,
         Created            = DateTime.Now
     };
     await BeerDataProvider.Add(entity);
 }
示例#18
0
        public int UpdateBeer(BeerDto source)
        {
            var entityToModify = Context.Beer.Find(source.Code);

            entityToModify.Name       = source.Description;
            entityToModify.Graduation = source.Graduation;
            entityToModify.Country    = Context.Country.Find(source.CountryId);
            entityToModify.BeerType   = Context.BeerType.Find(source.BeerTypeId);
            entityToModify.City       = Context.City.Find(source.CityId);

            return(Context.SaveChanges());
        }
示例#19
0
 public async Task Update(BeerDto dto)
 {
     Beer entity = new Beer()
     {
         Id    = dto.Id,
         Name  = dto.Name,
         Style = dto.Style,
         Logo  = dto.Logo,
         SuggestedGlassType = dto.SuggestedGlassType
     };
     await BeerDataProvider.Update(entity);
 }
示例#20
0
        public async Task <IActionResult> Post(BeerDto beerDto)
        {
            var userId = Convert.ToInt64(User.Identity.Name);

            if (await _beerService.Add(beerDto, userId))
            {
                return(Ok());
            }
            else
            {
                return(BadRequest());
            }
        }
示例#21
0
        public async Task <ActionResult <BeerDto> > GetBeer(int id)
        {
            var beer = await _beerService.GetAsync(id);

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

            BeerDto result = _mapper.Map <BeerDto>(beer);

            return(Ok(result));
        }
        public async Task <IActionResult> Put([FromBody] BeerDto dto)
        {
            try
            {
                await BeerService.Update(dto);

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex));
            }
        }
        public async Task <IActionResult> Get(int id)
        {
            try
            {
                BeerDto beer = await BeerService.GetBeer(id);

                return(Ok(beer));
            }
            catch (Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, ex));
            }
        }
示例#24
0
        public static BeerViewModel ToBeerViewModel(this BeerDto source)
        {
            var result = new BeerViewModel
            {
                Id          = source.Code,
                Name        = source.Description,
                Graduation  = source.Graduation,
                BeerTypeDto = source.BeerTypeDto,
                CountryDto  = source.CountryDto,
                CityDto     = source.CityDto
            };

            return(result);
        }
示例#25
0
        public static BeerViewModelDetails ToBeerViewModelDetails(this BeerDto source)
        {
            var result = new BeerViewModelDetails
            {
                Id         = source.Code,
                Name       = source.Description,
                Graduation = source.Graduation,
                BeerTypeId = source.BeerTypeId,
                CountryId  = source.CountryId,
                CityId     = source.CityId
            };

            return(result);
        }
示例#26
0
        public static BeerDto ModelDetailsToBeerDto(this BeerViewModelDetails source)
        {
            var result = new BeerDto
            {
                Code        = source.Id,
                Description = source.Name,
                Graduation  = source.Graduation,
                BeerTypeId  = source.BeerTypeDto.Code,
                CountryId   = source.CountryDto.Code,
                CityId      = source.CityDto.Code
            };

            return(result);
        }
示例#27
0
        public static BeerDto ToBeerDto(this Beer source)
        {
            var result = new BeerDto
            {
                Code        = source.Id,
                Graduation  = source.Graduation,
                Description = source.Name,
                BeerTypeDto = source.BeerType.ToBeerTypeDto(),
                CountryDto  = source.Country.ToCountryDto(),
                CityDto     = source.City.ToCityDto(),
            };

            return(result);
        }
示例#28
0
        public static BeerDto ToBeerDetailDto(this Beer source)
        {
            var result = new BeerDto
            {
                Code        = source.Id,
                Graduation  = source.Graduation,
                Description = source.Name,
                BeerTypeId  = source.BeerType.Id,
                CountryId   = source.Country.Id,
                CityId      = source.City.Id
            };

            return(result);
        }
示例#29
0
        public static Beer ToBeer(this BeerDto source)
        {
            var result = new Beer
            {
                Id         = source.Code,
                Name       = source.Description,
                Graduation = source.Graduation,
                BeerType   = source.BeerTypeDto.ToBeerType(),
                Country    = source.CountryDto.ToCountry(),
                City       = source.CityDto.ToCity()
            };

            return(result);
        }
        public async Task <IActionResult> PutBeer(int id, BeerDto beerDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (id != beerDto.Id)
            {
                return(BadRequest());
            }
            await _beerService.UpdateAsync(beerDto);

            return(new StatusCodeResult((int)HttpStatusCode.Created));
        }