feat: things are happening...
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import { StoccaTreDbConn, WithoutId } from "../../database.ts";
|
||||
import { Q, StoccaTreDbConn, WithoutId } from "../../database.ts";
|
||||
import { IngredientModel } from "./IngredientModel.ts";
|
||||
import { Maybe } from "../../Maybe.ts";
|
||||
import { Result, StoccaTreError } from "../../Result.ts";
|
||||
|
||||
const TABLE_NAME = "main.ingredients";
|
||||
|
||||
export default class IngredientCollection {
|
||||
private db: StoccaTreDbConn;
|
||||
@@ -10,36 +12,38 @@ export default class IngredientCollection {
|
||||
this.db = database;
|
||||
}
|
||||
|
||||
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<Maybe<IngredientModel[]>> {
|
||||
return await this.db.query(sql =>
|
||||
sql<IngredientModel[]>`INSERT INTO ingredients ${ sql(ingredient) }`
|
||||
);
|
||||
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<Result<IngredientModel>> {
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`INSERT INTO ${ TABLE_NAME } VALUES (DEFAULT, $name, $displayName, $displayNameDE) RETURNING *;`, ingredient);
|
||||
if (error) {
|
||||
return [error];
|
||||
}
|
||||
if (result.length > 0) {
|
||||
return [, result[0]];
|
||||
}
|
||||
return [new StoccaTreError("Ingredient wasn't inserted!")];
|
||||
}
|
||||
|
||||
async getById(id: number): Promise<Maybe<IngredientModel>> {
|
||||
async getById(id: number): Promise<Result<IngredientModel>> {
|
||||
const found = this.mapById.get(id);
|
||||
if (found) {
|
||||
return { just: found };
|
||||
return [, found];
|
||||
}
|
||||
const result = await this.db.query(sql => sql<IngredientModel[]>`SELECT * FROM ingredients WHERE id is ${ id }`);
|
||||
if (result.error) {
|
||||
return result;
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ TABLE_NAME } WHERE id is $id`, { id });
|
||||
if (error) {
|
||||
return [error];
|
||||
}
|
||||
const ingredient = result.just[0];
|
||||
const ingredient = result[0];
|
||||
this.mapById.set(ingredient.id, ingredient);
|
||||
return { just: ingredient };
|
||||
return [, ingredient];
|
||||
}
|
||||
async getAllIngredients(): Promise<Maybe<IterableIterator<IngredientModel>>> {
|
||||
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));
|
||||
|
||||
async getAllIngredients(): Promise<Result<IterableIterator<IngredientModel>>> {
|
||||
const [error, result] = <Q<IngredientModel>> await this.db.query(`SELECT * FROM ${ TABLE_NAME }`);
|
||||
if (!error) {
|
||||
result.forEach((ingredient: IngredientModel) => this.mapById.set(ingredient.id, ingredient));
|
||||
} else {
|
||||
return result;
|
||||
return [error];
|
||||
}
|
||||
return {
|
||||
just: this.mapById.values(),
|
||||
};
|
||||
return [, this.mapById.values()];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,4 +9,6 @@ export const IngredientSchema = z.object({
|
||||
|
||||
export const IngredientSchemaWithoutId = IngredientSchema.omit({ id: true });
|
||||
|
||||
export type IngredientModel = z.infer<typeof IngredientSchema>;
|
||||
|
||||
export type IngredientModel = z.infer<typeof IngredientSchema>;
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import {StoccaTreDbConn} from "../../database.ts";
|
||||
import { StoccaTreDbConn } from "../../database.ts";
|
||||
import IngredientCollection from "./IngredientCollection.ts";
|
||||
import StoccaTreRequest, {RouteDefinition} from "../../StoccaTreRequest.ts";
|
||||
import {Maybe} from "../../Maybe.ts";
|
||||
import {JSONObject} from "../../JSON.ts";
|
||||
import {IngredientSchemaWithoutId} from "./IngredientModel.ts";
|
||||
import StoccaTreRequest, { RouteDefinition } from "../../StoccaTreRequest.ts";
|
||||
import {Result, StoccaTreError} from "../../Result.ts";
|
||||
import { JSONObject } from "../../JSON.ts";
|
||||
import { IngredientModel, IngredientSchemaWithoutId } from "./IngredientModel.ts";
|
||||
|
||||
export default class IngredientResource {
|
||||
private dbConnection: StoccaTreDbConn;
|
||||
@@ -11,11 +11,11 @@ export default class IngredientResource {
|
||||
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,40 @@ export default class IngredientResource {
|
||||
this.collection = new IngredientCollection(dbConnection);
|
||||
}
|
||||
|
||||
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
|
||||
async handleRequest(request: StoccaTreRequest): Promise<Result<JSONObject> | null> {
|
||||
let result;
|
||||
if (request.match(this.routes.Add)) {
|
||||
return await this.addIngredient(request);
|
||||
result = await this.addIngredient(request);
|
||||
}
|
||||
if (request.match(this.routes.GetAll)) {
|
||||
return await this.allIngredients(request);
|
||||
result = await this.allIngredients(request);
|
||||
}
|
||||
return { error: { message: "Invalid route" }};
|
||||
if (result) {
|
||||
const [error] = result;
|
||||
if (error) {
|
||||
return [error.qualified("Could not fulfill ingredient request: ")];
|
||||
} else {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private async addIngredient(request: StoccaTreRequest): Promise<Maybe<{ insertedId: number }>> {
|
||||
const ingredient = IngredientSchemaWithoutId.safeParse(JSON.parse(request.body ?? "{}"));
|
||||
private async addIngredient(request: StoccaTreRequest): Promise<Result<{ id: number }>> {
|
||||
const ingredient = IngredientSchemaWithoutId.safeParse(request.body);
|
||||
if (!ingredient.success) {
|
||||
return { error: new Error("Ingredient was malformed.") };
|
||||
return [new StoccaTreError("Ingredient definition was malformed.", 400)];
|
||||
}
|
||||
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;
|
||||
private async allIngredients(request: StoccaTreRequest): Promise<Result<JSONObject>> {
|
||||
const [error, ingredients] = await this.collection.getAllIngredients();
|
||||
if (error) {
|
||||
return [error];
|
||||
}
|
||||
return {
|
||||
just: {
|
||||
ingredients: Array.from(getAllIngredientResult.just),
|
||||
},
|
||||
};
|
||||
return [, {
|
||||
ingredients: Array.from(ingredients),
|
||||
}];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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())];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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>;
|
||||
|
||||
@@ -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 }];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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";
|
||||
|
||||
Reference in New Issue
Block a user