Skip to content

Commit

Permalink
Use GraphQL queries in UI, remove HTTP controllers
Browse files Browse the repository at this point in the history
Now the UI updates itself when the GraphQL query is run, and the GraphQL
cache is automatically updated with the correct values.
  • Loading branch information
rmunn committed Jul 23, 2024
1 parent e9f56d0 commit cfdb9a6
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 67 deletions.
62 changes: 0 additions & 62 deletions backend/LexBoxApi/Controllers/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActionResult<int>> 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<ActionResult<string[]>> 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<string>(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<ActionResult<string[]>> 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<string>(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<ActionResult> QueueUpdateProjectMetadataTask(string projectCode)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
_changeProjectName,
_deleteProjectUser,
_leaveProject,
_updateProjectLanguageList,
_updateProjectLexEntryCount,
type ProjectUser,
} from './+page';
import AddProjectMember from './AddProjectMember.svelte';
Expand Down Expand Up @@ -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;
Expand All @@ -75,17 +76,15 @@
let loadingEntryCount = false;
async function updateEntryCount(): Promise<void> {
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<void> {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import type {
ProjectPageQuery,
SetProjectConfidentialityInput,
SetProjectConfidentialityMutation,
UpdateLangProjectIdMutation,
UpdateProjectLanguageListMutation,
UpdateProjectLexEntryCountMutation,
} from '$lib/gql/types';
import { getClient, graphql } from '$lib/gql';

Expand Down Expand Up @@ -266,6 +269,98 @@ export async function _bulkAddProjectMembers(input: BulkAddProjectMembersInput):
return result;
}

export async function _updateProjectLexEntryCount(code: string): $OpResult<UpdateProjectLexEntryCountMutation> {
//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<UpdateLangProjectIdMutation> {
//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<UpdateProjectLanguageListMutation> {
//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<ChangeProjectMemberRoleMutation> {
//language=GraphQL
const result = await getClient()
Expand Down

0 comments on commit cfdb9a6

Please sign in to comment.