Ejemplo n.º 1
0
        public async Task <int> UpdateRatingAsync(int ratingId, AddBeerRatingDto ratingDto)
        {
            var rating = await _repository.GetAsync(ratingId);

            _mapper.Map(ratingDto, rating);
            await _repository.SaveAsync();

            return(rating.BeerId);
        }
Ejemplo n.º 2
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.º 3
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());
        }
Ejemplo n.º 4
0
        public async Task <GetBeerRatingDto> AddBeerRatingAsync(int userLogged, int beerId, AddBeerRatingDto beerRatingDto)
        {
            var rating = _mapper.Map <AddBeerRatingDto, BeerRating>(beerRatingDto);

            rating.BeerId = beerId;
            rating.UserId = userLogged;
            await _repository.AddAsyn(rating);

            await _repository.SaveAsync();

            var result = await GetBeerRaitingForUserAsync(userLogged, beerId);

            return(result);
        }