Exemplo n.º 1
0
        public async Task <IActionResult> Rate(RateDto rateDto)
        {
            var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (!(await _postService.Any(rateDto.PostId)))
            {
                return(new ObjectActionResult(success: false, statusCode: HttpStatusCode.NotFound, data: null));
            }

            if (await _rateService.Any(rateDto.PostId, userId))
            {
                var _rating = _rateService.GetRate(rateDto.PostId, userId);
                var rating  = _mapper.Map <Rating>(rateDto);
                rating.Id     = _rating.Id;
                rating.UserId = userId;
                var rate = _rateService.Update(rating);
                return(new ObjectActionResult(success: true, statusCode: HttpStatusCode.OK, data: rate));
            }
            else
            {
                var rating = _mapper.Map <Rating>(rateDto);
                rating.UserId = userId;
                var rate = _rateService.Save(rating);
                return(new ObjectActionResult(success: true, statusCode: HttpStatusCode.Created, data: rate));
            }
        }