Пример #1
0
        public async Task <PhraseItem> UpdatePhraseAsync([NotNull] PutPhraseItemRequest request, [NotNull] ServerUser user,
                                                         [NotNull] PhraseItem existingPhrase)
        {
            var cloned = existingPhrase.Clone();

            cloned.Phrase      = string.IsNullOrWhiteSpace(request.Phrase) ? cloned.Phrase : request.Phrase;
            cloned.Complexity  = request.Complexity < 0 || request.Complexity > 0 ? cloned.Complexity : request.Complexity;
            cloned.Description = request.Description ?? cloned.Description;
            cloned.Version++;

            var reviewState = new ReviewState {
                Comment = request.Comment, State = State.Accept, UserId = user.Id
            };

            UpdateReviewStates(cloned, reviewState, user.Id, request.ClearReview);

            await CloseAndInsertAsync(cloned, existingPhrase, user.Id);

            return(cloned);
        }
Пример #2
0
        public async Task <IActionResult> Put(int trackId, [FromBody] PutPhraseItemRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(HandleAndReturnBadRequest(ModelState, _logger));
            }

            var user = await _userRepository.GetByNameAsync(request.Author);

            if (user == null)
            {
                return(HandleAndReturnBadRequest($"User {request.Author} is not found", _logger));
            }

            var actual = await _phraseRepository.GetLatestByTrackIdAsync(trackId);

            if (actual == null)
            {
                return(HandleAndReturnBadRequest($"There is no phrase with track id {trackId}", _logger));
            }

            if (actual.Version >= request.Version)
            {
                var users = await _userRepository.GetAllAsync();

                return(Conflict(new ErrorResponse("The phrase has a newer version",
                                                  new BasePhraseItemResponse(actual, users))));
            }

            var conflictedPhrase = await _phraseRepository.GetByNameExceptTrackIdAsync(request.Phrase, trackId);

            if (conflictedPhrase != null)
            {
                return(HandleAndReturnBadRequest(
                           $"The phrase with name {request.Phrase} exists in the pack {conflictedPhrase.Pack.Name}", _logger));
            }

            var phrase = await _phraseRepository.UpdatePhraseAsync(request, user, actual);

            return(Ok(new BasePhraseItemResponse(phrase)));
        }