Files
Luncher/server/src/routes/storeRoutes.ts
T

80 lines
2.8 KiB
TypeScript

import express from "express";
import { getStores, addStore, updateStore, removeStore } from "../stores";
const router = express.Router();
router.get("/", async (_req, res, next) => {
try {
const stores = await getStores();
res.status(200).json(stores);
} catch (e: any) { next(e); }
});
router.post("/add", async (req, res, next) => {
const { name, heslo, urls } = req.body ?? {};
if (!name || typeof name !== 'string') {
return res.status(400).json({ error: 'Nebyl předán název obchodu' });
}
if (!heslo || typeof heslo !== 'string') {
return res.status(400).json({ error: 'Nebylo předáno heslo' });
}
if (urls != null && (!Array.isArray(urls) || urls.some((u: unknown) => typeof u !== 'string'))) {
return res.status(400).json({ error: 'Neplatný seznam URL obchodu' });
}
try {
const stores = await addStore(name, heslo, urls);
res.status(200).json(stores);
} catch (e: any) {
if (e.message === 'UNAUTHORIZED') {
return res.status(403).json({ error: 'Nesprávné heslo' });
}
next(e);
}
});
router.post("/update", async (req, res, next) => {
const { name, newName, heslo, urls } = req.body ?? {};
if (!name || typeof name !== 'string') {
return res.status(400).json({ error: 'Nebyl předán název obchodu' });
}
if (!heslo || typeof heslo !== 'string') {
return res.status(400).json({ error: 'Nebylo předáno heslo' });
}
if (newName != null && typeof newName !== 'string') {
return res.status(400).json({ error: 'Neplatný název obchodu' });
}
if (urls != null && (!Array.isArray(urls) || urls.some((u: unknown) => typeof u !== 'string'))) {
return res.status(400).json({ error: 'Neplatný seznam URL obchodu' });
}
try {
const stores = await updateStore(name, heslo, newName ?? undefined, urls ?? undefined);
res.status(200).json(stores);
} catch (e: any) {
if (e.message === 'UNAUTHORIZED') {
return res.status(403).json({ error: 'Nesprávné heslo' });
}
next(e);
}
});
router.post("/delete", async (req, res, next) => {
const { name, heslo } = req.body ?? {};
if (!name || typeof name !== 'string') {
return res.status(400).json({ error: 'Nebyl předán název obchodu' });
}
if (!heslo || typeof heslo !== 'string') {
return res.status(400).json({ error: 'Nebylo předáno heslo' });
}
try {
const stores = await removeStore(name, heslo);
res.status(200).json(stores);
} catch (e: any) {
if (e.message === 'UNAUTHORIZED') {
return res.status(403).json({ error: 'Nesprávné heslo' });
}
next(e);
}
});
export default router;