Skip to content

Commit

Permalink
🔨 finish refactoring of api routes
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Dec 20, 2024
1 parent 1b1628d commit 35071d6
Show file tree
Hide file tree
Showing 18 changed files with 442 additions and 448 deletions.
429 changes: 429 additions & 0 deletions adminSiteServer/apiRouter.ts

Large diffs are not rendered by default.

24 changes: 0 additions & 24 deletions adminSiteServer/apiRoutes/bulkUpdates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ import {
getGrapherConfigsForVariable,
updateGrapherConfigAdminOfVariable,
} from "../../db/model/Variable.js"
import {
getRouteWithROTransaction,
patchRouteWithRWTransaction,
} from "../functionalRouterHelpers.js"
import { saveGrapher } from "./charts.js"
import * as db from "../../db/db.js"
import * as lodash from "lodash"
import { apiRouter } from "../apiRouter.js"
import { Request } from "../authentication.js"
import e from "express"

Expand Down Expand Up @@ -245,22 +240,3 @@ export async function updateVariableAnnotations(

return { success: true }
}

patchRouteWithRWTransaction(
apiRouter,
"/variable-annotations",
updateVariableAnnotations
)

getRouteWithROTransaction(apiRouter, "/chart-bulk-update", getChartBulkUpdate)

patchRouteWithRWTransaction(
apiRouter,
"/chart-bulk-update",
updateBulkChartConfigs
)
getRouteWithROTransaction(
apiRouter,
"/variable-annotations",
getVariableAnnotations
)
17 changes: 0 additions & 17 deletions adminSiteServer/apiRoutes/chartViews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,11 @@ import { diffGrapherConfigs, mergeGrapherConfigs } from "@ourworldindata/utils"
import { omit, pick } from "lodash"
import { ApiChartViewOverview } from "../../adminShared/AdminTypes.js"
import { expectInt } from "../../serverUtils/serverUtil.js"
import { apiRouter } from "../apiRouter.js"
import {
saveNewChartConfigInDbAndR2,
updateChartConfigInDbAndR2,
} from "../chartConfigHelpers.js"
import { deleteGrapherConfigFromR2ByUUID } from "../chartConfigR2Helpers.js"
import {
getRouteWithROTransaction,
postRouteWithRWTransaction,
putRouteWithRWTransaction,
deleteRouteWithRWTransaction,
} from "../functionalRouterHelpers.js"

import * as db from "../../db/db.js"
import { expectChartById } from "./charts.js"
Expand Down Expand Up @@ -288,13 +281,3 @@ export async function deleteChartView(

return { success: true }
}

getRouteWithROTransaction(apiRouter, "/chartViews", getChartViews)

getRouteWithROTransaction(apiRouter, "/chartViews/:id", getChartViewById)

postRouteWithRWTransaction(apiRouter, "/chartViews", createChartView)

putRouteWithRWTransaction(apiRouter, "/chartViews/:id", updateChartView)

deleteRouteWithRWTransaction(apiRouter, "/chartViews/:id", deleteChartView)
79 changes: 13 additions & 66 deletions adminSiteServer/apiRoutes/charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ import {
BAKED_BASE_URL,
ADMIN_BASE_URL,
} from "../../settings/clientSettings.js"
import { apiRouter } from "../apiRouter.js"
import {
retrieveChartConfigFromDbAndSaveToR2,
updateChartConfigInDbAndR2,
Expand All @@ -55,12 +54,6 @@ import {
deleteGrapherConfigFromR2ByUUID,
saveGrapherConfigToR2ByUUID,
} from "../chartConfigR2Helpers.js"
import {
deleteRouteWithRWTransaction,
getRouteWithROTransaction,
postRouteWithRWTransaction,
putRouteWithRWTransaction,
} from "../functionalRouterHelpers.js"
import { triggerStaticBuild } from "./routeUtils.js"
import * as db from "../../db/db.js"
import { getLogsByChartId } from "../getLogsByChartId.js"
Expand Down Expand Up @@ -503,7 +496,7 @@ export async function updateGrapherConfigsInR2(
}
}

async function getChartsJson(
export async function getChartsJson(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadonlyTransaction
Expand All @@ -529,7 +522,7 @@ async function getChartsJson(
return { charts }
}

async function getChartsCsv(
export async function getChartsCsv(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadonlyTransaction
Expand Down Expand Up @@ -589,15 +582,15 @@ async function getChartsCsv(
return csv
}

async function getChartConfigJson(
export async function getChartConfigJson(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadonlyTransaction
) {
return expectChartById(trx, req.params.chartId)
}

async function getChartParentJson(
export async function getChartParentJson(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadonlyTransaction
Expand All @@ -615,7 +608,7 @@ async function getChartParentJson(
})
}

async function getChartPatchConfigJson(
export async function getChartPatchConfigJson(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadonlyTransaction
Expand All @@ -625,7 +618,7 @@ async function getChartPatchConfigJson(
return config
}

async function getChartLogsJson(
export async function getChartLogsJson(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadonlyTransaction
Expand All @@ -638,7 +631,7 @@ async function getChartLogsJson(
}
}

async function getChartReferencesJson(
export async function getChartReferencesJson(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadonlyTransaction
Expand All @@ -652,7 +645,7 @@ async function getChartReferencesJson(
return references
}

async function getChartRedirectsJson(
export async function getChartRedirectsJson(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadonlyTransaction
Expand All @@ -665,7 +658,7 @@ async function getChartRedirectsJson(
}
}

async function getChartPageviewsJson(
export async function getChartPageviewsJson(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadonlyTransaction
Expand All @@ -692,7 +685,7 @@ async function getChartPageviewsJson(
}
}

async function createChart(
export async function createChart(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadWriteTransaction
Expand All @@ -715,7 +708,7 @@ async function createChart(
}
}

async function setChartTagsHandler(
export async function setChartTagsHandler(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadWriteTransaction
Expand All @@ -727,7 +720,7 @@ async function setChartTagsHandler(
return { success: true }
}

async function updateChart(
export async function updateChart(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadWriteTransaction
Expand Down Expand Up @@ -762,7 +755,7 @@ async function updateChart(
}
}

async function deleteChart(
export async function deleteChart(
req: Request,
res: e.Response<any, Record<string, any>>,
trx: db.KnexReadWriteTransaction
Expand Down Expand Up @@ -814,49 +807,3 @@ async function deleteChart(

return { success: true }
}

getRouteWithROTransaction(apiRouter, "/charts.json", getChartsJson)
getRouteWithROTransaction(apiRouter, "/charts.csv", getChartsCsv)
getRouteWithROTransaction(
apiRouter,
"/charts/:chartId.config.json",
getChartConfigJson
)
getRouteWithROTransaction(
apiRouter,
"/charts/:chartId.parent.json",
getChartParentJson
)
getRouteWithROTransaction(
apiRouter,
"/charts/:chartId.patchConfig.json",
getChartPatchConfigJson
)
getRouteWithROTransaction(
apiRouter,
"/charts/:chartId.logs.json",
getChartLogsJson
)
getRouteWithROTransaction(
apiRouter,
"/charts/:chartId.references.json",
getChartReferencesJson
)
getRouteWithROTransaction(
apiRouter,
"/charts/:chartId.redirects.json",
getChartRedirectsJson
)
getRouteWithROTransaction(
apiRouter,
"/charts/:chartId.pageviews.json",
getChartPageviewsJson
)
postRouteWithRWTransaction(apiRouter, "/charts", createChart)
postRouteWithRWTransaction(
apiRouter,
"/charts/:chartId/setTags",
setChartTagsHandler
)
putRouteWithRWTransaction(apiRouter, "/charts/:chartId", updateChart)
deleteRouteWithRWTransaction(apiRouter, "/charts/:chartId", deleteChart)
23 changes: 0 additions & 23 deletions adminSiteServer/apiRoutes/datasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ import {
import { getDatasetById, setTagsForDataset } from "../../db/model/Dataset.js"
import { logErrorAndMaybeSendToBugsnag } from "../../serverUtils/errorLog.js"
import { expectInt } from "../../serverUtils/serverUtil.js"
import { apiRouter } from "../apiRouter.js"
import {
getRouteWithROTransaction,
putRouteWithRWTransaction,
postRouteWithRWTransaction,
deleteRouteWithRWTransaction,
} from "../functionalRouterHelpers.js"
import {
syncDatasetToGitRepo,
removeDatasetFromGitRepo,
Expand Down Expand Up @@ -413,19 +406,3 @@ export async function republishCharts(

return { success: true }
}

getRouteWithROTransaction(apiRouter, "/datasets.json", getDatasets)
getRouteWithROTransaction(apiRouter, "/datasets/:datasetId.json", getDataset)
putRouteWithRWTransaction(apiRouter, "/datasets/:datasetId", updateDataset)
postRouteWithRWTransaction(
apiRouter,
"/datasets/:datasetId/setArchived",
setArchived
)
postRouteWithRWTransaction(apiRouter, "/datasets/:datasetId/setTags", setTags)
deleteRouteWithRWTransaction(apiRouter, "/datasets/:datasetId", deleteDataset)
postRouteWithRWTransaction(
apiRouter,
"/datasets/:datasetId/charts",
republishCharts
)
13 changes: 0 additions & 13 deletions adminSiteServer/apiRoutes/explorer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import { JsonError } from "@ourworldindata/types"
import { apiRouter } from "../apiRouter.js"
import {
postRouteWithRWTransaction,
deleteRouteWithRWTransaction,
} from "../functionalRouterHelpers.js"
import { Request } from "express"
import * as e from "express"

Expand Down Expand Up @@ -36,11 +31,3 @@ export async function deleteExplorerTags(
await trx.table("explorer_tags").where({ explorerSlug: slug }).delete()
return { success: true }
}

postRouteWithRWTransaction(apiRouter, "/explorer/:slug/tags", addExplorerTags)

deleteRouteWithRWTransaction(
apiRouter,
"/explorer/:slug/tags",
deleteExplorerTags
)
22 changes: 0 additions & 22 deletions adminSiteServer/apiRoutes/gdocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,6 @@ import {
} from "../../db/model/Gdoc/GdocFactory.js"
import { GdocHomepage } from "../../db/model/Gdoc/GdocHomepage.js"
import { GdocPost } from "../../db/model/Gdoc/GdocPost.js"
import { apiRouter } from "../apiRouter.js"
import {
getRouteWithROTransaction,
getRouteNonIdempotentWithRWTransaction,
putRouteWithRWTransaction,
deleteRouteWithRWTransaction,
postRouteWithRWTransaction,
} from "../functionalRouterHelpers.js"
import { triggerStaticBuild, enqueueLightningChange } from "./routeUtils.js"
import * as db from "../../db/db.js"
import * as lodash from "lodash"
Expand Down Expand Up @@ -295,17 +287,3 @@ export async function setGdocTags(

return { success: true }
}

getRouteWithROTransaction(apiRouter, "/gdocs", getAllGdocIndexItems)

getRouteNonIdempotentWithRWTransaction(
apiRouter,
"/gdocs/:id",
getIndividualGdoc
)

putRouteWithRWTransaction(apiRouter, "/gdocs/:id", createOrUpdateGdoc)

deleteRouteWithRWTransaction(apiRouter, "/gdocs/:id", deleteGdoc)

postRouteWithRWTransaction(apiRouter, "/gdocs/:gdocId/setTags", setGdocTags)
26 changes: 0 additions & 26 deletions adminSiteServer/apiRoutes/images.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
import { DbEnrichedImage, JsonError } from "@ourworldindata/types"
import pMap from "p-map"
import { apiRouter } from "../apiRouter.js"
import {
getRouteNonIdempotentWithRWTransaction,
postRouteWithRWTransaction,
putRouteWithRWTransaction,
patchRouteWithRWTransaction,
deleteRouteWithRWTransaction,
getRouteWithROTransaction,
} from "../functionalRouterHelpers.js"
import {
validateImagePayload,
processImageContent,
Expand Down Expand Up @@ -268,20 +259,3 @@ export async function getImageUsageHandler(
usage,
}
}

getRouteNonIdempotentWithRWTransaction(
apiRouter,
"/images.json",
getImagesHandler
)

postRouteWithRWTransaction(apiRouter, "/images", postImageHandler)

putRouteWithRWTransaction(apiRouter, "/images/:id", putImageHandler)

// Update alt text via patch
patchRouteWithRWTransaction(apiRouter, "/images/:id", patchImageHandler)

deleteRouteWithRWTransaction(apiRouter, "/images/:id", deleteImageHandler)

getRouteWithROTransaction(apiRouter, "/images/usage", getImageUsageHandler)
6 changes: 0 additions & 6 deletions adminSiteServer/apiRoutes/mdims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,3 @@ export async function handleMultiDimDataPageRequest(
}
return { success: true, id }
}

putRouteWithRWTransaction(
apiRouter,
"/multi-dim/:slug",
handleMultiDimDataPageRequest
)
Loading

0 comments on commit 35071d6

Please sign in to comment.