Skip to content

Commit

Permalink
Provide assets per entity and system via API
Browse files Browse the repository at this point in the history
  • Loading branch information
Spenhouet committed Mar 3, 2024
1 parent 3963ed5 commit 9e08e6b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/api/resources/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class Entity extends JsonResources {
icons: {
$ref: `/entities/${id}/icons`,
},
assets: {
$ref: `/entities/${id}/assets`,
},
};
}

Expand Down Expand Up @@ -81,6 +84,23 @@ class Entity extends JsonResources {
async icons(id: bcked.entity.Id) {
return icons("entities", id);
}

@JsonResources.register({
path: "/entities/{id}/assets",
summary: "Get assets of a entity",
description: "Get assets of a entity by its ID",
type: "EntityAssets",
// TODO write schema
schema: {},
})
async assets(id: bcked.entity.Id, assetIds: bcked.asset.Id[]) {
return {
$id: `/entities/${id}/assets`,
assets: assetIds.map((assetId) => ({
$ref: `/assets/${assetId}`,
})),
};
}
}

export const ENTITY_RESOURCES = new Entity();
20 changes: 20 additions & 0 deletions src/api/resources/systems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class System extends JsonResources {
icons: {
$ref: `/systems/${id}/icons`,
},
assets: {
$ref: `/systems/${id}/assets`,
},
};
}

Expand Down Expand Up @@ -81,6 +84,23 @@ class System extends JsonResources {
async icons(id: bcked.system.Id) {
return icons("systems", id);
}

@JsonResources.register({
path: "/systems/{id}/assets",
summary: "Get assets of a system",
description: "Get assets of a system by its ID",
type: "SystemAssets",
// TODO write schema
schema: {},
})
async assets(id: bcked.system.Id, assetIds: bcked.asset.Id[]) {
return {
$id: `/systems/${id}/assets`,
assets: assetIds.map((assetId) => ({
$ref: `/assets/${assetId}`,
})),
};
}
}

export const SYSTEM_RESOURCES = new System();
15 changes: 15 additions & 0 deletions src/api/utils/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,18 @@ export async function compileIcons<

return resource;
}

export async function compileAssets<
Resources extends JsonResources & { assets: (...args: any[]) => any }
>(resources: Resources, path: string, id: string) {
const filePath = join(path, id, PATHS.records, "assets.json");
const assets = await readJson<{ ids: bcked.asset.Id[] }>(filePath);

if (!assets?.ids?.length) {
throw new Error(`No assets found for ${id}`);
}

const resource = await resources.assets(id, assets.ids);

return resource;
}
3 changes: 2 additions & 1 deletion src/api/workers/compile_entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parentPort } from "worker_threads";
import { PATHS } from "../../paths";
import { sendErrorReport } from "../../watcher/bot";
import { ENTITY_RESOURCES } from "../resources/entities";
import { compileDetails, compileIcons } from "../utils/compile";
import { compileAssets, compileDetails, compileIcons } from "../utils/compile";

parentPort?.on("message", async (id: bcked.entity.Id) => {
console.log(`Compile entity ${id}`);
Expand All @@ -11,6 +11,7 @@ parentPort?.on("message", async (id: bcked.entity.Id) => {
ENTITY_RESOURCES.entity(id),
compileDetails(ENTITY_RESOURCES, PATHS.entities, id),
compileIcons(ENTITY_RESOURCES, PATHS.entities, id),
compileAssets(ENTITY_RESOURCES, PATHS.entities, id),
]);

parentPort?.postMessage(null);
Expand Down
3 changes: 2 additions & 1 deletion src/api/workers/compile_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parentPort } from "worker_threads";
import { PATHS } from "../../paths";
import { sendErrorReport } from "../../watcher/bot";
import { SYSTEM_RESOURCES } from "../resources/systems";
import { compileDetails, compileIcons } from "../utils/compile";
import { compileAssets, compileDetails, compileIcons } from "../utils/compile";

parentPort?.on("message", async (id: bcked.system.Id) => {
console.log(`Compile system ${id}`);
Expand All @@ -11,6 +11,7 @@ parentPort?.on("message", async (id: bcked.system.Id) => {
SYSTEM_RESOURCES.system(id),
compileDetails(SYSTEM_RESOURCES, PATHS.systems, id),
compileIcons(SYSTEM_RESOURCES, PATHS.systems, id),
compileAssets(SYSTEM_RESOURCES, PATHS.systems, id),
]);

parentPort?.postMessage(null);
Expand Down
2 changes: 1 addition & 1 deletion src/api/workers/precompile_relations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function storeGrouping(
toId((asset as bcked.asset.Details).identifier)
);
const jsonFilePath = join(path, key, PATHS.records, "assets.json");
await writeJson(jsonFilePath, { assets: assetIds });
await writeJson(jsonFilePath, { ids: assetIds });
}
}

Expand Down

0 comments on commit 9e08e6b

Please sign in to comment.