コード例 #1
0
        public async Task <IActionResult> GetLabels(Guid token, Guid projectUid)
        {
            var result = new CommonResult();

            if (token.IsEmptyGuid() ||
                projectUid.IsEmptyGuid())
            {
                result.Messages.Add("token or projectUid is missing!");
                return(StatusCode(401, result));
            }

            var labelReadListRequest = new AllLabelReadListRequest(token, projectUid);
            var request  = new TokenValidateRequest(projectUid, token);
            var response = await _integrationService.ValidateToken(request);

            if (response.Status.IsNotSuccess)
            {
                result.Messages = response.ErrorMessages;
                result.Messages.Add("unauthorized");
                return(StatusCode(401, result));
            }

            var labelsResponse = await _labelService.GetLabelsWithTranslations(labelReadListRequest);

            if (labelsResponse.Status.IsNotSuccess)
            {
                result.Messages = labelsResponse.ErrorMessages;
                return(Json(result));
            }

            return(Json(labelsResponse.Labels));
        }
コード例 #2
0
        public async Task <IActionResult> GetMainLabels()
        {
            var result = new CommonResult();

            var labelReadListRequest = new AllLabelReadListRequest();
            var labelsResponse       = await _labelService.GetLabelsWithTranslations(labelReadListRequest);

            if (labelsResponse.Status.IsNotSuccess)
            {
                result.Messages = labelsResponse.ErrorMessages;
                return(Json(result));
            }

            return(Json(labelsResponse.Labels));
        }
コード例 #3
0
        public async Task <IActionResult> DownloadLabels(Guid id)
        {
            var projectUid = id;

            if (projectUid.IsEmptyGuid())
            {
                return(NoContent());
            }

            var request = new AllLabelReadListRequest(CurrentUser.Id, projectUid);

            request.IsAddLabelsNotTranslated = true;
            var response = await _labelService.GetLabelsWithTranslations(request);

            if (response.Status.IsNotSuccess)
            {
                return(NoContent());
            }

            var stringBuilder = new StringBuilder();

            stringBuilder.AppendLine("label_key,language,translation");
            for (var i = 0; i < response.Labels.Count; i++)
            {
                var label = response.Labels[i];
                if (!label.Translations.Any())
                {
                    stringBuilder.AppendLine($"{label.Key},en,{label.Key}");
                    continue;
                }

                for (var j = 0; j < label.Translations.Count; j++)
                {
                    var translation = label.Translations[j];
                    stringBuilder.AppendLine($"{label.Key},{translation.LanguageIsoCode2},{translation.Translation}");
                }
            }

            CurrentUser.IsActionSucceed = true;
            return(File(Encoding.UTF8.GetBytes(stringBuilder.ToString()), "text/csv", "labels.csv"));
        }
コード例 #4
0
ファイル: LabelService.cs プロジェクト: vandriiv/translation
        public async Task <AllLabelReadListResponse> GetLabelsWithTranslations(AllLabelReadListRequest request)
        {
            var response = new AllLabelReadListResponse();

            Project project;

            if (request.IsDefaultProject)
            {
                project = await _projectRepository.Select(x => x.IsSuperProject);
            }
            else
            {
                if (request.Token.IsNotEmptyGuid() && request.CurrentUserId > 0)
                {
                    response.SetInvalid();
                    return(response);
                }

                if (request.CurrentUserId > 0)
                {
                    project = await _projectRepository.Select(x => x.Uid == request.ProjectUid && x.IsActive);

                    if (project.IsNotExist())
                    {
                        response.SetInvalid();
                        response.ErrorMessages.Add("project_not_found");
                        return(response);
                    }

                    var currentUser = _cacheManager.GetCachedCurrentUser(request.CurrentUserId);
                    if (currentUser != null)
                    {
                        if (currentUser.OrganizationId != project.OrganizationId)
                        {
                            response.SetInvalid();
                            return(response);
                        }
                    }
                }
                else
                {
                    var now   = DateTime.UtcNow;
                    var token = await _tokenRepository.Select(x => x.AccessToken == request.Token && x.ExpiresAt > now);

                    if (token.IsNotExist())
                    {
                        response.SetInvalid();
                        return(response);
                    }

                    if (request.IsDefaultProject)
                    {
                        project = await _projectRepository.Select(x => x.Name == "Default" && x.OrganizationId == token.OrganizationId && x.IsActive);
                    }
                    else
                    {
                        project = await _projectRepository.Select(x => x.Uid == request.ProjectUid && x.IsActive);
                    }

                    if (project.IsNotExist())
                    {
                        response.SetInvalid();
                        response.ErrorMessages.Add("project_not_found");
                        return(response);
                    }

                    if (token.OrganizationId != project.OrganizationId)
                    {
                        response.SetInvalid();
                        return(response);
                    }
                }

                if (await _organizationRepository.Any(x => x.Id == project.OrganizationId && !x.IsActive))
                {
                    response.SetInvalid();
                    return(response);
                }
            }

            var translations = await _labelTranslationRepository.SelectAll(x => x.ProjectId == project.Id && x.IsActive);

            var languages = await _languageRepository.SelectAll(null);

            var entities = await _labelRepository.SelectAll(x => x.ProjectId == project.Id && x.IsActive);

            if (translations != null)
            {
                for (var i = 0; i < entities.Count; i++)
                {
                    var entity            = entities[i];
                    var labelTranslations = translations.Where(x => x.LabelId == entity.Id).ToList();
                    if (!labelTranslations.Any() &&
                        !request.IsAddLabelsNotTranslated)
                    {
                        continue;
                    }

                    var dto = new LabelFatDto
                    {
                        Uid = entity.Uid,
                        Key = entity.Key
                    };

                    for (var j = 0; j < labelTranslations.Count; j++)
                    {
                        var labelTranslation = labelTranslations[j];
                        var language         = languages.Find(x => x.Id == labelTranslation.LanguageId);
                        if (language == null)
                        {
                            continue;
                        }

                        dto.Translations.Add(new LabelTranslationSlimDto
                        {
                            LabelKey         = entity.Key,
                            Translation      = labelTranslation.Translation,
                            LanguageIsoCode2 = language.IsoCode2Char
                        });
                    }

                    response.Labels.Add(dto);
                }
            }

            response.Status = ResponseStatus.Success;
            return(response);
        }
コード例 #5
0
        public static AllLabelReadListRequest GetAllLabelReadListRequest()
        {
            var request = new AllLabelReadListRequest(CurrentUserId, UidOne);

            return(request);
        }