// GET: Translations/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var translation = await _translationRepository.GetTranslationDetailsAsync(id);

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

            return(View(translation));
        }
Esempio n. 2
0
        public async Task <IActionResult> Translate([Bind("Text,Id")] TranslatorViewModel model)
        {
            if (ModelState.IsValid)
            {
                var language = await _translationRepository.GetTranslationDetailsAsync(model.Id);

                var response = await _client.GetResponseAsync(model.Text, language.Language);

                var resString = await response.Content.ReadAsStringAsync();

                if (response.IsSuccessStatusCode)
                {
                    var success = JsonConvert.DeserializeObject <SuccessRootDto>(resString);

                    var successModel = new SuccessResponse
                    {
                        Translation = success.Contents.Translation,
                        Text        = success.Contents.Text,
                        Translated  = success.Contents.Translated
                    };

                    await _translatorRepository.AddResponseAsync(model.Text, model.Id, successModel, null);

                    return(RedirectToAction("Details", new { id = successModel.QueryId }));
                }
                else
                {
                    var error = JsonConvert.DeserializeObject <ErrorRootDto>(resString);

                    var errorModel = new ErrorResponse
                    {
                        Code    = error.Error.Code,
                        Message = error.Error.Message
                    };

                    await _translatorRepository.AddResponseAsync(model.Text, model.Id, null, errorModel);

                    return(RedirectToAction("Details", new { id = errorModel.QueryId }));
                }
            }

            return(View(model));
        }