db points
This commit is contained in:
7
src/database/db.ts
Normal file
7
src/database/db.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { dbCredentials } from "../../drizzle.config.ts";
|
||||
import { drizzle } from "drizzle-orm/mysql2";
|
||||
import mysql from "mysql2/promise";
|
||||
import * as schema from "./schema";
|
||||
|
||||
const connection = await mysql.createConnection(dbCredentials);
|
||||
export const db = drizzle(connection, { schema, mode: 'default' });
|
||||
25
src/database/schema.ts
Normal file
25
src/database/schema.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { relations } from "drizzle-orm";
|
||||
import { int, mysqlTable, varchar } from "drizzle-orm/mysql-core";
|
||||
|
||||
export const personTable = mysqlTable('person', {
|
||||
personId: int('personId').primaryKey().autoincrement(),
|
||||
name: varchar('name', { length: 255 }),
|
||||
secret: varchar('secret', { length: 255 }),
|
||||
});
|
||||
|
||||
export const personRelations = relations(personTable, ({ many }) => ({
|
||||
points: many(pointsTable)
|
||||
}));
|
||||
|
||||
export const pointsTable = mysqlTable('points', {
|
||||
pointsId: int('pointsId').primaryKey().autoincrement(),
|
||||
personId: int('personId').notNull().references(() => personTable.personId),
|
||||
points: int('points'),
|
||||
});
|
||||
|
||||
export const pointsRelations = relations(pointsTable, ({ one }) => ({
|
||||
person: one(personTable, {
|
||||
fields: [pointsTable.personId],
|
||||
references: [personTable.personId],
|
||||
})
|
||||
}));
|
||||
91
src/index.ts
91
src/index.ts
@ -1,6 +1,9 @@
|
||||
import express from "express";
|
||||
import ejs from "ejs";
|
||||
import multer from "multer";
|
||||
import { db } from "./database/db";
|
||||
import { eq, sql, sum } from "drizzle-orm";
|
||||
import { personTable, pointsTable } from "./database/schema";
|
||||
|
||||
const app = express();
|
||||
const port = 8080;
|
||||
@ -10,19 +13,29 @@ let people = [
|
||||
{
|
||||
id: 1,
|
||||
name: "Adam",
|
||||
secret: "adam"
|
||||
secret: "adam",
|
||||
points: []
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Vojta",
|
||||
secret: "vojta"
|
||||
secret: "vojta",
|
||||
points: []
|
||||
},
|
||||
]
|
||||
|
||||
//app.use(express.urlencoded);
|
||||
app.use(express.static('www'));
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
app.get("/", async (req, res) => {
|
||||
const people = await db.select({
|
||||
personId: personTable.personId,
|
||||
name: personTable.name,
|
||||
pointsTotal: sql`coalesce(sum(${pointsTable.points}), 0)`
|
||||
}).from(personTable)
|
||||
.leftJoin(pointsTable, eq(personTable.personId, pointsTable.personId))
|
||||
.groupBy(personTable.personId)
|
||||
|
||||
ejs.renderFile('src/templates/index.ejs', { people: people }, function (err, str) {
|
||||
if (err) {
|
||||
res.status(500).send(err);
|
||||
@ -32,8 +45,23 @@ app.get("/", (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
app.get("/gamble", (req, res) => {
|
||||
ejs.renderFile('src/templates/gamble.ejs', { people: people }, function (err, str) {
|
||||
app.get("/gamble/:id", async (req, res) => {
|
||||
let id = parseInt(req.params.id);
|
||||
if (id == undefined) {
|
||||
res.status(404).send("Invalid person id");
|
||||
return;
|
||||
}
|
||||
|
||||
let person = await db.query.personTable.findFirst({
|
||||
where: eq(personTable.personId, id)
|
||||
})
|
||||
|
||||
if (!person) {
|
||||
res.status(404).send("Invalid person id");
|
||||
return;
|
||||
}
|
||||
|
||||
ejs.renderFile('src/templates/gamble.ejs', { person: person }, function (err, str) {
|
||||
if (err) {
|
||||
res.status(500).send(err);
|
||||
}
|
||||
@ -42,13 +70,56 @@ app.get("/gamble", (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/gamble", upload.none(), (req, res) => {
|
||||
console.log(req.body);
|
||||
res.redirect('/');
|
||||
app.post("/gamble/:id", upload.none(), async (req, res) => {
|
||||
let id = parseInt(req.params.id);
|
||||
if (id == undefined) {
|
||||
res.status(404).send("Invalid person id");
|
||||
return;
|
||||
}
|
||||
|
||||
let person = await db.query.personTable.findFirst({
|
||||
where: eq(personTable.personId, id)
|
||||
})
|
||||
|
||||
if (!person) {
|
||||
res.status(404).send("Invalid person id");
|
||||
return;
|
||||
}
|
||||
|
||||
if (person.secret != req.body['secret']) {
|
||||
res.sendStatus(403);
|
||||
return;
|
||||
}
|
||||
|
||||
await db.insert(pointsTable).values({
|
||||
personId: id,
|
||||
points: req.body['points']
|
||||
})
|
||||
|
||||
res.redirect('/person/' + id);
|
||||
});
|
||||
|
||||
app.get("/person/:id", (req, res) => {
|
||||
let person = people.find((elem) => elem.id == req.params.id);
|
||||
app.get("/person/:id", async (req, res) => {
|
||||
let id = parseInt(req.params.id);
|
||||
if (id == undefined) {
|
||||
res.status(404).send("Invalid person id");
|
||||
return;
|
||||
}
|
||||
|
||||
let person = (await db.select({
|
||||
personId: personTable.personId,
|
||||
name: personTable.name,
|
||||
pointsTotal: sql`coalesce(sum(${pointsTable.points}), 0)`
|
||||
}).from(personTable)
|
||||
.leftJoin(pointsTable, eq(personTable.personId, pointsTable.personId))
|
||||
.where(eq(personTable.personId, id))
|
||||
.groupBy(personTable.personId))[0];
|
||||
|
||||
if (!person) {
|
||||
res.status(404).send("Invalid person id");
|
||||
return;
|
||||
}
|
||||
|
||||
ejs.renderFile('src/templates/person.ejs', { person: person }, function (err, str) {
|
||||
if (err) {
|
||||
res.status(500).send(err);
|
||||
|
||||
@ -10,11 +10,11 @@
|
||||
</head>
|
||||
|
||||
<body data-bs-theme="dark">
|
||||
<form action="/gamble" method="post" enctype="multipart/form-data">
|
||||
<label for="fname">First name:</label><br>
|
||||
<input type="text" id="fname" name="fname" value="John"><br>
|
||||
<label for="lname">Last name:</label><br>
|
||||
<input type="text" id="lname" name="lname" value="Doe"><br><br>
|
||||
<form action="/gamble/<%= person.personId %>" method="post" enctype="multipart/form-data">
|
||||
<label for="fname">Points:</label><br>
|
||||
<input type="number" id="points" name="points"><br>
|
||||
<label for="fname">Secret</label><br>
|
||||
<input type="text" id="secret" name="secret"><br>
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
</body>
|
||||
|
||||
@ -15,11 +15,11 @@
|
||||
<div class="row p-5">
|
||||
<div class="list-group col-3">
|
||||
<% people.forEach((person) => {%>
|
||||
<a href='/person/<%= person.id %>'
|
||||
<a href='/person/<%= person.personId %>'
|
||||
class="list-group-item list-group-item-action">
|
||||
<div class="me-auto">
|
||||
<span class="fw-bold"><%= person.name %></span>
|
||||
<%= person.id %>
|
||||
<%= person.pointsTotal %>
|
||||
</div>
|
||||
</a>
|
||||
<% }); %>
|
||||
|
||||
@ -11,15 +11,12 @@
|
||||
|
||||
<body data-bs-theme="dark">
|
||||
<a href="/">Index</a>
|
||||
<a href="/gamble">gamble</a>
|
||||
<a href="/gamble/<%= person.personId %>">gamble</a>
|
||||
<p>
|
||||
Name: <%= person.name %>
|
||||
</p>
|
||||
<p>
|
||||
Id: <%= person.id %>
|
||||
</p>
|
||||
<p>
|
||||
secret: <%= person.secret %>
|
||||
Points: <%= person.pointsTotal %>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user