feat: embrace full REST API and OpenAPI+Swagger

This commit is contained in:
mattia
2025-01-06 16:36:53 +01:00
parent f9c61bae6f
commit 79e4a4c012
10 changed files with 507 additions and 119 deletions

58
src/server/db/post-log.ts Normal file
View File

@@ -0,0 +1,58 @@
import { SingleMetadata } from "../contract";
import { HttpError } from "../http-error";
import { db } from "./init";
export async function postLog(
gameName: string,
version: string,
saveGuid: string | undefined,
sessionGuid: string,
message: string,
metadata: SingleMetadata[]
) {
if (
gameName === "" ||
sessionGuid === "" ||
message === "" ||
version === ""
) {
const errorMessage = `One of the necessary fields was missing: gameName=${gameName}, sessionGuid=${sessionGuid}, message=${message}, version=${version}`;
throw new HttpError(422, errorMessage);
} else {
await db.transaction().execute(async (trx) => {
// upsert the session info
await trx
.insertInto("session_info")
.ignore()
.values({
id: sessionGuid,
game_name: gameName,
save_id: saveGuid || "",
version,
})
.execute();
// add the main log line
const insertResult = await trx
.insertInto("log_entries")
.values({
session_info_id: sessionGuid,
message: message,
timestamp: new Date(),
})
.executeTakeFirstOrThrow();
// add all the metadata
if (metadata.length > 0) {
await trx
.insertInto("log_metadata")
.values(
metadata.map(({ key, value }) => ({
log_entry_id: Number(insertResult.insertId),
key,
value,
}))
)
.execute();
}
});
}
}