Ejemplo n.º 1
0
        public async Task <ActionResult <Word> > UpdateWord(int wordId, [FromBody] SaveWordDto saveWordDto)
        {
            var validator        = new SaveWordDtoValidator();
            var validationResult = await validator.ValidateAsync(saveWordDto);

            if (!validationResult.IsValid)
            {
                foreach (ValidationFailure failure in validationResult.Errors)
                {
                    return(BadRequest(failure.ErrorMessage));
                }
            }

            var wordToupdate = await _wordService.GetWordById(wordId);

            if (wordToupdate == null)
            {
                return(NotFound());
            }

            var word = _mapper.Map <SaveWordDto, Word>(saveWordDto);

            await _wordService.UpdateWord(wordToupdate, word);

            var updatedWord = await _wordService.GetWordById(wordId);

            var updatedWordDto = _mapper.Map <Word, WordDto>(updatedWord);

            return(Ok(updatedWordDto));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult <Word> > CreateWord(int vocabularyId, [FromBody] SaveWordDto saveWordDto)
        {
            saveWordDto.VocabularyId = vocabularyId;

            var validator        = new SaveWordDtoValidator();
            var validationResult = await validator.ValidateAsync(saveWordDto);

            if (!validationResult.IsValid)
            {
                foreach (ValidationFailure failure in validationResult.Errors)
                {
                    return(BadRequest(failure.ErrorMessage));
                }
            }

            var wordToCreate = _mapper.Map <SaveWordDto, Word>(saveWordDto);
            var newWord      = await _wordService.CreateWord(wordToCreate);

            var newWordDto = _mapper.Map <Word, WordDto>(newWord);

            return(Ok(newWordDto));
        }