示例#1
0
        public async Task <IActionResult> UploadLabelTranslationFromCSVFile(UploadLabelTranslationFromCSVFileModel model)
        {
            if (model.IsNotValid())
            {
                model.SetInputModelValues();
                return(View(model));
            }

            var translationListInfos = new List <TranslationListInfo>();

            var lines = new List <string>();

            using (var reader = new StreamReader(model.CSVFile.OpenReadStream()))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    lines.Add(line);
                }
            }

            for (var i = 1; i < lines.Count; i++)
            {
                var values = lines[i].Split(',');
                if (values.Length != 2)
                {
                    model.ErrorMessages.Add("file_has_more_columns_than_expected");
                    model.ErrorMessages.Add("error line : " + i);
                    model.SetInputModelValues();
                    return(View(model));
                }

                translationListInfos.Add(new TranslationListInfo
                {
                    LanguageIsoCode2 = values[0],
                    Translation      = values[1]
                });
            }

            var request  = new LabelTranslationCreateListRequest(CurrentUser.Id, model.OrganizationUid, model.LabelUid, translationListInfos);
            var response = await _labelService.CreateTranslationFromList(request);

            if (response.Status.IsNotSuccess)
            {
                model.MapMessages(response);
                model.SetInputModelValues();
                return(View(model));
            }

            var doneModel = new TranslationUploadFromCSVDoneModel();

            doneModel.MapMessages(response);
            doneModel.LabelUid = model.LabelUid;
            doneModel.LabelKey = model.LabelKey;
            doneModel.AddedTranslationCount       = response.AddedTranslationCount;
            doneModel.CanNotAddedTranslationCount = response.CanNotAddedTranslationCount;
            doneModel.TotalRowsProcessed          = lines.Count - 1;

            CurrentUser.IsActionSucceed = true;
            return(View("UploadLabelTranslationFromCSVFileDone", doneModel));
        }