Exemple #1
0
        public async Task <IHttpActionResult> AddToDictionary(string token, string value, [FromUri] string[] translations)
        {
            var resultWithData = await UserService.GetUserIdByTokenAsync(token);

            if (!resultWithData.Success)
            {
                return(BadRequest());
            }

            var userId = resultWithData.ResultData;

            if (String.IsNullOrEmpty(userId))
            {
                return(BadRequest());
            }

            var resultWithDataSourceLanguage = _languageService.GetIdByCode("en");
            var resultWithDataTargetLanguage = _languageService.GetIdByCode("ru");

            if (!resultWithDataSourceLanguage.Success || !resultWithDataTargetLanguage.Success)
            {
                return(InternalServerError());
            }

            // Create dictionary if not exists.
            var resultWithDataDictionary = await _dictionaryService.CreateForPluginAsync(
                userId,
                ConfigurationHelper.DictionaryNameForPluginRuEn,
                ConfigurationHelper.DictionaryDescriptionForPluginRuEn,
                resultWithDataSourceLanguage.ResultData,
                resultWithDataTargetLanguage.ResultData
                );

            if (!resultWithDataDictionary.Success)
            {
                return(InternalServerError());
            }

            translations = translations ?? new string[0];

            var validTranslations = translations
                                    .Where(t => !String.IsNullOrEmpty(t))
                                    .ToList();

            dynamic translation = new ExpandoObject();

            translation.DictionaryId = resultWithDataDictionary.ResultData;
            translation.Value        = value;
            translation.Translations = validTranslations;

            var result = await _translationService.CreateAsync(translation);

            if (!result.Success)
            {
                return(InternalServerError());
            }

            return(Ok(new { success = true }));
        }