feat: things are happening...
This commit is contained in:
@@ -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 }];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user