update eslint and refactor backend
This commit is contained in:
51
src/index.ts
51
src/index.ts
@@ -2,31 +2,66 @@ import { serve } from 'bun';
|
||||
import index from './index.html';
|
||||
import DB from './server/db';
|
||||
|
||||
const db = new DB();
|
||||
|
||||
const server = serve({
|
||||
routes: {
|
||||
// Serve index.html for all unmatched routes.
|
||||
'/*': index,
|
||||
|
||||
'/api/group/:id': async req => {
|
||||
const id = req.params.id;
|
||||
const group = await new DB().getGroup(id);
|
||||
console.log('Fetching group with ID:', id, 'Result:', group);
|
||||
if (!group) return new Response(JSON.stringify({ error: 'Not found' }), { status: 404 });
|
||||
'/api/user/:mail': async req => {
|
||||
const mail = req.params.mail;
|
||||
console.log('Received request for user:', mail);
|
||||
const user = await db.getUserByMail(mail);
|
||||
console.log('Fetching user with mail:', mail, 'Result:', user);
|
||||
if (!user) return new Response(JSON.stringify({ error: 'User not found' }), { status: 404 });
|
||||
return new Response(JSON.stringify(user), { headers: { 'Content-Type': 'application/json' } });
|
||||
},
|
||||
|
||||
'/api/user': {
|
||||
async POST(req) {
|
||||
try {
|
||||
const { mail } = await req.json() as { mail: string };
|
||||
console.log('Received request to create user with mail:', mail);
|
||||
const user = await db.createUser(mail);
|
||||
return new Response(JSON.stringify(user), { headers: { 'Content-Type': 'application/json' } });
|
||||
} catch (err) {
|
||||
console.error('Error creating user:', err);
|
||||
return new Response(JSON.stringify({ error: 'Failed to create user' }), { status: 500 });
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
'/api/group/:code': async req => {
|
||||
const code = req.params.code;
|
||||
console.log('Received request for group:', code);
|
||||
const group = await db.getGroupByCode(code);
|
||||
console.log('Fetching group with ID:', code, 'Result:', group);
|
||||
if (!group) return new Response(JSON.stringify({ error: 'Group not found' }), { status: 404 });
|
||||
return new Response(JSON.stringify(group), { headers: { 'Content-Type': 'application/json' } });
|
||||
},
|
||||
|
||||
'/api/group': {
|
||||
async POST(req) {
|
||||
try {
|
||||
const group = (await req.formData() as FormData).get('code') as string;
|
||||
const response = await new DB().createGroup(group);
|
||||
const { name, mail } = await req.json() as { name: string; mail: string };
|
||||
console.log('Received request to create group with name:', name, 'and mail:', mail);
|
||||
const response = db.createGroup({ name, mail });
|
||||
return new Response(JSON.stringify(response), { headers: { 'Content-Type': 'application/json' } });
|
||||
} catch (err) {
|
||||
console.error('Error creating group:', err);
|
||||
return new Response(JSON.stringify({ error: 'Failed to create group' }), { status: 500 });
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
'/api/group_members/:groupId': async req => {
|
||||
const groupId = parseInt(req.params.groupId, 10);
|
||||
console.log('Received request for group members of group ID:', groupId);
|
||||
const members = await db.getGroupMembers(groupId);
|
||||
console.log('Fetching members for group ID:', groupId, 'Result:', members);
|
||||
return new Response(JSON.stringify(members), { headers: { 'Content-Type': 'application/json' } });
|
||||
},
|
||||
},
|
||||
|
||||
development: process.env.NODE_ENV !== 'production' && {
|
||||
|
||||
Reference in New Issue
Block a user