From cfdb9a661853b869e613a61611fe6f96114ad124 Mon Sep 17 00:00:00 2001 From: Robin Munn Date: Fri, 19 Jul 2024 13:10:31 +0700 Subject: [PATCH] Use GraphQL queries in UI, remove HTTP controllers Now the UI updates itself when the GraphQL query is run, and the GraphQL cache is automatically updated with the correct values. --- .../Controllers/ProjectController.cs | 62 ------------ .../project/[project_code]/+page.svelte | 9 +- .../project/[project_code]/+page.ts | 95 +++++++++++++++++++ 3 files changed, 99 insertions(+), 67 deletions(-) diff --git a/backend/LexBoxApi/Controllers/ProjectController.cs b/backend/LexBoxApi/Controllers/ProjectController.cs index dd5e8d631..c64f32645 100644 --- a/backend/LexBoxApi/Controllers/ProjectController.cs +++ b/backend/LexBoxApi/Controllers/ProjectController.cs @@ -232,68 +232,6 @@ private async Task StreamHttpResponse(HttpContent hgResult) await hgResult.CopyToAsync(writer.AsStream()); } - [HttpPost("updateLexEntryCount/{code}")] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status404NotFound)] - [ProducesDefaultResponseType] - public async Task> UpdateLexEntryCount(string code) - { - var result = await projectService.UpdateLexEntryCount(code); - return result is null ? NotFound() : result; - } - - [HttpPost("updateLanguageList/{code}")] - [ProducesResponseType(StatusCodes.Status200OK)] - [ProducesResponseType(StatusCodes.Status404NotFound)] - [ProducesDefaultResponseType] - public async Task UpdateLanguageList(string code) - { - var projectId = await projectService.LookupProjectId(code); - await projectService.UpdateProjectLangTags(projectId); - } - - [HttpPost("updateMissingLanguageList")] - public async Task> UpdateMissingLanguageList(int limit = 10) - { - var projects = lexBoxDbContext.Projects - .Include(p => p.FlexProjectMetadata) - .Where(p => p.Type == ProjectType.FLEx && p.LastCommit != null && p.FlexProjectMetadata!.WritingSystems == null) - .Take(limit) - .AsAsyncEnumerable(); - var codes = new List(limit); - await foreach (var project in projects) - { - codes.Add(project.Code); - project.FlexProjectMetadata ??= new FlexProjectMetadata(); - project.FlexProjectMetadata.WritingSystems = await hgService.GetProjectWritingSystems(project.Code); - } - - await lexBoxDbContext.SaveChangesAsync(); - - return Ok(codes); - } - - [HttpPost("updateMissingLangProjectId")] - public async Task> UpdateMissingLangProjectId(int limit = 10) - { - var projects = lexBoxDbContext.Projects - .Include(p => p.FlexProjectMetadata) - .Where(p => p.Type == ProjectType.FLEx && p.LastCommit != null && p.FlexProjectMetadata!.LangProjectId == null) - .Take(limit) - .AsAsyncEnumerable(); - var codes = new List(limit); - await foreach (var project in projects) - { - codes.Add(project.Code); - project.FlexProjectMetadata ??= new FlexProjectMetadata(); - project.FlexProjectMetadata.LangProjectId = await hgService.GetProjectIdOfFlexProject(project.Code); - } - - await lexBoxDbContext.SaveChangesAsync(); - - return Ok(codes); - } - [HttpPost("queueUpdateProjectMetadataTask")] public async Task QueueUpdateProjectMetadataTask(string projectCode) { diff --git a/frontend/src/routes/(authenticated)/project/[project_code]/+page.svelte b/frontend/src/routes/(authenticated)/project/[project_code]/+page.svelte index c7610baa4..ddd044c60 100644 --- a/frontend/src/routes/(authenticated)/project/[project_code]/+page.svelte +++ b/frontend/src/routes/(authenticated)/project/[project_code]/+page.svelte @@ -13,6 +13,8 @@ _changeProjectName, _deleteProjectUser, _leaveProject, + _updateProjectLanguageList, + _updateProjectLexEntryCount, type ProjectUser, } from './+page'; import AddProjectMember from './AddProjectMember.svelte'; @@ -63,7 +65,6 @@ return a.user.name.localeCompare(b.user.name); }); - let lexEntryCount: number | string | null | undefined = undefined; $: lexEntryCount = project.flexProjectMetadata?.lexEntryCount; $: vernacularLangTags = project.flexProjectMetadata?.writingSystems?.vernacularWss; $: analysisLangTags = project.flexProjectMetadata?.writingSystems?.analysisWss; @@ -75,17 +76,15 @@ let loadingEntryCount = false; async function updateEntryCount(): Promise { loadingEntryCount = true; - const response = await fetch(`/api/project/updateLexEntryCount/${project.code}`, {method: 'POST'}); - lexEntryCount = await response.text(); + await _updateProjectLexEntryCount(project.code); loadingEntryCount = false; } let loadingLanguageList = false; async function updateLanguageList(): Promise { loadingLanguageList = true; - await fetch(`/api/project/updateLanguageList/${project.code}`, {method: 'POST'}); + await _updateProjectLanguageList(project.code); loadingLanguageList = false; - await invalidate(`project:${project.code}`); } let resetProjectModal: ResetProjectModal; diff --git a/frontend/src/routes/(authenticated)/project/[project_code]/+page.ts b/frontend/src/routes/(authenticated)/project/[project_code]/+page.ts index 6f2666331..048dca4b0 100644 --- a/frontend/src/routes/(authenticated)/project/[project_code]/+page.ts +++ b/frontend/src/routes/(authenticated)/project/[project_code]/+page.ts @@ -18,6 +18,9 @@ import type { ProjectPageQuery, SetProjectConfidentialityInput, SetProjectConfidentialityMutation, + UpdateLangProjectIdMutation, + UpdateProjectLanguageListMutation, + UpdateProjectLexEntryCountMutation, } from '$lib/gql/types'; import { getClient, graphql } from '$lib/gql'; @@ -266,6 +269,98 @@ export async function _bulkAddProjectMembers(input: BulkAddProjectMembersInput): return result; } +export async function _updateProjectLexEntryCount(code: string): $OpResult { + //language=GraphQL + const result = await getClient() + .mutation( + graphql(` + mutation UpdateProjectLexEntryCount($input: UpdateProjectLexEntryCountInput!) { + updateProjectLexEntryCount(input: $input) { + project { + id + flexProjectMetadata { + lexEntryCount + } + } + errors { + __typename + ... on Error { + message + } + } + } + } + `), + { input: { code } }, + ); + return result; +} + +export async function _updateLangProjectId(code: string): $OpResult { + //language=GraphQL + const result = await getClient() + .mutation( + graphql(` + mutation UpdateLangProjectId($input: UpdateLangProjectIdInput!) { + updateLangProjectId(input: $input) { + project { + id + flexProjectMetadata { + langProjectId + } + } + errors { + __typename + ... on Error { + message + } + } + } + } + `), + { input: { code } }, + ); + return result; +} + +export async function _updateProjectLanguageList(code: string): $OpResult { + //language=GraphQL + const result = await getClient() + .mutation( + graphql(` + mutation UpdateProjectLanguageList($input: UpdateProjectLanguageListInput!) { + updateProjectLanguageList(input: $input) { + project { + id + flexProjectMetadata { + writingSystems { + analysisWss { + tag + isActive + isDefault + } + vernacularWss { + tag + isActive + isDefault + } + } + } + } + errors { + __typename + ... on Error { + message + } + } + } + } + `), + { input: { code } }, + ); + return result; +} + export async function _changeProjectMemberRole(input: ChangeProjectMemberRoleInput): $OpResult { //language=GraphQL const result = await getClient()