Skip to content

Commit

Permalink
server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
PJvanDiepen committed Jul 5, 2024
1 parent f5bf6b5 commit 6eb2905
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 63 deletions.
82 changes: 43 additions & 39 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,36 @@ const tijdstip = new Date();
const os = require("os");
const db = require("./modules/db.cjs");

// TODO ronden per club / seizoen toevoegen voor backend en frontend

db.database.clubs.push({
clubCode: 0,
vereniging: "Waagtoren",
seizoenen: [
"1819",
"1920",
"2021",
"2122",
"2223",
"2324"]
/*
TODO seizoenen [{seizoen: en voluit: }, ...]
TODO teams
TODO competities
TODO uitslagen
TODO personen invullen
*/

const database = {
personen: [],
clubs: [{ // per club: seizoenen, ronden, uitslagen
clubCode: 0,
vereniging: "Waagtoren",
seizoenen: [
{seizoen: "1819"},
{seizoen: "1920"},
{seizoen: "2021"},
{seizoen: "2122"},
{seizoen: "2223"},
{seizoen: "2324"}]
}, {
clubCode: 1,
vereniging: "Jeugd Waagtoren",
seizoenen: [
"2309",
"2401"]
});
clubCode: 1,
vereniging: "Jeugd Waagtoren",
seizoenen: [
{seizoen: "2309"},
{seizoen: "2401"}]
}],
teams: [], // per team: spelers
competities: [] // per competitie: ranglijst
}

const laatsteMutaties = [];
let uniekeMutaties = 0;
Expand Down Expand Up @@ -94,21 +105,29 @@ function teamCodes(competities) {
*/
module.exports = function (url) {

console.log("--- endpoints ---"); // TODO haal tekst uit db.apiLijst en definieer function hier

// endpoints nieuwe stijl ------------------------------------------------------------------------------------------

url.get("/database", async function (ctx) {
ctx.body = JSON.stringify(db.database);
ctx.body = JSON.stringify(database);
});

/*
Frontend: start.js
*/
url.get("/:club/seizoenen", async function (ctx) {
ctx.body = JSON.stringify(db.database.clubs[ctx.params.club].seizoenen);
ctx.body = JSON.stringify(database.clubs[ctx.params.club].seizoenen);
});

url.get("/:club/:seizoen/ronden", async function (ctx) {
// TODO inlezen indien nodig
const ronden = await Ronde.query()
.where("team.clubCode", ctx.params.club)
.where("team.seizoen", ctx.params.seizoen)
.orderBy(["datum","rondeNummer"]);
// TODO stap voor stap, niet in 1 regel
ctx.body = JSON.stringify(database.clubs[ctx.params.club].ronden[ctx.params.seizoen]);
});

console.log("--- endpoints ---");

// geef values zonder keys van 1 kolom -----------------------------------------------------------------------------

/*
Expand Down Expand Up @@ -284,21 +303,6 @@ module.exports = function (url) {
ctx.body = persoon.length > 0 ? persoon[0] : {naam: "onbekend", knsbNummer: 0};
});

/*
Frontend: bestuur.js
lid.js
o_o_o.js
start.js
team.js
*/
url.get("/:club/:seizoen/teams", async function (ctx) {
ctx.body = await Team.query()
.select("team.*", "persoon.naam")
.join("persoon", "team.teamleider", "persoon.knsbNummer")
.where("team.clubCode", ctx.params.club)
.where("team.seizoen", ctx.params.seizoen);
});

/*
-- ronden per seizoen en competitie met aantal uitslagen TODO wordt aantalResultaten ergens gebruikt?
with u as
Expand Down
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const registerApi = require('./api');

const { Model } = require('objection');

// Tot mei 2023 was Knex(config.get('knex')) nog voldoende.
const knex = Knex(JSON.parse(JSON.stringify(config.get('knex'))));

Model.knex(knex);
Expand Down
11 changes: 0 additions & 11 deletions modules/db.cjs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/*
* Deze module bevat alle code voor het valideren van de velden in de tabellen van de MySQL database.
*
* TODO alle code voor het maken van een structuur (zoals in o_o_o.js: perTeamRondenUitslagen, ranglijst enz.)
* met laatste mutatie tijdstippen voor synchroniseren tussen server en browser
*
* Van deze module bestaan twee versies:
* - een Common.js versie voor node.js: db.cjs met module.exports = { .. };
* - een ES6 versie voor de browser: db.js met export { .. };
Expand All @@ -16,13 +13,6 @@

const apiLijst = []; // zie app.js

const database = {
personen: [],
clubs: [], // per club: seizoenen, ronden, uitslagen
teams: [], // per team: spelers
competities: [] // per competitie: ranglijst
}

/**
* key vertaalt object naar string voor api-call met :club/:seizoen/:team/:ronde/:speler
*
Expand Down Expand Up @@ -196,7 +186,6 @@ const MENU = "menu";

module.exports = { // CommonJS voor node.js
apiLijst,
database,

key, // (object)

Expand Down
11 changes: 0 additions & 11 deletions public/js/db.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/*
* Deze module bevat alle code voor het valideren van de velden in de tabellen van de MySQL database.
*
* TODO alle code voor het maken van een structuur (zoals in o_o_o.js: perTeamRondenUitslagen, ranglijst enz.)
* met laatste mutatie tijdstippen voor synchroniseren tussen server en browser
*
* Van deze module bestaan twee versies:
* - een Common.js versie voor node.js: db.cjs met module.exports = { .. };
* - een ES6 versie voor de browser: db.js met export { .. };
Expand All @@ -16,13 +13,6 @@

const apiLijst = []; // zie app.js

const database = {
personen: [],
clubs: [], // per club: seizoenen, ronden, uitslagen
teams: [], // per team: spelers
competities: [] // per competitie: ranglijst
}

/**
* key vertaalt object naar string voor api-call met :club/:seizoen/:team/:ronde/:speler
*
Expand Down Expand Up @@ -196,7 +186,6 @@ const MENU = "menu";

export { // ES6 voor browser,
apiLijst,
database,

key, // (object)

Expand Down
42 changes: 42 additions & 0 deletions public/js/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// because this is a module, I'm strict by default

const database = {};


/**
* localFetch optimaliseert de verbinding met de database op de server
* door het antwoord van de server ook lokaal op te slaan
*
* @param url de vraag aan de database op de server
* @returns {Promise<any>} het antwoord van de server
*/
async function localFetch(url) {
let object = JSON.parse(sessionStorage.getItem(url));
if (!object) {
object = await serverFetch(url);
sessionStorage.setItem(url, JSON.stringify(object));
}
return object;
}

/**
* serverFetch maakt verbinding met de database voor actuele situatie
*
* @param url de vraag aan de database op de server
* @returns {Promise<any>} het antwoord van de server
*/
async function serverFetch(url) {
try {
const response = await fetch(server + url);
if (response.ok) {
return await response.json();
} else {
console.log(`--- serverFetch ---`);
console.log(response);
return false;
}
} catch (error) {
console.log(`--- serverFetch error ---`);
console.error(error);
}
}
4 changes: 2 additions & 2 deletions public/js/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ import * as zyq from "./zyq.js";
})();

async function seizoenSelecteren(teamCode) {
const seizoenen = (await zyq.localFetch(`/${o_o_o.club}/seizoenen`)).map(function (seizoen) {
return [seizoen, zyq.seizoenVoluit(seizoen)];
const seizoenen = (await zyq.localFetch(`/${o_o_o.club}/seizoenen`)).map(function (club) {
return [club.seizoen, zyq.seizoenVoluit(club.seizoen)];
});
html.selectie(html.id("seizoenSelecteren"), o_o_o.seizoen, seizoenen, function (seizoen) {
html.zelfdePagina(`seizoen=${seizoen}&competitie=${db.INTERNE_COMPETITIE}&team=${db.INTERNE_COMPETITIE}`);
Expand Down

0 comments on commit 6eb2905

Please sign in to comment.