Files
Server/repositories/badgeRepository.js
T
azures04 b03f5c91e6 Add badges, checkins, and transport endpoints
Introduce gamification and check-in support plus expanded transport/admin APIs. Added DDL for checkins, badges and user_badges. New repositories: badgeRepository, userBadgeRepository, checkinRepository. New routes: admin badge management, admin transport (lines/platforms/stations), public transport endpoints, user checkins and stations endpoints, and user badges on /me. Services updated: adminService (badge CRUD & user assignment), userService (checkins, badge queries), authService (hasAdminRights). Minor package.json name update. These changes implement database, repo, service and route wiring for badges, user_badins and checkin features and additional transport admin routes.
2026-09-23 02:09:39 +02:00

63 lines
1.9 KiB
JavaScript

const { pool } = require("../modules/database")
const { DefaultError } = require("../errors/errors")
async function createBadge(name, description, icon, condition) {
try {
const sql = "INSERT INTO `badges` (name, description, icon, condition) VALUES (?, ?, ?, ?)"
const rows = await pool.query(sql, [name, description, icon, condition])
return rows.insertId
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function findAllBadges() {
try {
const sql = "SELECT * FROM `badges` ORDER BY name"
const rows = await pool.query(sql)
return rows
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function findBadgeById(badgeId) {
try {
const sql = "SELECT * FROM `badges` WHERE id = ?"
const rows = await pool.query(sql, [badgeId])
return rows[0] || null
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function updateBadge(badgeId, updateData) {
try {
const fields = Object.keys(updateData)
const values = Object.values(updateData)
const setClause = fields.map(f => `${f} = ?`).join(", ")
const sql = `UPDATE \`badges\` SET ${setClause} WHERE id = ?`
const rows = await pool.query(sql, [...values, badgeId])
return rows.affectedRows > 0
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
async function deleteBadge(badgeId) {
try {
const sql = "DELETE FROM `badges` WHERE id = ?"
const rows = await pool.query(sql, [badgeId])
return rows.affectedRows > 0
} catch (error) {
throw new DefaultError(500, "Internal Server Error", error)
}
}
module.exports = {
createBadge,
findAllBadges,
findBadgeById,
updateBadge,
deleteBadge
}