wheelshare-old-backend/src/api.ts

190 lines
4.4 KiB
TypeScript

import { Router } from 'express';
import { createSessionFromCodeAndProvider } from './auth';
import {
getGroupByID,
getPoolByID,
getPoolsWithUser,
getUserByID,
groups,
users,
pools,
} from './data';
export const router = Router();
router.get('/user', (req, res) => {
if (typeof req.query.userID != 'string') {
return;
}
let userID = req.query.userID;
let user = getUserByID(userID);
if (user) {
res.json({ status: 'success', data: user });
} else {
res.json({ status: 'error', error: 'not_found' });
}
});
router.patch('/user', (req, res) => {
if (!(req.body.userID in users)) {
res.json({ status: 'error', error: 'user not found' });
} else {
let user = users[req.body.userID];
user.username = req.body.username;
user.first_name = req.body.first_name;
user.last_name = req.body.last_name;
res.json({ status: 'success' });
}
});
router.delete('/user', (req, res) => {
delete users[req.body.userID];
res.json({ status: 'success' });
});
router.get('/pool', (req, res) => {
if (typeof req.query.poolID != 'string') {
return;
}
let poolID = req.query.poolID;
let pool = getPoolByID(poolID);
if (pool) {
res.json({ status: 'success', data: pool });
} else {
res.json({ status: 'error', error: 'not_found' });
}
});
router.post('/pool', (req, res) => {
if (req.body.poolID in pools) {
res.json({ status: 'error', error: 'already_exists' });
} else {
pools[req.body.poolID] = {
id: req.body.poolID,
title: req.body.title,
description: req.body.description,
participant_ids: [],
driver_id: 'no driver',
create_time: 'create time',
update_time: 'update time',
comments: [],
group_id: 'no group',
status: 'pending',
capacity: 0,
direction: 'pickup',
author_id: 'no author',
type: 'request',
};
res.json({ status: 'success' });
}
});
router.patch('/pool', (req, res) => {
if (!(req.body.poolID in pools)) {
res.json({ status: 'error', error: 'pool not found' });
} else {
let pool = pools[req.body.poolID];
pool.title = req.body.title;
pool.description = req.body.description;
pool.driver_id = req.body.driver_id;
pool.update_time = req.body.update_time;
pool.status = req.body.status;
pool.capacity = req.body.capacity;
pool.direction = req.body.direction;
res.json({ status: 'success' });
}
});
router.delete('/pool', (req, res) => {
delete pools[req.body.poolID];
res.json({ status: 'success' });
});
router.get('/group', (req, res) => {
if (typeof req.query.groupID != 'string') {
return;
}
let groupID = req.query.groupID;
let group = getGroupByID(groupID);
if (group) {
res.json({ status: 'success', data: group });
} else {
res.json({ status: 'error', error: 'not_found' });
}
});
router.post('/group', (req, res) => {
if (req.body.groupID in groups) {
res.json({ status: 'error', error: 'already_exists' });
} else {
groups[req.body.groupID] = {
id: req.body.groupID,
member_ids: [],
};
res.json({ status: 'success' });
}
});
router.patch('/group', (req, res) => {
if (!(req.body.groupID in groups)) {
res.json({ status: 'error', error: 'group not found' });
} else {
let group = users[req.body.groupID];
res.json({ status: 'success' });
}
});
router.delete('/group', (req, res) => {
delete groups[req.body.groupID];
res.json({ status: 'success' });
});
router.get('/my_pools', (req, res) => {
if (typeof req.query.userID != 'string') {
return;
}
let userID = req.query.userID;
let groupsWithUser = getPoolsWithUser(userID);
if (groupsWithUser) {
res.json({ status: 'success', data: groupsWithUser });
} else {
res.json({ status: 'error', error: 'not_found' });
}
});
router.post('/my_pools', (req, res) => {
if (req.body.groupID in groups) {
res.json({ status: 'error', error: 'already_exists' });
} else {
groups[req.body.groupID] = {
id: req.body.groupID,
member_ids: [],
};
res.json({ status: 'success' });
}
});
router.get('/create_session', (req, res) => {
res.send('hello');
});
router.post('/create_session', (req, res) => {
const { code, provider } = req.body;
console.log('Creating session: code =', code, 'provider =', provider);
createSessionFromCodeAndProvider(code, provider)
.then((token) => {
res.json({ status: 'success', session_token: token });
})
.catch(() => {
res.json({ status: 'error' });
});
});