protected override async Task <SFProjectEntity> InsertEntityAsync(SFProjectEntity entity) { entity = await base.InsertEntityAsync(entity); if (entity.TranslateConfig.Enabled) { var project = new Project { Id = entity.Id, SourceLanguageTag = entity.TranslateConfig.SourceInputSystem.Tag, TargetLanguageTag = entity.InputSystem.Tag }; await _engineService.AddProjectAsync(project); } var job = new SyncJobEntity() { Id = entity.ActiveSyncJobRef, ProjectRef = entity.Id, OwnerRef = UserId }; await _syncJobManager.StartAsync(job); return(entity); }
public async Task <IActionResult> CreateAsync([FromBody] ProjectDto newProject) { var project = new Project { Id = newProject.Id, SourceLanguageTag = newProject.SourceLanguageTag, TargetLanguageTag = newProject.TargetLanguageTag, SourceSegmentType = newProject.SourceSegmentType, TargetSegmentType = newProject.TargetSegmentType, IsShared = newProject.IsShared }; if (!await AuthorizeAsync(project, Operations.Create)) { return(StatusCode(StatusCodes.Status403Forbidden)); } bool created = await _engineService.AddProjectAsync(project); if (!created) { return(StatusCode(409)); } ProjectDto dto = CreateDto(project); return(Created(dto.Href, dto)); }
/// <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); }