예제 #1
0
        public async Task <object> AddEditRating(RatingRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(JsonResults.Error(400, ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage.ToString()));
            }

            try
            {
                var checkRating = await _ratingService.CheckRating(request);

                if (!checkRating)
                {
                    var model = new Rating
                    {
                        UserId    = request.UserId,
                        ServiceId = request.ServiceId,
                        Rating1   = request.Rating1
                    };

                    await _ratingService.AddRating(model);

                    return(JsonResults.Success(model));
                }

                var rating = await _ratingService.GetRating(request.Id);

                rating.Rating1 = request.Rating1;

                await _ratingService.UpdateRating(rating);

                return(JsonResults.Success(rating));
            }
            catch (Exception ex)
            {
                return(JsonResults.Error(400, ex.Message));
            }
        }