Add Backend Project and add Tournament Detail Page
This commit is contained in:
68
backend_splatournament_manager/src/app.ts
Normal file
68
backend_splatournament_manager/src/app.ts
Normal file
@@ -0,0 +1,68 @@
|
||||
import express, {Request, Response} from 'express';
|
||||
import bodyParser from 'body-parser';
|
||||
import 'dotenv/config';
|
||||
|
||||
import {TournamentService} from './services/tournament-service';
|
||||
import router from './middlewares/logger';
|
||||
|
||||
|
||||
const tournamentService = new TournamentService();
|
||||
const port = process.env.PORT || 3000;
|
||||
const app = express();
|
||||
|
||||
app.use(bodyParser.json());
|
||||
app.use(router);
|
||||
|
||||
app.get('/tournaments/available', async (req: Request, res: Response) => {
|
||||
console.log(req.params)
|
||||
if (!req.params.id) {
|
||||
const tournaments = await tournamentService.getAllTournaments();
|
||||
console.log(tournaments)
|
||||
return res.send(tournaments);
|
||||
}
|
||||
const tournament = await tournamentService.getBookById(+req.params.id);
|
||||
if (!tournament) {
|
||||
return res.status(404).send({error: 'Book not found'});
|
||||
}
|
||||
res.send(tournament);
|
||||
});
|
||||
|
||||
app.post('/books', async (req: Request, res: Response) => {
|
||||
console.log("post");
|
||||
|
||||
try {
|
||||
await tournamentService.addBook(req.body);
|
||||
res.status(200).send();
|
||||
}catch (err){
|
||||
console.log(err);
|
||||
res.status(404).send();
|
||||
}
|
||||
});
|
||||
|
||||
app.put('/books', async (req: Request, res: Response) => {
|
||||
if (!req.query.id) {
|
||||
return res.status(400).send({error: 'Missing id parameter'});
|
||||
}
|
||||
try {
|
||||
const success = await tournamentService.updateBook(+req.query.id!, req.body);
|
||||
} catch (err) {
|
||||
return res.status(400).send({error: 'Failed to update book'});
|
||||
}
|
||||
res.status(200).send({message: 'Book updated successfully'});
|
||||
});
|
||||
|
||||
app.delete('/books', async (req: Request, res: Response) => {
|
||||
if (!req.query.id) {
|
||||
return res.status(400).send({error: 'Missing id parameter'});
|
||||
}
|
||||
try {
|
||||
const success = await tournamentService.deleteBook(+req.query.id!);
|
||||
} catch (err) {
|
||||
return res.status(400).send({error: 'Failed to delete book'});
|
||||
}
|
||||
res.status(200).send({message: 'Book deleted successfully'});
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`server started on port ${port}`);
|
||||
});
|
||||
20
backend_splatournament_manager/src/middlewares/logger.ts
Normal file
20
backend_splatournament_manager/src/middlewares/logger.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import express from 'express';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
|
||||
|
||||
const filePath = path.join(process.cwd(), 'request_logs.txt');
|
||||
const router = express.Router();
|
||||
|
||||
router.use((req, res, next) => {
|
||||
const log = `[${new Date().toLocaleString()}] ${req.method} ${req.url}\n`;
|
||||
console.log(log);
|
||||
|
||||
fs.appendFile(filePath, log, (err) => {
|
||||
if (err) console.error("Request Failed", err);
|
||||
});
|
||||
|
||||
next();
|
||||
});
|
||||
|
||||
export = router;
|
||||
7
backend_splatournament_manager/src/models/tournament.ts
Normal file
7
backend_splatournament_manager/src/models/tournament.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export interface Tournament {
|
||||
id:number;
|
||||
name:String;
|
||||
description:String;
|
||||
maxTeamAmount:number;
|
||||
currentTeamAmount:number;
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
import {Tournament} from '../models/tournament';
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import {Database, RunResult} from "sqlite3";
|
||||
|
||||
export class TournamentService {
|
||||
private csvFilename = 'csv/tournaments.csv';
|
||||
private dbFilename = 'tournaments.sqlite';
|
||||
private db: Database;
|
||||
|
||||
constructor() {
|
||||
fs.unlinkSync(this.dbFilename);
|
||||
this.db = new Database(this.dbFilename);
|
||||
this.db.serialize(() => {
|
||||
this.db.run(`CREATE TABLE IF NOT EXISTS Tournaments
|
||||
(
|
||||
id
|
||||
INTEGER
|
||||
PRIMARY
|
||||
KEY
|
||||
AUTOINCREMENT,
|
||||
name
|
||||
TEXT,
|
||||
description
|
||||
TEXT,
|
||||
maxTeamAmount
|
||||
INTEGER,
|
||||
currentTeamAmount
|
||||
INTEGER
|
||||
)`);
|
||||
})
|
||||
this.seedDb();
|
||||
}
|
||||
|
||||
private seedDb() {
|
||||
fs.readFile(path.join(process.cwd(), "dist", this.csvFilename), 'utf-8', (_, data) => {
|
||||
const entries = data.split('\n');
|
||||
entries.shift();
|
||||
const statement = this.db.prepare("INSERT INTO Tournaments ( name, description, maxTeamAmount, currentTeamAmount) VALUES (?, ?, ?, ?)");
|
||||
entries.forEach(line => {
|
||||
if (line) {
|
||||
const parts = line.split(',');
|
||||
const tournament = {
|
||||
id: 0,
|
||||
name: parts[0].trim(),
|
||||
description: parts[1].trim(),
|
||||
maxTeamAmount: +parts[2],
|
||||
currentTeamAmount: +parts[3]
|
||||
} as Tournament;
|
||||
statement.run(tournament.name, tournament.description, tournament.maxTeamAmount, tournament.currentTeamAmount);
|
||||
console.log(tournament)
|
||||
}
|
||||
});
|
||||
statement.finalize();
|
||||
});
|
||||
}
|
||||
|
||||
getAllTournaments(): Promise<Tournament[]> {
|
||||
return new Promise<Tournament[]>((resolve, reject) => {
|
||||
this.db.all(`SELECT *
|
||||
FROM Tournaments`,
|
||||
(err: RunResult, rows: Tournament[]) => {
|
||||
if (!err) {
|
||||
resolve(rows);
|
||||
}
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
getBookById(id: number): Promise<Tournament> {
|
||||
return new Promise<Tournament>((resolve, reject) => {
|
||||
this.db.get(`Select *
|
||||
From Tournaments
|
||||
WHERE id = ${id}`, (err: RunResult, book: Tournament) => {
|
||||
if (!err) {
|
||||
resolve(book);
|
||||
}
|
||||
reject(err);
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
addBook(tournament: Tournament): Promise<void> {
|
||||
return new Promise<void>((resolve, reject) => {
|
||||
const statement = this.db.prepare('Insert Into Tournaments (name, description, maxTeamAmount, currentTeamAmount) VALUES (?, ?, ?, ?)')
|
||||
statement.run(tournament.name, tournament.description, tournament.maxTeamAmount, tournament.currentTeamAmount);
|
||||
resolve();
|
||||
})
|
||||
}
|
||||
|
||||
updateBook(id: number, updatedBook: Tournament): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.run(`Update Tournaments
|
||||
Set name = $name,
|
||||
description = $description,
|
||||
maxTeamAmount = $maxTeamAmount,
|
||||
currentTeamAmount = $currentTeamAmount
|
||||
where id = $id`,
|
||||
{
|
||||
$id: id,
|
||||
$description: updatedBook.description,
|
||||
$maxTeamAmount: updatedBook.maxTeamAmount,
|
||||
$currentTeamAmount: updatedBook.currentTeamAmount,
|
||||
});
|
||||
resolve();
|
||||
})
|
||||
}
|
||||
|
||||
deleteBook(id: number): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.db.run('Delete From Tournaments where id = $id', {
|
||||
$id: id,
|
||||
});
|
||||
resolve();
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user