Ejemplo n.º 1
0
        public async Task <bool> UpdateBeer(int id, SaveBeerDto beerDto)
        {
            var beer = await _beerRepository.GetBeer(id);

            var mappedBeer = _mapper.Map(beerDto, beer);

            if (await _beerRepository.UpdateAsyn(mappedBeer, id) == null)
            {
                return(false);
            }
            return(true);
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> AddBeer([FromBody] SaveBeerDto beerDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var userLogged = int.Parse(HttpContext.User.Identity.Name);

            var result = await _service.AddBeerAsync(beerDto, userLogged);

            return(CreatedAtRoute("GetBeer", new { id = result.Id }, result));
        }
Ejemplo n.º 3
0
        public async Task <GetBeerDto> AddBeerAsync(SaveBeerDto beerDto, int userLogged)
        {
            var beer = _mapper.Map <SaveBeerDto, Beer>(beerDto);

            beer.CreateDateTime = DateTime.Now;
            beer.Confirmation   = false;
            beer.UserId         = userLogged;
            await _beerRepository.AddAsyn(beer);

            await _beerRepository.SaveAsync();

            return(await GetBeerAsync(beer.Id));
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> AddRating(int beerId, [FromBody] AddBeerRatingDto ratingDto)
        {
            if (!await _service.IfBeerExistAsync(beerId))
            {
                return(NotFound());
            }

            var userLogged = int.Parse(HttpContext.User.Identity.Name);

            var userRating = await _service.GetBeerRaitingForUserAsync(userLogged, beerId);

            if (userRating == null)
            {
                var result = await _service.AddBeerRatingAsync(userLogged, beerId, ratingDto);

                var beer = await _beerService.GetBeerAsync(beerId);

                var updatedBeer = new SaveBeerDto();

                updatedBeer.Name        = beer.Name;
                updatedBeer.AvatarUrl   = beer.AvatarUrl;
                updatedBeer.BeerTypeId  = beer.BeerType.Id;
                updatedBeer.BreweryId   = beer.Brewery.Id;
                updatedBeer.Description = beer.Description;
                updatedBeer.Percentage  = beer.Percentage;
                if (beer.AverageRating == 0)
                {
                    updatedBeer.AverageRating = (beer.AverageRating + ratingDto.Average);
                }
                else
                {
                    updatedBeer.AverageRating = (beer.AverageRating + ratingDto.Average) / 2;
                }


                var updateResult = await _beerService.UpdateBeer(beerId, updatedBeer);

                if (!updateResult)
                {
                    return(BadRequest("Niepowodzenie :("));
                }

                return(Ok(result));
            }

            return(BadRequest("Juz dodałeś ocenę"));
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> UpdateBeer(int id, [FromBody] SaveBeerDto beerDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            if (!await _service.IsBeerExist(id))
            {
                return(NotFound());
            }



            if (!await _service.UpdateBeer(id, beerDto))
            {
                return(BadRequest("Wystąpił problem podczas modyfikacji typu piwa"));
            }

            return(Ok());
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> UpdateRating(int ratingId, [FromBody] AddBeerRatingDto ratingDto)
        {
            if (!await _service.IfExistRating(ratingId))
            {
                return(NotFound());
            }
            var beerId = await _service.UpdateRatingAsync(ratingId, ratingDto);

            GetBeerRatingDto beerRating = await _service.GetBeerRatingAsync(beerId);

            var beer = await _beerService.GetBeerAsync(beerId);

            var updatedBeer = new SaveBeerDto();

            updatedBeer.Name        = beer.Name;
            updatedBeer.AvatarUrl   = beer.AvatarUrl;
            updatedBeer.BeerTypeId  = beer.BeerType.Id;
            updatedBeer.BreweryId   = beer.Brewery.Id;
            updatedBeer.Description = beer.Description;
            updatedBeer.Percentage  = beer.Percentage;
            if (beer.AverageRating == 0)
            {
                updatedBeer.AverageRating = beerRating.Average;
            }
            else
            {
                updatedBeer.AverageRating = beerRating.Average;
            }


            var updateResult = await _beerService.UpdateBeer(beerId, updatedBeer);

            if (!updateResult)
            {
                return(BadRequest("Niepowodzenie :("));
            }

            return(Ok());
        }