feat: new user resource integrating postgresql
This commit is contained in:
@@ -1,31 +1,42 @@
|
||||
import {StoccaTreDbConn, WithoutId} from "../../database.ts";
|
||||
import {IngredientModel} from "./IngredientModel.ts";
|
||||
import {Maybe} from "../../Maybe.ts";
|
||||
import { StoccaTreDbConn, WithoutId } from "../../database.ts";
|
||||
import { IngredientModel } from "./IngredientModel.ts";
|
||||
import { Maybe } from "../../Maybe.ts";
|
||||
|
||||
export default class IngredientCollection {
|
||||
private dbConnection: StoccaTreDbConn;
|
||||
private db: StoccaTreDbConn;
|
||||
private mapById: Map<number, IngredientModel> = new Map<number, IngredientModel>();
|
||||
private allGotten = false;
|
||||
|
||||
constructor(database: StoccaTreDbConn) {
|
||||
this.dbConnection = database;
|
||||
this.db = database;
|
||||
}
|
||||
|
||||
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<any> {
|
||||
return await this.dbConnection.query<any>(
|
||||
`INSERT INTO ingredients (id, name, displayName, displayNameDE) VALUES (NULL, '${ingredient.name}', '${ingredient.displayName}', '${ingredient.displayNameDE}');`
|
||||
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<Maybe<IngredientModel[]>> {
|
||||
return await this.db.query(sql =>
|
||||
sql<IngredientModel[]>`INSERT INTO ingredients ${ sql(ingredient) }`
|
||||
);
|
||||
}
|
||||
|
||||
async getById(id: number): Promise<Maybe<IngredientModel>> {
|
||||
const found = this.mapById.get(id);
|
||||
if (found) {
|
||||
return { just: found };
|
||||
}
|
||||
const result = await this.db.query(sql => sql<IngredientModel[]>`SELECT * FROM ingredients WHERE id is ${ id }`);
|
||||
if (result.error) {
|
||||
return result;
|
||||
}
|
||||
const ingredient = result.just[0];
|
||||
this.mapById.set(ingredient.id, ingredient);
|
||||
return { just: ingredient };
|
||||
}
|
||||
async getAllIngredients(): Promise<Maybe<IterableIterator<IngredientModel>>> {
|
||||
if (!this.allGotten) {
|
||||
const result = await this.dbConnection.query<IngredientModel[]>("SELECT * FROM ingredients");
|
||||
if (!result.error) {
|
||||
result.just.forEach((ingredient: IngredientModel) => this.mapById.set(ingredient.id, ingredient));
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
this.allGotten = true;
|
||||
const result: Maybe<IngredientModel[]> = await this.db.query(sql =>
|
||||
sql<IngredientModel[]>`SELECT * FROM ingredients`
|
||||
);
|
||||
if (!result.error) {
|
||||
result.just.forEach(ingredient => this.mapById.set(ingredient.id, ingredient));
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
return {
|
||||
just: this.mapById.values(),
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
export type IngredientModel = {
|
||||
id: number,
|
||||
name: string,
|
||||
displayName: string,
|
||||
displayNameDE: string,
|
||||
};
|
||||
import { z } from "zod";
|
||||
|
||||
export const IngredientSchema = z.object({
|
||||
id: z.number(),
|
||||
name: z.string().nonempty(),
|
||||
displayName: z.string().nonempty(),
|
||||
displayNameDE: z.string().nonempty(),
|
||||
});
|
||||
|
||||
export const IngredientSchemaWithoutId = IngredientSchema.omit({ id: true });
|
||||
|
||||
export type IngredientModel = z.infer<typeof IngredientSchema>;
|
||||
@@ -1,45 +1,48 @@
|
||||
import {StoccaTreDbConn} from "../../database.ts";
|
||||
import IngredientCollection from "./IngredientCollection.ts";
|
||||
import StoccaTreRequest from "../../StoccaTreRequest.ts";
|
||||
import StoccaTreRequest, {RouteDefinition} from "../../StoccaTreRequest.ts";
|
||||
import {Maybe} from "../../Maybe.ts";
|
||||
import {JSONObject} from "../../JSON.ts";
|
||||
import {IngredientModel} from "./IngredientModel.ts";
|
||||
import {IngredientSchemaWithoutId} from "./IngredientModel.ts";
|
||||
|
||||
export default class IngredientResource {
|
||||
private dbConnection: StoccaTreDbConn;
|
||||
private collection: IngredientCollection;
|
||||
private routes: Readonly<Record<string, RouteDefinition>> = {
|
||||
Add: {
|
||||
pattern: /\/add/,
|
||||
method: "POST"
|
||||
},
|
||||
GetAll: {
|
||||
pattern: /\/all/,
|
||||
method: "GET"
|
||||
},
|
||||
} as const;
|
||||
|
||||
constructor(dbConnection: StoccaTreDbConn) {
|
||||
this.dbConnection = dbConnection;
|
||||
this.collection = new IngredientCollection(dbConnection);
|
||||
}
|
||||
|
||||
static isIngredient(json: JSONObject): json is IngredientModel {
|
||||
if (json) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
||||
switch (request.route) {
|
||||
case "/add":
|
||||
if (request.method === "POST") {
|
||||
const ingredient = JSON.parse(request.body ?? "");
|
||||
return await this.collection.addIngredient(JSON.parse(request.body));
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
case "/all":
|
||||
return await this.allIngredients(request);
|
||||
default:
|
||||
break;
|
||||
if (request.match(this.routes.Add)) {
|
||||
return await this.addIngredient(request);
|
||||
}
|
||||
return { error: {message: "Invalid route" }};
|
||||
if (request.match(this.routes.GetAll)) {
|
||||
return await this.allIngredients(request);
|
||||
}
|
||||
return { error: { message: "Invalid route" }};
|
||||
}
|
||||
|
||||
async allIngredients(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
||||
private async addIngredient(request: StoccaTreRequest): Promise<Maybe<{ insertedId: number }>> {
|
||||
const ingredient = IngredientSchemaWithoutId.safeParse(JSON.parse(request.body ?? "{}"));
|
||||
if (!ingredient.success) {
|
||||
return { error: new Error("Ingredient was malformed.") };
|
||||
}
|
||||
return await this.collection.addIngredient(ingredient.data);
|
||||
}
|
||||
|
||||
private async allIngredients(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
||||
const getAllIngredientResult = await this.collection.getAllIngredients();
|
||||
if (getAllIngredientResult.error) {
|
||||
return getAllIngredientResult;
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
export * from "./user/main.ts";
|
||||
export * from "./ingredient/main.ts";
|
||||
|
||||
50
server/resources/user/UserCollection.ts
Normal file
50
server/resources/user/UserCollection.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import * as bcrypt from "bcrypt";
|
||||
import { StoccaTreDbConn, WithoutId } from "../../database.ts";
|
||||
import { UserModel } from "./UserModel.ts";
|
||||
import { Maybe } from "../../Maybe.ts";
|
||||
|
||||
export default class UserCollection {
|
||||
private db: StoccaTreDbConn;
|
||||
private mapById: Map<number, UserModel> = new Map<number, UserModel>();
|
||||
|
||||
constructor(database: StoccaTreDbConn) {
|
||||
this.db = database;
|
||||
}
|
||||
|
||||
async addUser(user: WithoutId<UserModel>): Promise<Maybe<{ insertedId: 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 { error: new Error("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;
|
||||
}
|
||||
return {
|
||||
just: { insertedId: result.just[0]?.id ?? NaN }
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
return {
|
||||
just: Array.from(this.mapById.values()),
|
||||
};
|
||||
}
|
||||
}
|
||||
12
server/resources/user/UserModel.ts
Normal file
12
server/resources/user/UserModel.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const UserSchema = z.object({
|
||||
id: z.number(),
|
||||
displayName: z.string(),
|
||||
email: z.string().email(),
|
||||
password: z.string().min(256).max(256),
|
||||
});
|
||||
|
||||
export const UserSchemaWithoutId = UserSchema.omit({ id: true });
|
||||
|
||||
export type UserModel = z.infer<typeof UserSchema>;
|
||||
56
server/resources/user/UserResource.ts
Normal file
56
server/resources/user/UserResource.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import {StoccaTreDbConn} from "../../database.ts";
|
||||
import StoccaTreRequest, {RouteDefinition} from "../../StoccaTreRequest.ts";
|
||||
import {Maybe} from "../../Maybe.ts";
|
||||
import {JSONObject} from "../../JSON.ts";
|
||||
import UserCollection from "./UserCollection.ts";
|
||||
import {UserSchemaWithoutId} from "./UserModel.ts";
|
||||
|
||||
export default class UserResource {
|
||||
private dbConnection: StoccaTreDbConn;
|
||||
private collection: UserCollection;
|
||||
private routes: Readonly<Record<string, RouteDefinition>> = {
|
||||
Add: {
|
||||
pattern: /\/add/,
|
||||
method: "POST"
|
||||
},
|
||||
GetAll: {
|
||||
pattern: /\/all/,
|
||||
method: "GET"
|
||||
},
|
||||
} as const;
|
||||
|
||||
constructor(dbConnection: StoccaTreDbConn) {
|
||||
this.dbConnection = dbConnection;
|
||||
this.collection = new UserCollection(dbConnection);
|
||||
}
|
||||
|
||||
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
||||
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" }};
|
||||
}
|
||||
|
||||
private async addUser(request: StoccaTreRequest): Promise<Maybe<{ insertedId: number }>> {
|
||||
const user = UserSchemaWithoutId.safeParse(JSON.parse(request.body ?? "{}"));
|
||||
if (!user.success) {
|
||||
return { error: new Error("Ingredient was malformed.") };
|
||||
}
|
||||
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;
|
||||
}
|
||||
return {
|
||||
just: {
|
||||
ingredients: Array.from(allUsers.just),
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
3
server/resources/user/main.ts
Normal file
3
server/resources/user/main.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from "./UserModel.ts";
|
||||
export * from "./UserCollection.ts";
|
||||
export * from "./UserResource.ts";
|
||||
Reference in New Issue
Block a user