fix: removed collage
fix: removing collage feat: added ingredient type to server for testing and a config" chore: setup deno server package
This commit is contained in:
34
server/Ingredient.ts
Normal file
34
server/Ingredient.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { StoccaTreDbConn, WithoutId } from "./database.ts";
|
||||
|
||||
export type Ingredient = {
|
||||
id: number,
|
||||
name: string,
|
||||
displayName: string,
|
||||
displayNameDE: string,
|
||||
};
|
||||
|
||||
export default class IngredientService {
|
||||
private dbConnection: StoccaTreDbConn;
|
||||
private mapById: Map<number, Ingredient> = new Map<number, Ingredient>();
|
||||
private allGotten = false;
|
||||
|
||||
constructor(database: StoccaTreDbConn) {
|
||||
this.dbConnection = database;
|
||||
}
|
||||
|
||||
async addIngredient(ingredient: WithoutId<Ingredient>): Promise<any> {
|
||||
const result = await this.dbConnection.query<any>(
|
||||
`INSERT INTO ingredients (id, name, displayName, displayNameDE) VALUES (NULL, '${ingredient.name}', '${ingredient.displayName}', '${ingredient.displayNameDE}');`
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
async getAllIngredients(): Promise<IterableIterator<Ingredient>> {
|
||||
if (!this.allGotten) {
|
||||
const result = await this.dbConnection.query<Ingredient[]>("SELECT * FROM ingredients");
|
||||
result.forEach((ingredient) => this.mapById.set(ingredient.id, ingredient));
|
||||
this.allGotten = true;
|
||||
}
|
||||
return this.mapById.values();
|
||||
}
|
||||
}
|
||||
16
server/database.ts
Normal file
16
server/database.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { Client } from "https://deno.land/x/mysql/mod.ts";
|
||||
import dbconfig from "./config.json" assert { type: "json" };
|
||||
|
||||
export type WithoutId<T> = Omit<T, "id">;
|
||||
export interface StoccaTreDbConn {
|
||||
query<T>(query: string): Promise<T>;
|
||||
}
|
||||
export default async function createNewDbConnection(): Promise<StoccaTreDbConn> {
|
||||
return await new Client().connect({
|
||||
hostname: dbconfig.hostname,
|
||||
username: dbconfig.username,
|
||||
db: "stocca_tre",
|
||||
password: dbconfig.password,
|
||||
});
|
||||
}
|
||||
|
||||
25
server/main.ts
Normal file
25
server/main.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import createNewDbConnection from "./database.ts";
|
||||
import IngredientService from "./Ingredient.ts";
|
||||
|
||||
const server = Deno.listen({ port: 8080 });
|
||||
|
||||
console.log(`HTTP webserver running. Access it at: http://localhost:8080/`);
|
||||
|
||||
const database = await createNewDbConnection();
|
||||
const ingredientService = new IngredientService(database);
|
||||
|
||||
for await (const conn of server) {
|
||||
serveHttp(conn);
|
||||
}
|
||||
|
||||
async function serveHttp(conn: Deno.Conn) {
|
||||
const httpConn = Deno.serveHttp(conn);
|
||||
|
||||
for await (const requestEvent of httpConn) {
|
||||
const body = [];
|
||||
for (const ingredient of (await ingredientService.getAllIngredients())) {
|
||||
body.push(ingredient);
|
||||
}
|
||||
requestEvent.respondWith(Response.json(body, { status: 200 }));
|
||||
}
|
||||
}
|
||||
10
server/package.json
Normal file
10
server/package.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "stocca-tre-server",
|
||||
"version": "0.0.1",
|
||||
"scripts": {
|
||||
"start": "deno run --allow-net main.ts"
|
||||
},
|
||||
"description": "Backend for StoccaTre",
|
||||
"author": "Daniel Ledda",
|
||||
"license": "MIT"
|
||||
}
|
||||
Reference in New Issue
Block a user