feat: team indeling endpoint + proposed_teams field
This commit is contained in:
@@ -286,11 +286,11 @@ function updateMatch(id, match) {
|
||||
// Build SET dynamically to only update provided fields
|
||||
const fields = [];
|
||||
const values = [];
|
||||
for (const key of ['status', 'responses', 'proposed_at', 'date', 'start', 'end', 'location', 'day', 'score', 'booker_id', 'booker_name']) {
|
||||
for (const key of ['status', 'responses', 'proposed_at', 'date', 'start', 'end', 'location', 'day', 'score', 'booker_id', 'booker_name', 'proposed_teams']) {
|
||||
if (match[key] !== undefined) {
|
||||
fields.push(`${key}=?`);
|
||||
let val = merged[key];
|
||||
if (key === 'responses' || (key === 'score' && typeof val === 'object' && val !== null)) {
|
||||
if (key === 'responses' || key === 'score' || key === 'proposed_teams') {
|
||||
val = JSON.stringify(val);
|
||||
}
|
||||
values.push(key === 'booker_id' || key === 'booker_name' ? String(val) : val);
|
||||
@@ -446,6 +446,11 @@ function rowToMatch(r) {
|
||||
try { score = JSON.parse(r.score); } catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
let proposed_teams = null;
|
||||
if (r.proposed_teams) {
|
||||
try { proposed_teams = JSON.parse(r.proposed_teams); } catch (e) { /* ignore */ }
|
||||
}
|
||||
|
||||
return {
|
||||
id: r.id,
|
||||
status: r.status,
|
||||
@@ -457,6 +462,7 @@ function rowToMatch(r) {
|
||||
location: r.location,
|
||||
day: r.day,
|
||||
score: score,
|
||||
proposed_teams: proposed_teams,
|
||||
booker_id: r.booker_id || null,
|
||||
booker_name: r.booker_name || null,
|
||||
players: players
|
||||
|
||||
@@ -363,6 +363,29 @@ app.delete("/api/matches/:id", (req, res) => {
|
||||
res.json({ ok: true });
|
||||
});
|
||||
|
||||
// ─── TEAM INDELING (voorgesteld) ───
|
||||
app.post("/api/matches/:id/teams", (req, res) => {
|
||||
let m = db.getMatch(String(req.params.id));
|
||||
if (!m) return res.status(404).json({ error: "match not found" });
|
||||
const { team1, team2 } = req.body;
|
||||
if (!Array.isArray(team1) || !Array.isArray(team2))
|
||||
return res.status(400).json({ error: "team1 and team2 must be arrays" });
|
||||
const allIds = [...team1, ...team2];
|
||||
if (allIds.length !== 4)
|
||||
return res.status(400).json({ error: "expected 4 players total" });
|
||||
db.updateMatch(String(req.params.id), { proposed_teams: { team1, team2 } });
|
||||
m = db.getMatch(String(req.params.id));
|
||||
const playerIds = (m.players || []).map(p => p.id);
|
||||
res.json({ ...m, players: playerIds, players_array: m.players });
|
||||
});
|
||||
|
||||
app.get("/api/matches/:id/teams", (req, res) => {
|
||||
const m = db.getMatch(String(req.params.id));
|
||||
if (!m) return res.status(404).json({ error: "match not found" });
|
||||
const teams = m.proposed_teams || null;
|
||||
res.json({ teams, match: { id: m.id, date: m.date, start: m.start, status: m.status } });
|
||||
});
|
||||
|
||||
// ─── SCORES ───
|
||||
app.post("/api/matches/:id/score", (req, res) => {
|
||||
let m = db.getMatch(String(req.params.id));
|
||||
|
||||
Reference in New Issue
Block a user