db points
This commit is contained in:
177
.gitignore
vendored
177
.gitignore
vendored
@ -1,175 +1,2 @@
|
|||||||
# Based on https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore
|
node_modules
|
||||||
|
.mysql
|
||||||
# Logs
|
|
||||||
|
|
||||||
logs
|
|
||||||
_.log
|
|
||||||
npm-debug.log_
|
|
||||||
yarn-debug.log*
|
|
||||||
yarn-error.log*
|
|
||||||
lerna-debug.log*
|
|
||||||
.pnpm-debug.log*
|
|
||||||
|
|
||||||
# Caches
|
|
||||||
|
|
||||||
.cache
|
|
||||||
|
|
||||||
# Diagnostic reports (https://nodejs.org/api/report.html)
|
|
||||||
|
|
||||||
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
|
|
||||||
|
|
||||||
# Runtime data
|
|
||||||
|
|
||||||
pids
|
|
||||||
_.pid
|
|
||||||
_.seed
|
|
||||||
*.pid.lock
|
|
||||||
|
|
||||||
# Directory for instrumented libs generated by jscoverage/JSCover
|
|
||||||
|
|
||||||
lib-cov
|
|
||||||
|
|
||||||
# Coverage directory used by tools like istanbul
|
|
||||||
|
|
||||||
coverage
|
|
||||||
*.lcov
|
|
||||||
|
|
||||||
# nyc test coverage
|
|
||||||
|
|
||||||
.nyc_output
|
|
||||||
|
|
||||||
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
|
|
||||||
|
|
||||||
.grunt
|
|
||||||
|
|
||||||
# Bower dependency directory (https://bower.io/)
|
|
||||||
|
|
||||||
bower_components
|
|
||||||
|
|
||||||
# node-waf configuration
|
|
||||||
|
|
||||||
.lock-wscript
|
|
||||||
|
|
||||||
# Compiled binary addons (https://nodejs.org/api/addons.html)
|
|
||||||
|
|
||||||
build/Release
|
|
||||||
|
|
||||||
# Dependency directories
|
|
||||||
|
|
||||||
node_modules/
|
|
||||||
jspm_packages/
|
|
||||||
|
|
||||||
# Snowpack dependency directory (https://snowpack.dev/)
|
|
||||||
|
|
||||||
web_modules/
|
|
||||||
|
|
||||||
# TypeScript cache
|
|
||||||
|
|
||||||
*.tsbuildinfo
|
|
||||||
|
|
||||||
# Optional npm cache directory
|
|
||||||
|
|
||||||
.npm
|
|
||||||
|
|
||||||
# Optional eslint cache
|
|
||||||
|
|
||||||
.eslintcache
|
|
||||||
|
|
||||||
# Optional stylelint cache
|
|
||||||
|
|
||||||
.stylelintcache
|
|
||||||
|
|
||||||
# Microbundle cache
|
|
||||||
|
|
||||||
.rpt2_cache/
|
|
||||||
.rts2_cache_cjs/
|
|
||||||
.rts2_cache_es/
|
|
||||||
.rts2_cache_umd/
|
|
||||||
|
|
||||||
# Optional REPL history
|
|
||||||
|
|
||||||
.node_repl_history
|
|
||||||
|
|
||||||
# Output of 'npm pack'
|
|
||||||
|
|
||||||
*.tgz
|
|
||||||
|
|
||||||
# Yarn Integrity file
|
|
||||||
|
|
||||||
.yarn-integrity
|
|
||||||
|
|
||||||
# dotenv environment variable files
|
|
||||||
|
|
||||||
.env
|
|
||||||
.env.development.local
|
|
||||||
.env.test.local
|
|
||||||
.env.production.local
|
|
||||||
.env.local
|
|
||||||
|
|
||||||
# parcel-bundler cache (https://parceljs.org/)
|
|
||||||
|
|
||||||
.parcel-cache
|
|
||||||
|
|
||||||
# Next.js build output
|
|
||||||
|
|
||||||
.next
|
|
||||||
out
|
|
||||||
|
|
||||||
# Nuxt.js build / generate output
|
|
||||||
|
|
||||||
.nuxt
|
|
||||||
dist
|
|
||||||
|
|
||||||
# Gatsby files
|
|
||||||
|
|
||||||
# Comment in the public line in if your project uses Gatsby and not Next.js
|
|
||||||
|
|
||||||
# https://nextjs.org/blog/next-9-1#public-directory-support
|
|
||||||
|
|
||||||
# public
|
|
||||||
|
|
||||||
# vuepress build output
|
|
||||||
|
|
||||||
.vuepress/dist
|
|
||||||
|
|
||||||
# vuepress v2.x temp and cache directory
|
|
||||||
|
|
||||||
.temp
|
|
||||||
|
|
||||||
# Docusaurus cache and generated files
|
|
||||||
|
|
||||||
.docusaurus
|
|
||||||
|
|
||||||
# Serverless directories
|
|
||||||
|
|
||||||
.serverless/
|
|
||||||
|
|
||||||
# FuseBox cache
|
|
||||||
|
|
||||||
.fusebox/
|
|
||||||
|
|
||||||
# DynamoDB Local files
|
|
||||||
|
|
||||||
.dynamodb/
|
|
||||||
|
|
||||||
# TernJS port file
|
|
||||||
|
|
||||||
.tern-port
|
|
||||||
|
|
||||||
# Stores VSCode versions used for testing VSCode extensions
|
|
||||||
|
|
||||||
.vscode-test
|
|
||||||
|
|
||||||
# yarn v2
|
|
||||||
|
|
||||||
.yarn/cache
|
|
||||||
.yarn/unplugged
|
|
||||||
.yarn/build-state.yml
|
|
||||||
.yarn/install-state.gz
|
|
||||||
.pnp.*
|
|
||||||
|
|
||||||
# IntelliJ based IDEs
|
|
||||||
.idea
|
|
||||||
|
|
||||||
# Finder (MacOS) folder config
|
|
||||||
.DS_Store
|
|
||||||
|
|||||||
22
docker-compose.yml
Normal file
22
docker-compose.yml
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
services:
|
||||||
|
adminer:
|
||||||
|
image: adminer:latest
|
||||||
|
restart: always
|
||||||
|
environment:
|
||||||
|
ADMINER_DESIGN: 'dracula'
|
||||||
|
ports:
|
||||||
|
- 8082:8080
|
||||||
|
db:
|
||||||
|
image: mysql:lts
|
||||||
|
restart: always
|
||||||
|
environment:
|
||||||
|
MYSQL_ROOT_PASSWORD: password
|
||||||
|
ports:
|
||||||
|
- 8081:3306
|
||||||
|
volumes:
|
||||||
|
- ./.mysql:/var/lib/mysql
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
|
||||||
|
timeout: 20s
|
||||||
|
interval: 2s
|
||||||
|
retries: 10
|
||||||
18
drizzle.config.ts
Normal file
18
drizzle.config.ts
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
import { defineConfig } from "drizzle-kit";
|
||||||
|
|
||||||
|
export const dbCredentials = {
|
||||||
|
host: "127.0.0.1",
|
||||||
|
//host: "db",
|
||||||
|
port: 8081,
|
||||||
|
//port: 3306,
|
||||||
|
user: "root",
|
||||||
|
password: "password",
|
||||||
|
database: "sous-podzim2024-app"
|
||||||
|
};
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
dialect: "mysql",
|
||||||
|
schema: "./src/database/schema.ts",
|
||||||
|
out: "./drizzle",
|
||||||
|
dbCredentials: dbCredentials
|
||||||
|
});
|
||||||
@ -12,11 +12,15 @@
|
|||||||
"@types/ejs": "^3.1.5",
|
"@types/ejs": "^3.1.5",
|
||||||
"@types/express": "^4.17.21",
|
"@types/express": "^4.17.21",
|
||||||
"@types/multer": "^1.4.12",
|
"@types/multer": "^1.4.12",
|
||||||
|
"drizzle-kit": "^0.24.2",
|
||||||
|
"drizzle-orm": "^0.33.0",
|
||||||
"ejs": "^3.1.10",
|
"ejs": "^3.1.10",
|
||||||
"express": "^4.19.2",
|
"express": "^4.19.2",
|
||||||
"multer": "^1.4.5-lts.1"
|
"multer": "^1.4.5-lts.1",
|
||||||
|
"mysql2": "^3.11.0"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "bun --watch src/index.ts"
|
"dev": "bun --watch src/index.ts",
|
||||||
|
"push": "bun drizzle-kit push"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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 express from "express";
|
||||||
import ejs from "ejs";
|
import ejs from "ejs";
|
||||||
import multer from "multer";
|
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 app = express();
|
||||||
const port = 8080;
|
const port = 8080;
|
||||||
@ -10,19 +13,29 @@ let people = [
|
|||||||
{
|
{
|
||||||
id: 1,
|
id: 1,
|
||||||
name: "Adam",
|
name: "Adam",
|
||||||
secret: "adam"
|
secret: "adam",
|
||||||
|
points: []
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 2,
|
id: 2,
|
||||||
name: "Vojta",
|
name: "Vojta",
|
||||||
secret: "vojta"
|
secret: "vojta",
|
||||||
|
points: []
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
||||||
//app.use(express.urlencoded);
|
//app.use(express.urlencoded);
|
||||||
app.use(express.static('www'));
|
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) {
|
ejs.renderFile('src/templates/index.ejs', { people: people }, function (err, str) {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(500).send(err);
|
res.status(500).send(err);
|
||||||
@ -32,8 +45,23 @@ app.get("/", (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/gamble", (req, res) => {
|
app.get("/gamble/:id", async (req, res) => {
|
||||||
ejs.renderFile('src/templates/gamble.ejs', { people: people }, function (err, str) {
|
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) {
|
if (err) {
|
||||||
res.status(500).send(err);
|
res.status(500).send(err);
|
||||||
}
|
}
|
||||||
@ -42,13 +70,56 @@ app.get("/gamble", (req, res) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post("/gamble", upload.none(), (req, res) => {
|
app.post("/gamble/:id", upload.none(), async (req, res) => {
|
||||||
console.log(req.body);
|
let id = parseInt(req.params.id);
|
||||||
res.redirect('/');
|
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) => {
|
app.get("/person/:id", async (req, res) => {
|
||||||
let person = people.find((elem) => elem.id == req.params.id);
|
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) {
|
ejs.renderFile('src/templates/person.ejs', { person: person }, function (err, str) {
|
||||||
if (err) {
|
if (err) {
|
||||||
res.status(500).send(err);
|
res.status(500).send(err);
|
||||||
|
|||||||
@ -10,11 +10,11 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body data-bs-theme="dark">
|
<body data-bs-theme="dark">
|
||||||
<form action="/gamble" method="post" enctype="multipart/form-data">
|
<form action="/gamble/<%= person.personId %>" method="post" enctype="multipart/form-data">
|
||||||
<label for="fname">First name:</label><br>
|
<label for="fname">Points:</label><br>
|
||||||
<input type="text" id="fname" name="fname" value="John"><br>
|
<input type="number" id="points" name="points"><br>
|
||||||
<label for="lname">Last name:</label><br>
|
<label for="fname">Secret</label><br>
|
||||||
<input type="text" id="lname" name="lname" value="Doe"><br><br>
|
<input type="text" id="secret" name="secret"><br>
|
||||||
<input type="submit" value="Submit">
|
<input type="submit" value="Submit">
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@ -15,11 +15,11 @@
|
|||||||
<div class="row p-5">
|
<div class="row p-5">
|
||||||
<div class="list-group col-3">
|
<div class="list-group col-3">
|
||||||
<% people.forEach((person) => {%>
|
<% people.forEach((person) => {%>
|
||||||
<a href='/person/<%= person.id %>'
|
<a href='/person/<%= person.personId %>'
|
||||||
class="list-group-item list-group-item-action">
|
class="list-group-item list-group-item-action">
|
||||||
<div class="me-auto">
|
<div class="me-auto">
|
||||||
<span class="fw-bold"><%= person.name %></span>
|
<span class="fw-bold"><%= person.name %></span>
|
||||||
<%= person.id %>
|
<%= person.pointsTotal %>
|
||||||
</div>
|
</div>
|
||||||
</a>
|
</a>
|
||||||
<% }); %>
|
<% }); %>
|
||||||
|
|||||||
@ -11,15 +11,12 @@
|
|||||||
|
|
||||||
<body data-bs-theme="dark">
|
<body data-bs-theme="dark">
|
||||||
<a href="/">Index</a>
|
<a href="/">Index</a>
|
||||||
<a href="/gamble">gamble</a>
|
<a href="/gamble/<%= person.personId %>">gamble</a>
|
||||||
<p>
|
<p>
|
||||||
Name: <%= person.name %>
|
Name: <%= person.name %>
|
||||||
</p>
|
</p>
|
||||||
<p>
|
<p>
|
||||||
Id: <%= person.id %>
|
Points: <%= person.pointsTotal %>
|
||||||
</p>
|
|
||||||
<p>
|
|
||||||
secret: <%= person.secret %>
|
|
||||||
</p>
|
</p>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
Reference in New Issue
Block a user