From 76f084133ce506fd66f5d9247e28a85b13309e41 Mon Sep 17 00:00:00 2001 From: buchananwill <139961977+buchananwill@users.noreply.github.com> Date: Wed, 10 Apr 2024 21:43:03 +0100 Subject: [PATCH] Generating endpoints for the 10 base crud endpoints in all concrete classes. --- .../READ-ONLY-generated-actions/Carousel.ts | 8 +- .../CarouselGroup.ts | 23 +++++ .../CarouselGroupOption.ts | 23 +++++ .../CarouselOption.ts | 23 +++++ .../CarouselOrder.ts | 23 +++++ .../CarouselOrderItem.ts | 23 +++++ .../Organization.ts | 23 +++++ .../WorkProjectSeriesSchema.ts | 23 +++++ .../WorkSeriesBundleAssignment.ts | 23 +++++ .../WorkSeriesSchemaBundle.ts | 23 +++++ .../WorkTaskType.ts | 23 +++++ app/api/actions/template-actions.ts | 15 ++++ app/api/actions/template-endpoints.ts | 87 +++++++++++++++++-- app/api/main.ts | 1 + .../class-hierarchy/[yearGroup]/page.tsx | 19 ++-- .../bundle-items-context-provider.tsx | 14 +-- 16 files changed, 344 insertions(+), 30 deletions(-) create mode 100644 app/api/READ-ONLY-generated-actions/CarouselGroup.ts create mode 100644 app/api/READ-ONLY-generated-actions/CarouselGroupOption.ts create mode 100644 app/api/READ-ONLY-generated-actions/CarouselOption.ts create mode 100644 app/api/READ-ONLY-generated-actions/CarouselOrder.ts create mode 100644 app/api/READ-ONLY-generated-actions/CarouselOrderItem.ts create mode 100644 app/api/READ-ONLY-generated-actions/Organization.ts create mode 100644 app/api/READ-ONLY-generated-actions/WorkProjectSeriesSchema.ts create mode 100644 app/api/READ-ONLY-generated-actions/WorkSeriesBundleAssignment.ts create mode 100644 app/api/READ-ONLY-generated-actions/WorkSeriesSchemaBundle.ts create mode 100644 app/api/READ-ONLY-generated-actions/WorkTaskType.ts diff --git a/app/api/READ-ONLY-generated-actions/Carousel.ts b/app/api/READ-ONLY-generated-actions/Carousel.ts index db1244b..31e8ebc 100644 --- a/app/api/READ-ONLY-generated-actions/Carousel.ts +++ b/app/api/READ-ONLY-generated-actions/Carousel.ts @@ -6,7 +6,13 @@ export const { getPage, deleteIdList, postList, - putList + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList } = generateBaseEndpointSet< CarouselDto, string diff --git a/app/api/READ-ONLY-generated-actions/CarouselGroup.ts b/app/api/READ-ONLY-generated-actions/CarouselGroup.ts new file mode 100644 index 0000000..0c5d029 --- /dev/null +++ b/app/api/READ-ONLY-generated-actions/CarouselGroup.ts @@ -0,0 +1,23 @@ +'use server'; +import { generateBaseEndpointSet } from '../actions/template-endpoints'; +import { CarouselGroupDto } from '../dtos/CarouselGroupDtoSchema'; + +export const { + getPage, + deleteIdList, + postList, + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList +} = generateBaseEndpointSet< + CarouselGroupDto, + string +>( + '/api/v2/carouselGroups' +); + + diff --git a/app/api/READ-ONLY-generated-actions/CarouselGroupOption.ts b/app/api/READ-ONLY-generated-actions/CarouselGroupOption.ts new file mode 100644 index 0000000..ee08486 --- /dev/null +++ b/app/api/READ-ONLY-generated-actions/CarouselGroupOption.ts @@ -0,0 +1,23 @@ +'use server'; +import { generateBaseEndpointSet } from '../actions/template-endpoints'; +import { CarouselGroupOptionDto } from '../dtos/CarouselGroupOptionDtoSchema'; + +export const { + getPage, + deleteIdList, + postList, + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList +} = generateBaseEndpointSet< + CarouselGroupOptionDto, + number +>( + '/api/v2/carouselGroups/options' +); + + diff --git a/app/api/READ-ONLY-generated-actions/CarouselOption.ts b/app/api/READ-ONLY-generated-actions/CarouselOption.ts new file mode 100644 index 0000000..36a6185 --- /dev/null +++ b/app/api/READ-ONLY-generated-actions/CarouselOption.ts @@ -0,0 +1,23 @@ +'use server'; +import { generateBaseEndpointSet } from '../actions/template-endpoints'; +import { CarouselOptionDto } from '../dtos/CarouselOptionDtoSchema'; + +export const { + getPage, + deleteIdList, + postList, + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList +} = generateBaseEndpointSet< + CarouselOptionDto, + number +>( + '/api/v2/carouselGroups/carousels/options' +); + + diff --git a/app/api/READ-ONLY-generated-actions/CarouselOrder.ts b/app/api/READ-ONLY-generated-actions/CarouselOrder.ts new file mode 100644 index 0000000..65bcd48 --- /dev/null +++ b/app/api/READ-ONLY-generated-actions/CarouselOrder.ts @@ -0,0 +1,23 @@ +'use server'; +import { generateBaseEndpointSet } from '../actions/template-endpoints'; +import { CarouselOrderDto } from '../dtos/CarouselOrderDtoSchema'; + +export const { + getPage, + deleteIdList, + postList, + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList +} = generateBaseEndpointSet< + CarouselOrderDto, + string +>( + '/api/v2/carouselGroups/orders' +); + + diff --git a/app/api/READ-ONLY-generated-actions/CarouselOrderItem.ts b/app/api/READ-ONLY-generated-actions/CarouselOrderItem.ts new file mode 100644 index 0000000..d83deab --- /dev/null +++ b/app/api/READ-ONLY-generated-actions/CarouselOrderItem.ts @@ -0,0 +1,23 @@ +'use server'; +import { generateBaseEndpointSet } from '../actions/template-endpoints'; +import { CarouselOrderItemDto } from '../dtos/CarouselOrderItemDtoSchema'; + +export const { + getPage, + deleteIdList, + postList, + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList +} = generateBaseEndpointSet< + CarouselOrderItemDto, + number +>( + '/api/v2/carousels/orders/items' +); + + diff --git a/app/api/READ-ONLY-generated-actions/Organization.ts b/app/api/READ-ONLY-generated-actions/Organization.ts new file mode 100644 index 0000000..99f31f4 --- /dev/null +++ b/app/api/READ-ONLY-generated-actions/Organization.ts @@ -0,0 +1,23 @@ +'use server'; +import { generateBaseEndpointSet } from '../actions/template-endpoints'; +import { Long } from '../dtos/LongSchema'; + +export const { + getPage, + deleteIdList, + postList, + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList +} = generateBaseEndpointSet< + Long, + number +>( + '/api/v2/workProjectSeriesSchemas/bundleAssignments' +); + + diff --git a/app/api/READ-ONLY-generated-actions/WorkProjectSeriesSchema.ts b/app/api/READ-ONLY-generated-actions/WorkProjectSeriesSchema.ts new file mode 100644 index 0000000..66a7a89 --- /dev/null +++ b/app/api/READ-ONLY-generated-actions/WorkProjectSeriesSchema.ts @@ -0,0 +1,23 @@ +'use server'; +import { generateBaseEndpointSet } from '../actions/template-endpoints'; +import { WorkProjectSeriesSchemaDto } from '../dtos/WorkProjectSeriesSchemaDtoSchema'; + +export const { + getPage, + deleteIdList, + postList, + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList +} = generateBaseEndpointSet< + WorkProjectSeriesSchemaDto, + string +>( + '/api/v2/workProjectSeriesSchemas' +); + + diff --git a/app/api/READ-ONLY-generated-actions/WorkSeriesBundleAssignment.ts b/app/api/READ-ONLY-generated-actions/WorkSeriesBundleAssignment.ts new file mode 100644 index 0000000..b9fd5b8 --- /dev/null +++ b/app/api/READ-ONLY-generated-actions/WorkSeriesBundleAssignment.ts @@ -0,0 +1,23 @@ +'use server'; +import { generateBaseEndpointSet } from '../actions/template-endpoints'; +import { WorkSeriesBundleAssignmentDto } from '../dtos/WorkSeriesBundleAssignmentDtoSchema'; + +export const { + getPage, + deleteIdList, + postList, + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList +} = generateBaseEndpointSet< + WorkSeriesBundleAssignmentDto, + number +>( + '/api/v2/workProjectSeriesSchemas/bundleAssignments' +); + + diff --git a/app/api/READ-ONLY-generated-actions/WorkSeriesSchemaBundle.ts b/app/api/READ-ONLY-generated-actions/WorkSeriesSchemaBundle.ts new file mode 100644 index 0000000..cd0861a --- /dev/null +++ b/app/api/READ-ONLY-generated-actions/WorkSeriesSchemaBundle.ts @@ -0,0 +1,23 @@ +'use server'; +import { generateBaseEndpointSet } from '../actions/template-endpoints'; +import { WorkSeriesSchemaBundleLeanDto } from '../dtos/WorkSeriesSchemaBundleLeanDtoSchema'; + +export const { + getPage, + deleteIdList, + postList, + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList +} = generateBaseEndpointSet< + WorkSeriesSchemaBundleLeanDto, + number +>( + '/api/v2/workProjectSeriesSchemas/bundles' +); + + diff --git a/app/api/READ-ONLY-generated-actions/WorkTaskType.ts b/app/api/READ-ONLY-generated-actions/WorkTaskType.ts new file mode 100644 index 0000000..f3a9e84 --- /dev/null +++ b/app/api/READ-ONLY-generated-actions/WorkTaskType.ts @@ -0,0 +1,23 @@ +'use server'; +import { generateBaseEndpointSet } from '../actions/template-endpoints'; +import { WorkTaskTypeDto } from '../dtos/WorkTaskTypeDtoSchema'; + +export const { + getPage, + deleteIdList, + postList, + putList, + getOne, + postOne, + putOne, + deleteOne, + getDtoListByBodyList, + getDtoListByParamList +} = generateBaseEndpointSet< + WorkTaskTypeDto, + number +>( + '/api/v2/workTaskTypes' +); + + diff --git a/app/api/actions/template-actions.ts b/app/api/actions/template-actions.ts index 3840246..e5d3bd9 100644 --- a/app/api/actions/template-actions.ts +++ b/app/api/actions/template-actions.ts @@ -90,6 +90,16 @@ export async function patchEntity( }); return callApi(url, requestInit); } +export async function putEntity( + entity: T, + url: string +): ActionResponsePromise { + const requestInit = createRequestInit({ + body: entity, + method: 'PUT' + }); + return callApi(url, requestInit); +} export async function patchEntityList( entityList: T[], url: string @@ -137,6 +147,11 @@ export async function deleteEntities( return callApi(url, request); } +export async function deleteEntity(url: string): ActionResponsePromise { + const request = createRequestInit({ method: 'DELETE' }); + return callApi(url, request); +} + async function callApi( url: string, request: RequestInit diff --git a/app/api/actions/template-endpoints.ts b/app/api/actions/template-endpoints.ts index c0a3974..e1f33f8 100644 --- a/app/api/actions/template-endpoints.ts +++ b/app/api/actions/template-endpoints.ts @@ -1,19 +1,24 @@ import { ActionResponsePromise } from './actionResponse'; import { deleteEntities, + deleteEntity, + getDtoListByIds, getWithoutBody, postEntities, - putEntities + postEntity, + putEntities, + putEntity } from './template-actions'; -import { API_V2_URL, isNotUndefined, Page } from '../main'; +import { BASE_URL, isNotUndefined, Page } from '../main'; +import { HasNumberId, HasUuid } from '../dto-interfaces'; function constructUrl(resourceSegments: string[] | string, action?: string) { - const basePath = API_V2_URL; + const basePath = BASE_URL; const resourcePath = Array.isArray(resourceSegments) ? resourceSegments.join('/') : resourceSegments; - return `${basePath}/${resourcePath}${ + return `${basePath}${resourcePath}${ isNotUndefined(action) ? `/${action}` : '' }`; } @@ -24,11 +29,23 @@ export interface PageRequest { sort?: string; } -export interface BaseEndpointSet { +export interface BaseEndpointSet { getPage: (pageRequest: PageRequest) => ActionResponsePromise>; putList: (dtoList: T[]) => ActionResponsePromise; postList: (dtoList: T[]) => ActionResponsePromise; deleteIdList: (idDeletionList: ID_TYPE[]) => ActionResponsePromise; + getOne: (id: ID_TYPE) => ActionResponsePromise>; + putOne: (dto: T) => ActionResponsePromise; + postOne: (dto: T) => ActionResponsePromise; + deleteOne: (id: ID_TYPE) => ActionResponsePromise; + getDtoListByParamList: ( + idList: ID_TYPE[], + url: string + ) => ActionResponsePromise; + getDtoListByBodyList: ( + idList: ID_TYPE[], + url: string + ) => ActionResponsePromise; } async function getDtoList( @@ -37,6 +54,19 @@ async function getDtoList( ): ActionResponsePromise> { return getWithoutBody(`${url}?page=${page}&size=${pageSize}`); } +async function getDtoListByParamList( + idList: ID_TYPE[], + url: string +): ActionResponsePromise { + const paramString = idList.map((id) => `id=${id}`).join('&'); + return getWithoutBody(`${url}/byIdList?${paramString}`); +} +async function getDtoListByBodyList( + idList: ID_TYPE[], + url: string +): ActionResponsePromise { + return getDtoListByIds(idList, `${url}/byIdList`); +} async function putDtoList( dtoList: T[], @@ -59,15 +89,54 @@ async function deleteDtoList( return deleteEntities(deletionIdList, url); } -export function generateBaseEndpointSet( - path: string | string[] -): BaseEndpointSet { +function getSingletonCrudUrl(url: string, id: number | string) { + return `${url}/${id}`; +} + +async function getDto( + id: ID_TYPE, + url: string +): ActionResponsePromise { + return getWithoutBody(getSingletonCrudUrl(url, id)); +} + +async function putDto( + dto: T, + url: string +): ActionResponsePromise { + return putEntity(dto, getSingletonCrudUrl(url, dto.id)); +} + +async function postDto(dto: T, url: string): ActionResponsePromise { + return postEntity(dto, `${url}/create`); +} + +async function deleteOneEntity( + deletionId: ID_TYPE, + url: string +): ActionResponsePromise { + return deleteEntity(getSingletonCrudUrl(url, deletionId)); +} + +export function generateBaseEndpointSet< + T extends HasNumberId | HasUuid, + ID_TYPE extends string | number +>(path: string | string[]): BaseEndpointSet { const generatedUrl = constructUrl(path); + console.log('generated url:', generatedUrl); return { getPage: (pageRequest) => getDtoList(pageRequest, generatedUrl), putList: (dtoList) => putDtoList(dtoList, generatedUrl), postList: (dtoList) => postDtoList(dtoList, generatedUrl), deleteIdList: (idDeletionList) => - deleteDtoList(idDeletionList, generatedUrl) + deleteDtoList(idDeletionList, generatedUrl), + getOne: (id) => getDto(id, generatedUrl), + putOne: (dto) => putDto(dto, generatedUrl), + postOne: (dto) => postDto(dto, generatedUrl), + deleteOne: (id) => deleteOneEntity(id, generatedUrl), + getDtoListByParamList: (idList) => + getDtoListByParamList(idList, generatedUrl), + getDtoListByBodyList: (idList) => + getDtoListByBodyList(idList, generatedUrl) }; } diff --git a/app/api/main.ts b/app/api/main.ts index 3383220..6d964c7 100644 --- a/app/api/main.ts +++ b/app/api/main.ts @@ -1,5 +1,6 @@ import { StringMap } from '../contexts/string-map-context/string-map-reducer'; +export const BASE_URL = process.env.BASE_URL!; export const API_BASE_URL = process.env.API_BASE_URL!; export const API_V2_URL = `${process.env.API_BASE_URL!}/v2`; export const API_ACADEMIC_URL = process.env.API_ACADEMIC_URL!; diff --git a/app/curriculum/class-hierarchy/[yearGroup]/page.tsx b/app/curriculum/class-hierarchy/[yearGroup]/page.tsx index 7a26f05..f2b5e56 100644 --- a/app/curriculum/class-hierarchy/[yearGroup]/page.tsx +++ b/app/curriculum/class-hierarchy/[yearGroup]/page.tsx @@ -7,26 +7,17 @@ import { BundleItemsContextProvider } from '../../delivery-models/contexts/bundl import { BundleAssignmentsProvider } from '../../delivery-models/contexts/bundle-assignments-provider'; import { StringMap } from '../../../contexts/string-map-context/string-map-reducer'; -import { - getWorkTaskTypes, - getWorkTaskTypesByWorkProjectSeriesSchemaIdList -} from '../../../api/actions/work-task-types'; +import { getWorkTaskTypesByWorkProjectSeriesSchemaIdList } from '../../../api/actions/work-task-types'; import { CurriculumDeliveryModelsInit } from '../../delivery-models/curriculum-delivery-models-init'; import CurriculumDeliveryGraph, { CurriculumDeliveryGraphPageKey } from '../../../graphing/graph-types/organization/curriculum-delivery-graph'; import React from 'react'; -import { - getBundles, - getBundlesBySchemaIdList -} from '../../../api/actions/work-series-schema-bundles'; import { getBundleAssignmentsByOrgType } from '../../../api/actions/work-series-bundle-assignments'; -import { - getOrganizationGraphByOrganizationType, - getOrganizationGraphByRootId -} from '../../../api/actions/organizations'; +import { getOrganizationGraphByOrganizationType } from '../../../api/actions/organizations'; import { parseTen } from '../../../api/date-and-time'; import { isNotUndefined } from '../../../api/main'; +import { getPage } from '../../../api/READ-ONLY-generated-actions/WorkSeriesSchemaBundle'; const emptyBundles = {} as StringMap; @@ -37,7 +28,9 @@ export default async function Page({ yearGroup: string; }; }) { - const actionResponseAllBundles = await getBundles(); + const actionResponseAllBundles = await getPage({ pageSize: 100 }); + + console.log('bundle response:', actionResponseAllBundles); const schemaIdList = actionResponseAllBundles.data?.content .map((bundle) => bundle.workProjectSeriesSchemaIds) diff --git a/app/curriculum/delivery-models/contexts/bundle-items-context-provider.tsx b/app/curriculum/delivery-models/contexts/bundle-items-context-provider.tsx index 4991b4e..576f3be 100644 --- a/app/curriculum/delivery-models/contexts/bundle-items-context-provider.tsx +++ b/app/curriculum/delivery-models/contexts/bundle-items-context-provider.tsx @@ -22,10 +22,10 @@ import { useModal } from '../../../generic/components/modals/confirm-action-moda import { UnsavedChangesModal } from '../../../generic/components/modals/unsaved-changes-modal'; import { UnsavedBundleEdits } from '../../../selective-context/keys/work-series-schema-bundle-keys'; import { - deleteBundles, - postBundles, - putBundles -} from '../../../api/actions/work-series-schema-bundles'; + deleteIdList, + postList, + putList +} from '../../../api/READ-ONLY-generated-actions/WorkSeriesSchemaBundle'; export const StaticDeletedBundleList: number[] = []; @@ -74,8 +74,8 @@ export function BundleItemsContextProvider({ const updatedBundles: StringMapPayload[] = []; const responses: ActionResponse[] = []; - deleteBundles(deleteBundleIds) - .then(() => postBundles(newBundles)) + deleteIdList(deleteBundleIds) + .then(() => postList(newBundles)) .then((r) => { if (r.status >= 200 && r.status < 300 && r.data) { getPayloadArray(r.data, (bundle) => bundle.id.toString()).forEach( @@ -85,7 +85,7 @@ export function BundleItemsContextProvider({ return; } }) - .then(() => putBundles(existingBundles)) + .then(() => putList(existingBundles)) .then((r) => { if (r.status >= 200 && r.status < 300 && r.data) { const payloadArray = getPayloadArray(r.data, (bundle) =>