feat: things are happening...

This commit is contained in:
Daniel Ledda
2022-07-10 23:07:51 +02:00
parent ba68f953f0
commit fb9f78caf7
22 changed files with 448 additions and 207 deletions

View File

@@ -1,7 +1,9 @@
import * as bcrypt from "bcrypt";
import { StoccaTreDbConn, WithoutId } from "../../database.ts";
import {Q, StoccaTreDbConn, WithoutId} from "../../database.ts";
import { UserModel } from "./UserModel.ts";
import { Maybe } from "../../Maybe.ts";
import {Result, StoccaTreError} from "../../Result.ts";
const TABLE_NAME = "main.users";
export default class UserCollection {
private db: StoccaTreDbConn;
@@ -11,40 +13,34 @@ export default class UserCollection {
this.db = database;
}
async addUser(user: WithoutId<UserModel>): Promise<Maybe<{ insertedId: number }>> {
async addUser(user: WithoutId<UserModel>): Promise<Result<{ id: number }>> {
let hash: string;
try {
hash = await bcrypt.hash(user.password);
} catch (e: unknown) {
const error = e as { message?: string };
if (typeof error.message === "string") {
return { error: { message: error.message }};
return [new StoccaTreError(error.message)];
}
return { error: new Error("Failed to create user") };
return [new StoccaTreError("Failed to create user")];
}
user.password = hash;
const result: Maybe<UserModel[]> = await this.db.query(sql => sql`
INSERT INTO users ${ sql(user, "displayName") }
RETURNING *
`);
if (result.error) {
return result;
const [error, users] = <Q<UserModel>> await this.db.query(`INSERT INTO ${TABLE_NAME} ($displayName, $password) RETURNING *`, user);
if (error) {
return [error];
}
return {
just: { insertedId: result.just[0]?.id ?? NaN }
if (users.length === 0) {
return [new StoccaTreError("Failed to insert user.")];
}
return [, users[0]];
}
async getAllUsers(): Promise<Maybe<UserModel[]>> {
const result = await this.db.query((sql) => sql<UserModel[]>`SELECT * FROM users`);
if (!result.error) {
result.just.forEach(user => this.mapById.set(user.id, user));
} else {
return result;
async getAllUsers(): Promise<Result<UserModel[]>> {
const [error, users] = <Q<UserModel>> await this.db.query(`SELECT * FROM ${TABLE_NAME}`);
if (error) {
return [error];
}
return {
just: Array.from(this.mapById.values()),
};
users.forEach((user) => this.mapById.set(user.id, user));
return [, Array.from(this.mapById.values())];
}
}

View File

@@ -9,4 +9,4 @@ export const UserSchema = z.object({
export const UserSchemaWithoutId = UserSchema.omit({ id: true });
export type UserModel = z.infer<typeof UserSchema>;
export type UserModel = z.infer<typeof UserSchema>;

View File

@@ -1,9 +1,9 @@
import {StoccaTreDbConn} from "../../database.ts";
import StoccaTreRequest, {RouteDefinition} from "../../StoccaTreRequest.ts";
import {Maybe} from "../../Maybe.ts";
import {JSONObject} from "../../JSON.ts";
import { StoccaTreDbConn } from "../../database.ts";
import StoccaTreRequest, { RouteDefinition } from "../../StoccaTreRequest.ts";
import {Result, StoccaTreError} from "../../Result.ts";
import { JSONObject } from "../../JSON.ts";
import UserCollection from "./UserCollection.ts";
import {UserSchemaWithoutId} from "./UserModel.ts";
import { UserSchemaWithoutId } from "./UserModel.ts";
export default class UserResource {
private dbConnection: StoccaTreDbConn;
@@ -11,11 +11,11 @@ export default class UserResource {
private routes: Readonly<Record<string, RouteDefinition>> = {
Add: {
pattern: /\/add/,
method: "POST"
method: "POST",
},
GetAll: {
pattern: /\/all/,
method: "GET"
method: "GET",
},
} as const;
@@ -24,33 +24,29 @@ export default class UserResource {
this.collection = new UserCollection(dbConnection);
}
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
async handleRequest(request: StoccaTreRequest): Promise<Result<JSONObject> | null> {
if (request.match(this.routes.Add)) {
return await this.addUser(request);
}
if (request.match(this.routes.GetAll)) {
return await this.allUsers(request);
}
return { error: { message: "Invalid route" }};
return null;
}
private async addUser(request: StoccaTreRequest): Promise<Maybe<{ insertedId: number }>> {
const user = UserSchemaWithoutId.safeParse(JSON.parse(request.body ?? "{}"));
private async addUser(request: StoccaTreRequest): Promise<Result<{ id: number }>> {
const user = UserSchemaWithoutId.safeParse(request.body);
if (!user.success) {
return { error: new Error("Ingredient was malformed.") };
return [new StoccaTreError("User definition was malformed.", 400)];
}
return await this.collection.addUser(user.data);
}
private async allUsers(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
const allUsers = await this.collection.getAllUsers();
if (allUsers.error) {
return allUsers;
private async allUsers(request: StoccaTreRequest): Promise<Result<JSONObject>> {
const [error, result] = await this.collection.getAllUsers();
if (error) {
return [error];
}
return {
just: {
ingredients: Array.from(allUsers.just),
},
};
return [, { users: result }];
}
}

View File

@@ -1,3 +1,3 @@
export * from "./UserModel.ts";
export * from "./UserCollection.ts";
export * from "./UserResource.ts";
export { default as UserCollection } from "./UserCollection.ts";
export { default as UserResource } from "./UserResource.ts";