示例#1
0
        public static LabelUploadFromCSVModel MapLabelUploadFromCSVModel(ProjectDto project)
        {
            var model = new LabelUploadFromCSVModel();

            model.OrganizationUid = project.OrganizationUid;
            model.ProjectUid      = project.Uid;
            model.ProjectName     = project.Name;

            model.SetInputModelValues();
            return(model);
        }
示例#2
0
        public async Task <IActionResult> UploadLabelFromCSVFile(LabelUploadFromCSVModel model)
        {
            if (model.IsNotValid())
            {
                model.SetInputModelValues();
                return(View(model));
            }

            var labelListInfos = new List <LabelListInfo>();

            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 != 3)
                {
                    model.ErrorMessages.Add("file_has_more_columns_than_expected");
                    model.ErrorMessages.Add("error line : " + i);
                    model.SetInputModelValues();
                    return(View(model));
                }

                labelListInfos.Add(new LabelListInfo
                {
                    LabelKey         = values[0],
                    LanguageIsoCode2 = values[1],
                    Translation      = values[2]
                });
            }

            var request  = new LabelCreateListRequest(CurrentUser.Id, model.OrganizationUid, model.ProjectUid, labelListInfos);
            var response = await _labelService.CreateLabelFromList(request);

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

            var doneModel = new LabelUploadFromCSVDoneModel();

            doneModel.MapMessages(response);
            doneModel.ProjectUid                       = model.ProjectUid;
            doneModel.ProjectName                      = model.ProjectName;
            doneModel.AddedLabelCount                  = response.AddedLabelCount;
            doneModel.CanNotAddedLabelCount            = response.CanNotAddedLabelCount;
            doneModel.AddedLabelTranslationCount       = response.AddedLabelTranslationCount;
            doneModel.CanNotAddedLabelTranslationCount = response.CanNotAddedLabelTranslationCount;
            doneModel.TotalRowsProcessed               = lines.Count - 1;

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