Exemple #1
0
        public async Task <IActionResult> DeleteParserWord(string name)
        {
            if (!ModelState.IsValid || string.IsNullOrEmpty(name))
            {
                return(BadRequest(new
                {
                    status = StatusCodes.Status400BadRequest,
                    message = "Request is not correct."
                }));
            }

            try
            {
                ParserWord parserWord = await Task.Run(() => _wordService.DeleteParserWord(name));

                ParserWordDTO parserWordDTO = _mapper.Map <ParserWordDTO>(parserWord);

                return(Ok(new
                {
                    status = StatusCodes.Status200OK,
                    message = $"The <{name}> ParserWord record is deleted.",
                    data = parserWordDTO
                }));
            }
            catch (Exception ex)
            {
                return(BadRequest(new
                {
                    status = StatusCodes.Status400BadRequest,
                    message = ex.Message
                }));
            }
        }
Exemple #2
0
        public async Task <IActionResult> AddParserWord([FromBody] ParserWordDTO word)
        {
            if (!ModelState.IsValid || word == null)
            {
                return(BadRequest(new
                {
                    status = StatusCodes.Status400BadRequest,
                    message = "WordParserDTO request object is not correct."
                }));
            }

            try
            {
                ParserWord parserWord = _mapper.Map <ParserWord>(word);

                await Task.Run(() => _wordService.AddParserWord(parserWord));

                return(Ok(new
                {
                    status = StatusCodes.Status201Created,
                    message = "The new record of the ParserWords table was successfully added.",
                    data = word
                }));
            }
            catch (Exception ex)
            {
                return(BadRequest(new
                {
                    status = StatusCodes.Status400BadRequest,
                    message = ex.Message
                }));
            }
        }
        public IEnumerable <ParserWordDTO> AddParserWordsFromRow(SubtitleRow row)
        {
            if (row == null || string.IsNullOrEmpty(row.Value))
            {
                return(null);
            }

            string[] words;

            List <ParserWordDTO> wordsDto      = new List <ParserWordDTO>();
            List <ParserWord>    wordsToCreate = new List <ParserWord>();

            if (TryParseWords(row.Value, out words))
            {
                string language      = row.LanguageName;
                int    subtitleRowId = row.Id;

                foreach (string word in words)
                {
                    ParserWord newWord = new ParserWord
                    {
                        Name          = word,
                        LanguageName  = language,
                        SubtitleRowId = subtitleRowId
                    };

                    wordsToCreate.Add(newWord);

                    ParserWordDTO newWordDto = new ParserWordDTO()
                    {
                        Name          = word,
                        LanguageName  = language,
                        SubtitleRowId = subtitleRowId
                    };

                    wordsDto.Add(newWordDto);
                }

                AddRangeParserWords(wordsToCreate);

                IEnumerable <ParserWordDTO> results = (IEnumerable <ParserWordDTO>)wordsDto;

                return(results);
            }

            return(null);
        }
Exemple #4
0
        public async Task <IActionResult> GetParserWord(string name)
        {
            _logger.Info($"GET Action: Attempt to get ParserWord record by \"{name}\"");

            if (!ModelState.IsValid)
            {
                _logger.Error("ModelState is not valid.");
                _logger.Error("400 StatusCode is generated.");

                return(BadRequest(BaseStatusDto.CreateErrorDto("Bad request. Model state is not valid.")));
            }

            try
            {
                _logger.Debug("Call ParserWordService for the request handling.");

                ParserWord word = _wordService.GetParserWord(name);

                if (word == null)
                {
                    _logger.Debug("The ParserWord record with Name = \"{name}\" is not found.");
                    _logger.Debug("404 StatusCode is generated.");

                    return(NotFound(BaseStatusDto.CreateErrorDto(ERR_ID_NOT_FOUND + $"\"{name}\"")));
                }

                _logger.Debug("The ParserWord record with Name = \"{name}\" is found.");
                _logger.Info("Database record from ParserWords is safely returned to the client side.");
                _logger.Debug("200 StatusCode is generated.");

                ParserWordDTO wordDTO = _mapper.Map <ParserWordDTO>(word);
                wordDTO.CreateSuccess("GET request succeeds.");

                return(Ok(wordDTO));
            }
            catch (Exception ex)
            {
                _logger.Error($"{ex.GetType()} exception is generated.");
                _logger.Error($"{ex.Message}");

                return(BadRequest(BaseStatusDto.CreateErrorDto(ex.Message)));
            }
        }
Exemple #5
0
        public async Task <IActionResult> PutInsertOrUpdateParserWord([FromBody] ParserWordDTO word)
        {
            if (!ModelState.IsValid || word == null)
            {
                return(BadRequest(BaseStatusDto.CreateErrorDto("WordParserDTO request object is not correct.")));
            }

            try
            {
                ParserWord parserWord = _mapper.Map <ParserWord>(word);

                await Task.Run(() => _wordService.InsertOrUpdateParserWord(parserWord));

                ParserWordDTO parserWordDto = _mapper.Map <ParserWordDTO>(parserWord);

                return(Ok(parserWordDto));
            }
            catch (Exception ex)
            {
                return(BadRequest(BaseStatusDto.CreateErrorDto(ex.Message)));
            }
        }