backend returns done with boolean type instead of number

This commit is contained in:
2025-07-04 16:44:43 +02:00
parent 1c72ab279d
commit bd2a1ae60c

View File

@@ -8,6 +8,12 @@ const db = drizzle("file:local.db");
const app = express();
const userId = "Detlef";
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};
type TaskResponse = Prettify<Omit<typeof task.$inferSelect, 'done'> & { done: boolean }>
app.use(cors())
app.use(express.json());
@@ -15,13 +21,14 @@ app.get('/', (req, res) => {
res.send('Hello World');
});
app.get('/tasks', async(req, res) => {
const tasks = await db.select().from(task)
console.log(tasks)
res.send(tasks);
app.get('/tasks', async (req, res) => {
const tasks: typeof task.$inferSelect[] = await db.select().from(task)
res.send(tasks.map<TaskResponse>(task => {
return { ...task, done: task.done === 1 }
}));
});
app.get('/events', async(req, res) => {
app.get('/events', async (req, res) => {
res.send(await db.select().from(event))
});
@@ -29,21 +36,21 @@ app.get('/user/:id', (req, res) => {
const id = req.params['id'];
if (id == null) {
res.status(400).send({error: 'Needs an user id'});
res.status(400).send({ error: 'Needs an user id' });
return;
}
const user = {id: id, name: 'Cracker'} //TODO
const user = { id: id, name: 'Cracker' } //TODO
res.json(user);
});
app.get('/task/:id', async(req, res) => {
app.get('/task/:id', async (req, res) => {
const id = parseInt(req.params['id']);
if (id == null) {
res.status(400).send({error: 'Needs an id'});
res.status(400).send({ error: 'Needs an id' });
return;
}
@@ -58,15 +65,15 @@ app.get('/event/:id', (req, res) => {
const id = req.params['id'];
if (id == null) {
res.status(400).send({error: 'Needs an id'});
res.status(400).send({ error: 'Needs an id' });
return;
}
const event = {id: id, name: 'Pary'} //TODO
const event = { id: id, name: 'Pary' } //TODO
res.json(event);
});
app.post('/task', async(req, res) => {
app.post('/task', async (req, res) => {
const newTask = req.body
newTask.userid = userId
@@ -77,7 +84,7 @@ app.post('/task', async(req, res) => {
res.status(201).json(returnedTask);
});
app.post('/event', async(req, res) => {
app.post('/event', async (req, res) => {
const newEvent: typeof event.$inferInsert = req.body
newEvent.description = ""
@@ -110,14 +117,14 @@ app.put('/event', (req, res) => {
res.status(200).json(updatedEvent);
});
app.delete('/task/:id', async(req, res) => {
app.delete('/task/:id', async (req, res) => {
const id = parseInt(req.params['id']);
const success = await db.delete(task).where(eq(task.id, id))
res.send("Deleted");
});
app.delete('/event/:id', async(req, res) => {
app.delete('/event/:id', async (req, res) => {
const id = parseInt(req.params['id']);
const success = await db.delete(event).where(eq(event.id, id))