diff --git a/bun.lock b/bun.lock index 11dfe4b..ebe77b3 100644 --- a/bun.lock +++ b/bun.lock @@ -11,6 +11,7 @@ "pinia": "^3.0.1", "trpc-bun-adapter": "^1.2.2", "ts-log": "^2.2.7", + "ts-results-es": "^5.0.1", "vue": "^3.5.13", "vue-router": "4", "zod": "^3.24.2", @@ -527,6 +528,8 @@ "ts-log": ["ts-log@2.2.7", "", {}, "sha512-320x5Ggei84AxzlXp91QkIGSw5wgaLT6GeAH0KsqDmRZdVWW2OiSeVvElVoatk3f7nicwXlElXsoFkARiGE2yg=="], + "ts-results-es": ["ts-results-es@5.0.1", "", {}, "sha512-HjX/7HxQe2bXkbp8pHTjy4Ir9eHIDnDDsLDphhGqy6I9iZ/vD4QXWEIlrVRZsEX+kS2jIiiF/mnl0nKnPTiYFw=="], + "typescript": ["typescript@5.7.3", "", { "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" } }, "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw=="], "undici-types": ["undici-types@6.20.0", "", {}, "sha512-Ny6QZ2Nju20vw1SRHe3d9jVu6gJ+4e3+MMpqu7pqE5HT6WsTSlce++GQmK5UXS8mzV8DSYHrQH+Xrf2jVcuKNg=="], diff --git a/package.json b/package.json index a2669fd..d1fa9c7 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "pinia": "^3.0.1", "trpc-bun-adapter": "^1.2.2", "ts-log": "^2.2.7", + "ts-results-es": "^5.0.1", "vue": "^3.5.13", "vue-router": "4", "zod": "^3.24.2" diff --git a/server/database.test.ts b/server/database.test.ts new file mode 100644 index 0000000..fc6f029 --- /dev/null +++ b/server/database.test.ts @@ -0,0 +1,10 @@ +import { test, expect } from "bun:test" +import * as db from "./database.ts" + +test("DataBase", () => { + const allTables = db.allTables() + expect(allTables).toBeArray() + expect(allTables).toEqual(["Users", "Boards"]) + expect(db.boardsNum()).toBe(0) + +}) diff --git a/server/database.ts b/server/database.ts index 3c121df..586dccc 100644 --- a/server/database.ts +++ b/server/database.ts @@ -1,5 +1,6 @@ import { Database } from "bun:sqlite"; -import { isNumber, isString } from "lodash"; +import _ from "lodash"; +import { Ok, Err, Result, None, Some, Option } from "ts-results-es"; import { z } from "zod"; const db = new Database("lab.sqlite", { strict: true }) @@ -7,7 +8,7 @@ initDB(); const boardSchema = z.object({ - id: z.number(), + id: z.number().nonnegative(), name: z.string(), room: z.string(), ipv4: z.string().ip({ version: "v4" }), @@ -26,7 +27,7 @@ export function isBoard(obj: any): obj is Board { } const userSchema = z.object({ - id: z.number(), + id: z.number().nonnegative(), name: z.string(), password: z.string(), boardID: z.number(), @@ -90,6 +91,61 @@ export function tableColumnName(table: string): Array { return columnName } +export function addBoard(board: Board): Result { + if (!isBoard(board)) { + return new Err("Wrong params type") + } + + if (board.id == 0) { + const idNum = boardsNum() + board.id = idNum + } else { + + } + + const query = db.query(` + INSERT INTO Boards VALUES + (${board.id}, + '${board.name}, + '${board.room}', + '${board.ipv4}', + '${typeof board.ipv6 === "undefined" ? "NULL" : board.ipv6}', + ${board.port}); + `) + query.run() + + return Ok(board) +} + +export function boardsNum(): number { + const query = db.query(`SELECT COUNT(*) FROM Boards`) + return query.values()[0][0] as number +} + +export function deleteBoard(name?: string): Result +export function deleteBoard(id?: number): Result +export function deleteBoard(board?: Board): Result +export function deleteBoard(arg: any): Result { + let retBoard + let condition: string + if (isBoard(arg)) { + retBoard = arg + + } else if (_.isNumber(arg)) { + retBoard = findBoard(arg) + + } else if (_.isString(arg)) { + retBoard = findBoard(arg) + + } else { + return new Err("Wrong Type") + } + + const query = db.query(`DELETE FROM Boards WHERE id=${arg.id}`) + + return new Ok(retBoard) +} + export function allBoards() { const query = db.query(`SELECT * FROM Boards`) const boards = query.all() @@ -98,26 +154,33 @@ export function allBoards() { return boards } -export function findBoard(id?: number | string): Board { + +export function findBoard(name?: string): Result, "Wrong type"> +export function findBoard(id?: number): Result, "Wrong type"> +export function findBoard(arg: any): Result, "Wrong type"> { let condition: string - if (isNumber(id)) { - condition = `id=${id}` - } else if (isString(id)) { - condition = `name='${id}'` + if (_.isNumber(arg)) { + condition = `arg=${arg}` + + } else if (_.isString(arg)) { + condition = `name='${arg}'` + } else { - throw new Error("Failure: Wrong type when find board") + return new Err("Wrong type") + } const query = db.query(`SELECT * FROM Boards WHERE ${condition}`) const spRet = boardSchema.safeParse(query.get()) if (spRet.success) { - return spRet.data + return new Ok(Some(spRet.data)) } else { - throw new Error(`Not Found ${id} FPGA Board`) + return new Ok(None) } } + export function findUser(name: string): User { const query = db.query(`SELECT * FROM Users WHERE name='${name}'`) diff --git a/server/equipment.ts b/server/equipment.ts index 8e848ee..79ab187 100644 --- a/server/equipment.ts +++ b/server/equipment.ts @@ -1,5 +1,5 @@ import type { Board } from "./database"; -import { boardSchema, findBoard } from "./database"; +import { findBoard, isBoard } from "./database"; import { numberToBytes } from "./common"; import { z } from "zod"; import _ from "lodash"; @@ -124,13 +124,13 @@ export class Equipment { constructor(name?: string) constructor(board?: Board) constructor(arg1: any) { - if (boardSchema.safeParse(arg1).success) { + if (isBoard(arg1)) { this.board = arg1 } else if (typeof arg1 === "string") { try { const board = findBoard(arg1) - this.board = boardSchema.parse(board) + this.board = board } catch (error) { throw new Error("Equipment Construction Failure") } diff --git a/server/test.ts b/server/test.ts deleted file mode 100644 index 4c65edf..0000000 --- a/server/test.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { allTables } from "./database"; - - -const tables = allTables(); -console.log(tables);