Bugfixing
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import Elysia, { t } from "elysia";
|
||||
import Elysia, { NotFoundError, t } from "elysia";
|
||||
import { StandardsParamsSchema } from "@blade-and-brawn/calculator";
|
||||
import { authPlugin } from "../plugins/auth";
|
||||
import { s } from "../services";
|
||||
import { BigIntIdSchema } from "../util/models";
|
||||
import { ConflictError } from "../util/error";
|
||||
|
||||
const StandardsController = new Elysia({ prefix: "/standards" })
|
||||
.use(authPlugin)
|
||||
@@ -17,18 +18,25 @@ const StandardsController = new Elysia({ prefix: "/standards" })
|
||||
})
|
||||
.group("/:id", (app) => app
|
||||
.get("/", async ({ params: { id } }) => {
|
||||
return await s.Standards.Configs.get(id);
|
||||
const config = await s.Standards.Configs.get(id);
|
||||
if (!config) throw new NotFoundError("Standards config not found");
|
||||
return config;
|
||||
}, {
|
||||
params: t.Object({ id: BigIntIdSchema })
|
||||
})
|
||||
.put("/", async ({ params: { id }, body: { name, datasetId, params: parameters } }) => {
|
||||
await s.Standards.Configs.update(id, name, datasetId, parameters);
|
||||
const activeConfig = await s.Calculator.Standards.Config.get();
|
||||
if (activeConfig.id === id) throw new ConflictError("Cannot edit the active standards config");
|
||||
const updated = await s.Standards.Configs.update(id, name, datasetId, parameters);
|
||||
if (!updated) throw new NotFoundError("Standards config not found");
|
||||
}, {
|
||||
params: t.Object({ id: BigIntIdSchema }),
|
||||
body: t.Object({ name: t.String(), datasetId: BigIntIdSchema, params: StandardsParamsSchema })
|
||||
})
|
||||
.delete("/", async ({ params: { id } }) => {
|
||||
await s.Standards.Configs.delete(id);
|
||||
if (await s.Standards.Configs.count() <= 1) throw new ConflictError("Cannot delete the last remaining standards config");
|
||||
const deleted = await s.Standards.Configs.delete(id);
|
||||
if (!deleted) throw new NotFoundError("Standards config not found");
|
||||
}, { params: t.Object({ id: BigIntIdSchema }) })
|
||||
)
|
||||
)
|
||||
@@ -38,12 +46,15 @@ const StandardsController = new Elysia({ prefix: "/standards" })
|
||||
})
|
||||
.group("/:id", (app) => app
|
||||
.get("/", async ({ params: { id } }) => {
|
||||
return await s.Standards.Datasets.get(id);
|
||||
const dataset = await s.Standards.Datasets.get(id);
|
||||
if (!dataset) throw new NotFoundError("Standards dataset not found");
|
||||
return dataset;
|
||||
}, {
|
||||
params: t.Object({ id: BigIntIdSchema })
|
||||
})
|
||||
.patch("/", async ({ params: { id }, body: { name } }) => {
|
||||
await s.Standards.Datasets.update(id, name);
|
||||
const updated = await s.Standards.Datasets.update(id, name);
|
||||
if (!updated) throw new NotFoundError("Standards dataset not found");
|
||||
}, {
|
||||
params: t.Object({ id: BigIntIdSchema }),
|
||||
body: t.Object({ name: t.String() })
|
||||
|
||||
Reference in New Issue
Block a user