51 lines
1.1 KiB
JavaScript
51 lines
1.1 KiB
JavaScript
import { z } from 'zod';
|
|
|
|
export default class Network {
|
|
prefix = `NETWORK`
|
|
indices = null
|
|
|
|
constructor(indices) {
|
|
this.indices = indices
|
|
}
|
|
|
|
schema = z.object({
|
|
id: z.number(),
|
|
name: z.string(),
|
|
apps: z.array(z.string()),
|
|
logo: z.string(),
|
|
abbreviation: z.string(),
|
|
created: z.string()
|
|
})
|
|
.strict()
|
|
|
|
get(stringID) {
|
|
let id = stringID.split("-")[1]
|
|
let index = this.indices[0] + (id - 1)
|
|
console.log("networkget", index, id, global.db.nodes[index-1])
|
|
return global.db.nodes[index]
|
|
}
|
|
|
|
save(n) {
|
|
let id = `${this.prefix}-${n.id}`
|
|
let result = this.schema.safeParse(n)
|
|
if(result.success) {
|
|
try {
|
|
super.add(id, n)
|
|
} catch(e) {
|
|
console.error(e)
|
|
throw e
|
|
}
|
|
} else {
|
|
console.error(result.error)
|
|
throw new global.ServerError(400, "Invalid Member Data!");
|
|
}
|
|
}
|
|
|
|
add(n) {
|
|
let toSave = {
|
|
id: this.entries.length+1,
|
|
...n
|
|
}
|
|
this.save(toSave)
|
|
}
|
|
} |