コード例 #1
0
        public async Task <IRpcMethodResult> Create(SFProjectCreateSettings settings)
        {
            try
            {
                string projectId = await _projectService.CreateProjectAsync(UserId, settings);

                return(Ok(projectId));
            }
            catch (ForbiddenException)
            {
                return(ForbiddenError());
            }
            catch (DataNotFoundException dnfe)
            {
                return(NotFoundError(dnfe.Message));
            }
            catch (InvalidOperationException e)
            {
                return(InvalidParamsError(e.Message));
            }
        }
コード例 #2
0
        /// <summary>
        /// Returns SF project id of created project.
        /// </summary>
        public async Task <string> CreateProjectAsync(string curUserId, SFProjectCreateSettings settings)
        {
            Attempt <UserSecret> userSecretAttempt = await _userSecrets.TryGetAsync(curUserId);

            if (!userSecretAttempt.TryResult(out UserSecret userSecret))
            {
                throw new DataNotFoundException("The user does not exist.");
            }

            IReadOnlyList <ParatextProject> ptProjects = await _paratextService.GetProjectsAsync(userSecret);

            ParatextProject ptProject = ptProjects.SingleOrDefault(p => p.ParatextId == settings.ParatextId);

            if (ptProject == null)
            {
                throw new DataNotFoundException("The paratext project does not exist.");
            }

            var project = new SFProject
            {
                ParatextId    = settings.ParatextId,
                Name          = ptProject.Name,
                ShortName     = ptProject.ShortName,
                WritingSystem = new WritingSystem {
                    Tag = ptProject.LanguageTag
                },
                TranslateConfig = new TranslateConfig
                {
                    TranslationSuggestionsEnabled = settings.TranslationSuggestionsEnabled
                },
                CheckingConfig = new CheckingConfig
                {
                    CheckingEnabled = settings.CheckingEnabled
                }
            };
            Attempt <string> attempt = await TryGetProjectRoleAsync(project, curUserId);

            if (!attempt.TryResult(out string projectRole) || projectRole != SFProjectRole.Administrator)
            {
                throw new ForbiddenException();
            }

            string projectId = ObjectId.GenerateNewId().ToString();

            using (IConnection conn = await RealtimeService.ConnectAsync(curUserId))
            {
                if (this.RealtimeService.QuerySnapshots <SFProject>().Any(
                        (SFProject sfProject) => sfProject.ParatextId == project.ParatextId))
                {
                    throw new InvalidOperationException(ErrorAlreadyConnectedKey);
                }
                IDocument <SFProject> projectDoc = await conn.CreateAsync <SFProject>(projectId, project);

                await ProjectSecrets.InsertAsync(new SFProjectSecret { Id = projectDoc.Id });

                IDocument <User> userDoc = await conn.FetchAsync <User>(curUserId);
                await AddUserToProjectAsync(conn, projectDoc, userDoc, SFProjectRole.Administrator, false);

                // Add the source after the project has been created
                // This will make the source project appear after the target, if it needs to be created
                if (settings.SourceParatextId != null && settings.SourceParatextId != settings.ParatextId)
                {
                    TranslateSource source = await this.GetTranslateSourceAsync(
                        curUserId, userSecret, settings.SourceParatextId, ptProjects);

                    await projectDoc.SubmitJson0OpAsync(op =>
                    {
                        UpdateSetting(op, p => p.TranslateConfig.Source, source);
                    });
                }

                if (projectDoc.Data.TranslateConfig.TranslationSuggestionsEnabled)
                {
                    var machineProject = new MachineProject
                    {
                        Id = projectDoc.Id,
                        SourceLanguageTag = projectDoc.Data.TranslateConfig.Source.WritingSystem.Tag,
                        TargetLanguageTag = projectDoc.Data.WritingSystem.Tag
                    };
                    await _engineService.AddProjectAsync(machineProject);
                }
            }

            await _syncService.SyncAsync(curUserId, projectId, true);

            return(projectId);
        }