VIDEO

Nech si sestříhat total brutal video nebo si nechcej připravit animaci svého loga.

  • Krátké
  • Dlouhé
  • Střih
  • Efekty

// Jednoduchý rezervační systém (Node.js + Express + SQLite)
// Instalace:
// npm install express sqlite3

const express = require(‚express‘);
const sqlite3 = require(‚sqlite3‘).verbose();
const app = express();
app.use(express.json());

// DB inicializace
const db = new sqlite3.Database(‚db.sqlite‘);
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS items (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
description TEXT,
qr_code TEXT,
status TEXT DEFAULT ‚available‘,
current_reservation_id INTEGER
)`);

db.run(`CREATE TABLE IF NOT EXISTS reservations (
id INTEGER PRIMARY KEY AUTOINCREMENT,
item_id INTEGER,
user_name TEXT,
user_contact TEXT,
borrowed_at TEXT,
returned_at TEXT
)`);
});

// Získání detailu výrobku
app.get(‚/item/:qr‘, (req, res) => {
db.get(`SELECT * FROM items WHERE qr_code = ?`, [req.params.qr], (err, item) => {
if (err || !item) return res.status(404).json({ error: ‚Item not found‘ });

if (item.current_reservation_id) {
db.get(`SELECT * FROM reservations WHERE id = ?`, [item.current_reservation_id], (e, reservation) => {
return res.json({ item, reservation });
});
} else {
return res.json({ item, reservation: null });
}
});
});

// Půjčení
app.post(‚/borrow/:qr‘, (req, res) => {
const { user_name, user_contact } = req.body;

db.get(`SELECT * FROM items WHERE qr_code = ?`, [req.params.qr], (err, item) => {
if (!item) return res.status(404).json({ error: ‚Item not found‘ });
if (item.status === ‚borrowed‘) return res.status(400).json({ error: ‚Item already borrowed‘ });

const now = new Date().toISOString();

db.run(`INSERT INTO reservations (item_id, user_name, user_contact, borrowed_at) VALUES (?, ?, ?, ?)`,
[item.id, user_name, user_contact, now], function () {
const reservationId = this.lastID;
db.run(`UPDATE items SET status = ‚borrowed‘, current_reservation_id = ? WHERE id = ?`,
[reservationId, item.id], () => {
res.json({ success: true, reservation_id: reservationId });
});
});
});
});

// Vrácení
app.post(‚/return/:qr‘, (req, res) => {
db.get(`SELECT * FROM items WHERE qr_code = ?`, [req.params.qr], (err, item) => {
if (!item) return res.status(404).json({ error: ‚Item not found‘ });
if (!item.current_reservation_id) return res.status(400).json({ error: ‚Item is not borrowed‘ });

const now = new Date().toISOString();

db.run(`UPDATE reservations SET returned_at = ? WHERE id = ?`,
[now, item.current_reservation_id], () => {
db.run(`UPDATE items SET status = ‚available‘, current_reservation_id = NULL WHERE id = ?`,
[item.id], () => {
res.json({ success: true });
});
});
});
});

app.listen(3000, () => console.log(‚Server běží na http://localhost:3000‘));

// — Jednoduché HTML rozhraní —
// Stránka detailu položky
app.get(‚/ui/item/:qr‘, (req, res) => {
db.get(`SELECT * FROM items WHERE qr_code = ?`, [req.params.qr], (err, item) => {
if (!item) return res.send(‚

Položka nenalezena

‚);

const formBorrow = `

Půjčit výrobek



`;

const btnReturn = `


`;

if (item.status === ‚available‘) {
res.send(`

${item.name}

${item.description}

Status: VOLNÝ

${formBorrow}
`);
} else {
db.get(`SELECT * FROM reservations WHERE id = ?`, [item.current_reservation_id], (err2, r) => {
res.send(`

${item.name}

${item.description}

Status: PŮJČENO

Půjčil: ${r.user_name}

Kontakt: ${r.user_contact}

Čas půjčení: ${r.borrowed_at}

${btnReturn}
`);
});
}
});
});

// Formulářové POSTy
app.use(require(‚express‘).urlencoded({ extended: true }));

app.post(‚/ui/borrow/:qr‘, (req, res) => {
const { user_name, user_contact } = req.body;
db.get(`SELECT * FROM items WHERE qr_code = ?`, [req.params.qr], (err, item) => {
if (!item) return res.send(‚Položka nenalezena‘);
if (item.status === ‚borrowed‘) return res.send(‚Výrobek je už půjčen‘);

const now = new Date().toISOString();
db.run(`INSERT INTO reservations (item_id, user_name, user_contact, borrowed_at) VALUES (?,?,?,?)`,
[item.id, user_name, user_contact, now], function () {
db.run(`UPDATE items SET status=’borrowed‘, current_reservation_id=? WHERE id=?`,
[this.lastID, item.id], () => {
res.redirect(`/ui/item/${req.params.qr}`);
});
}
);
});
});

app.post(‚/ui/return/:qr‘, (req, res) => {
db.get(`SELECT * FROM items WHERE qr_code = ?`, [req.params.qr], (err, item) => {
if (!item) return res.send(‚Položka nenalezena‘);
if (!item.current_reservation_id) return res.send(‚Výrobek není půjčen‘);

const now = new Date().toISOString();
db.run(`UPDATE reservations SET returned_at=? WHERE id=?`, [now, item.current_reservation_id], () => {
db.run(`UPDATE items SET status=’available‘, current_reservation_id=NULL WHERE id=?`, [item.id], () => {
res.redirect(`/ui/item/${req.params.qr}`);
});
});
});
});