Refactored rank change logic
This commit is contained in:
@@ -1,20 +1,15 @@
|
|||||||
const express = require('express');
|
const express = require('express');
|
||||||
const r = express.Router();
|
const r = express.Router();
|
||||||
const ur = express.Router();
|
const ur = express.Router();
|
||||||
|
const { getAllRanks, insertMemberRank } = require('../services/rankService')
|
||||||
const pool = require('../db');
|
const pool = require('../db');
|
||||||
|
|
||||||
//insert a new latest rank for a user
|
//insert a new latest rank for a user
|
||||||
ur.post('/', async (req, res) => {
|
ur.post('/', async (req, res) => {3
|
||||||
try {
|
try {
|
||||||
const change = req.body?.change;
|
const change = req.body?.change;
|
||||||
console.log(change);
|
console.log(change);
|
||||||
|
await insertMemberRank(change);
|
||||||
const sql = `INSERT INTO members_ranks (member_id, rank_id, event_date) VALUES (?, ?, ?);`;
|
|
||||||
|
|
||||||
const params = [change.member_id, change.rank_id, change.event_date]
|
|
||||||
|
|
||||||
await pool.query(sql, params);
|
|
||||||
|
|
||||||
res.sendStatus(201);
|
res.sendStatus(201);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -26,8 +21,9 @@ ur.post('/', async (req, res) => {
|
|||||||
//get all ranks
|
//get all ranks
|
||||||
r.get('/', async (req, res) => {
|
r.get('/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const result = await pool.query('SELECT id, name, short_name, sort_id FROM ranks;');
|
const ranks = await getAllRanks();
|
||||||
res.json(result);
|
console.log(ranks);
|
||||||
|
res.json(ranks);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
res.status(500).json({ error: 'Internal server error' });
|
res.status(500).json({ error: 'Internal server error' });
|
||||||
|
|||||||
23
api/services/rankService.js
Normal file
23
api/services/rankService.js
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
const pool = require('../db');
|
||||||
|
|
||||||
|
async function getAllRanks() {
|
||||||
|
const rows = await pool.query(
|
||||||
|
'SELECT id, name, short_name, sort_id FROM ranks;'
|
||||||
|
);
|
||||||
|
return rows;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function insertMemberRank(change) {
|
||||||
|
const sql = `
|
||||||
|
INSERT INTO members_ranks (member_id, rank_id, event_date)
|
||||||
|
VALUES (?, ?, ?);
|
||||||
|
`;
|
||||||
|
console.log(change)
|
||||||
|
const params = [change.member_id, change.rank_id, change.event_date];
|
||||||
|
await pool.query(sql, params);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getAllRanks,
|
||||||
|
insertMemberRank
|
||||||
|
};
|
||||||
@@ -20,13 +20,13 @@ export async function getRanks(): Promise<Rank[]> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Placeholder: submit a rank change
|
// Placeholder: submit a rank change
|
||||||
export async function submitRankChange(memberId: number, rankId: number, date: string): Promise<{ ok: boolean }> {
|
export async function submitRankChange(member_id: number, rank_id: number, date: string): Promise<{ ok: boolean }> {
|
||||||
const res = await fetch(`${addr}/rank`, {
|
const res = await fetch(`${addr}/memberRanks`, {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
},
|
},
|
||||||
body: JSON.stringify({ memberId, rankId, date }),
|
body: JSON.stringify({ change: { member_id, rank_id, date } }),
|
||||||
})
|
})
|
||||||
|
|
||||||
if (res.ok) {
|
if (res.ok) {
|
||||||
|
|||||||
Reference in New Issue
Block a user