Пример #1
0
        public async Task <AddorUpdateMovieRatingResponse> AddorUpdateMovieRating(AddMovieRating addMovieRating)
        {
            var addorUpdateMovieRatingResponse = new AddorUpdateMovieRatingResponse();
            var validateResult = _addMovieRatingValidator.Validate(addMovieRating);

            if (validateResult.Any())
            {
                addorUpdateMovieRatingResponse.Errors = validateResult.Select(x => new Error {
                    Code = x.ErrorCode, Message = x.ErrorMessage
                });
                return(addorUpdateMovieRatingResponse);
            }
            addorUpdateMovieRatingResponse = await _movieRepository.AddorUpdateMovieRating(addMovieRating);

            if (addorUpdateMovieRatingResponse.IsMovieorUserNotFound)
            {
                addorUpdateMovieRatingResponse.Errors = new[] { new Error {
                                                                    Code = ErrorCodes.Codes.NotFound.NotFound_MovieorUser, Message = ErrorCodes.Messages.NotFound.NotFound_MovieorUser
                                                                } };
                return(addorUpdateMovieRatingResponse);
            }
            if (!addorUpdateMovieRatingResponse.Success)
            {
                addorUpdateMovieRatingResponse.Errors = new[] { new Error {
                                                                    Code = ErrorCodes.Codes.ServerError.ServerError_UpsertFailedForRating, Message = ErrorCodes.Messages.ServerError.ServerError_UpsertFailedForRating
                                                                } };
            }
            return(addorUpdateMovieRatingResponse);
        }
Пример #2
0
        public async Task <AddorUpdateMovieRatingResponse> AddorUpdateMovieRating(AddMovieRating addMovieRating)
        {
            var addorUpdateMovieRatingResponse = new AddorUpdateMovieRatingResponse();

            try
            {
                var movieID = _context.Movies.FirstOrDefault(m => string.Equals(m.Title, addMovieRating.Title, StringComparison.InvariantCultureIgnoreCase))?.MovieID;
                var userID  = _context.Users.FirstOrDefault(u => string.Equals(u.UserName, addMovieRating.UserName, StringComparison.InvariantCultureIgnoreCase))?.UserID;

                if (movieID == null || userID == null)
                {
                    addorUpdateMovieRatingResponse.IsMovieorUserNotFound = true;
                    return(addorUpdateMovieRatingResponse);
                }
                var movieResult = _context.Ratings.FirstOrDefault(x => x.MovieID == movieID && x.UserID == userID);
                var result      = 0;
                if (movieResult == null)
                {
                    _context.Ratings.Add(new Rating {
                        MovieID = (int)movieID, UserID = (int)userID, MovieRating = addMovieRating.MovieRating
                    });
                    result = await _context.SaveChangesAsync();
                }
                else
                {
                    if (movieResult.MovieRating != addMovieRating.MovieRating)
                    {
                        movieResult.MovieRating = addMovieRating.MovieRating;
                        result = await _context.SaveChangesAsync();
                    }
                    else
                    {
                        result = 1;
                    }
                }

                addorUpdateMovieRatingResponse.Success = result > 0 ? true : false;
            }
            catch
            {
                addorUpdateMovieRatingResponse.Success = false;
            }
            return(addorUpdateMovieRatingResponse);
        }