Esempio n. 1
0
        public async Task <IActionResult> Vote(Vote vote)
        {
            if (vote.Rating > 5 || vote.Rating < 0)
            {
                return(ValidationProblem());
            }
            var userId = User.FindFirst("userId");

            vote.User_Id = userId.Value;
            var result = await _dataAccess.InsertVoteAsync(vote);

            if (result)
            {
                _caching.InsertVoteAsync(vote);
                var pic = await _caching.GetPhotoAsync(vote.Picture_Id);

                if (pic != null)
                {
                    pic.Total_Rating += vote.Rating;
                    pic.Votes++;
                }
                else
                {
                    pic = await _dataAccess.GetPictureAsync(vote.Picture_Id);
                }

                _search.UpdateScoreAsync(vote.Picture_Id, pic.Total_Rating, pic.Votes);
                _caching.UpdatePictureAsync(pic);
                return(Ok(new
                {
                    average = pic.Rating,
                    count = pic.Votes
                }));
            }
            else
            {
                return(ValidationProblem());
            }
        }