mirror of
https://github.com/myfatemi04/wheelshare-old-backend.git
synced 2025-04-09 14:40:17 -04:00
feat: fixed typings and get db methods
This commit is contained in:
parent
db8da4a55e
commit
9622ede6e0
151
src/api.ts
151
src/api.ts
|
@ -5,9 +5,6 @@ import {
|
|||
getPoolByID,
|
||||
getPoolsWithUser,
|
||||
getUserByID,
|
||||
groups,
|
||||
pools,
|
||||
users,
|
||||
} from './data';
|
||||
|
||||
export const router = Router();
|
||||
|
@ -33,21 +30,18 @@ router.get('/user', async (req, res) => {
|
|||
});
|
||||
|
||||
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' });
|
||||
}
|
||||
// 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.delete('/user', (req, res) => {});
|
||||
|
||||
router.get('/pool', (req, res) => {
|
||||
if (typeof req.query.poolID != 'string') {
|
||||
|
@ -65,49 +59,46 @@ router.get('/pool', (req, res) => {
|
|||
});
|
||||
|
||||
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' });
|
||||
}
|
||||
// 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: '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' });
|
||||
}
|
||||
// if (!(req.body.poolID in pools)) {
|
||||
// res.json({ status: 'error', error: '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.delete('/pool', (req, res) => {});
|
||||
|
||||
router.get('/group', (req, res) => {
|
||||
if (typeof req.query.groupID != 'string') {
|
||||
|
@ -125,39 +116,19 @@ router.get('/group', (req, res) => {
|
|||
});
|
||||
|
||||
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' });
|
||||
// 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('/my_pools', (req, res) => {
|
||||
if (typeof req.query.userID != 'string') {
|
||||
return;
|
||||
}
|
||||
|
||||
let userID = req.query.userID;
|
||||
let groupsWithUser = getPoolsWithUser(userID);
|
||||
|
||||
let groupsWithUser = getPoolsWithUser(req.session.accountID);
|
||||
if (groupsWithUser) {
|
||||
res.json({ status: 'success', data: groupsWithUser });
|
||||
} else {
|
||||
|
@ -165,18 +136,6 @@ router.get('/my_pools', (req, res) => {
|
|||
}
|
||||
});
|
||||
|
||||
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.post('/create_session', (req, res) => {
|
||||
const { code, provider } = req.body;
|
||||
console.log('Creating session: code =', code, 'provider =', provider);
|
||||
|
|
63
src/data.ts
63
src/data.ts
|
@ -1,28 +1,5 @@
|
|||
import { IonProfile } from './auth_ion';
|
||||
import { PoolModel, UserModel } from './models';
|
||||
|
||||
/**
|
||||
* Records users by id
|
||||
*/
|
||||
export const users: Record<string, Carpool.User> = {
|
||||
myfatemi04: {
|
||||
id: '3baeaed6-05cb-4c03-9b43-1d74beafdbb7',
|
||||
email: '2022mfatemi@tjhsst.edu',
|
||||
username: 'myfatemi04',
|
||||
first_name: 'Michael',
|
||||
last_name: 'Fatemi',
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Records groups by id
|
||||
*/
|
||||
export const groups: Record<string, Carpool.Group> = {};
|
||||
|
||||
/**
|
||||
* Records pools by id
|
||||
*/
|
||||
export const pools: Record<string, Carpool.Pool> = {};
|
||||
import { GroupModel, PoolModel, UserModel } from './models';
|
||||
|
||||
/**
|
||||
*
|
||||
|
@ -30,14 +7,7 @@ export const pools: Record<string, Carpool.Pool> = {};
|
|||
* @returns The group IDs with that userID as a member
|
||||
*/
|
||||
export function getGroupsWithUser(userID: string) {
|
||||
let groupIDs = [];
|
||||
for (let group of Object.values(groups)) {
|
||||
if (group.member_ids.includes(userID)) {
|
||||
groupIDs.push(group.id);
|
||||
}
|
||||
}
|
||||
|
||||
return groupIDs;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -46,34 +16,29 @@ export function getGroupsWithUser(userID: string) {
|
|||
* @returns The post IDs with that userID as the author
|
||||
*/
|
||||
export function getPoolsWithUser(userID: string) {
|
||||
let poolIDs: string[] = [];
|
||||
for (let post of Object.values(pools)) {
|
||||
if (post.author_id == userID) {
|
||||
poolIDs.push(post.id);
|
||||
}
|
||||
}
|
||||
|
||||
return poolIDs;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export async function getUserByID(userID: string) {
|
||||
return (await UserModel.findById(userID).exec()).toJSON() as Carpool.User;
|
||||
if (userID == null) {
|
||||
return undefined;
|
||||
}
|
||||
return (await UserModel.findById(userID).exec()).toJSON();
|
||||
}
|
||||
|
||||
export async function getPoolByID(poolID: string) {
|
||||
return (await PoolModel.findById(poolID).exec()).toJSON() as Carpool.Pool;
|
||||
let doc = await PoolModel.findById(poolID).exec();
|
||||
return doc?.toJSON();
|
||||
}
|
||||
|
||||
export function getGroupByID(groupID: string): Carpool.Group | undefined {
|
||||
return groups[groupID];
|
||||
export async function getGroupByID(groupID: string) {
|
||||
let doc = await GroupModel.findById(groupID).exec();
|
||||
return doc?.toJSON();
|
||||
}
|
||||
|
||||
export async function getUserByEmail(email: string) {
|
||||
return ((
|
||||
await UserModel.findOne({
|
||||
email,
|
||||
}).exec()
|
||||
).toJSON() as unknown) as Carpool.User;
|
||||
const user = await UserModel.findOne({ email }).exec();
|
||||
return user?.toJSON();
|
||||
}
|
||||
|
||||
export async function registerUserFromIonProfile(
|
||||
|
|
|
@ -59,7 +59,6 @@ export interface Pool extends Document {
|
|||
}
|
||||
|
||||
const PoolSchema: Schema = new Schema({
|
||||
id: { type: String, required: true },
|
||||
title: { type: String, required: true },
|
||||
description: { type: String, required: true },
|
||||
participant_ids: { type: [String], required: true },
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
"esModuleInterop": true,
|
||||
"moduleResolution": "Node"
|
||||
},
|
||||
"files": ["src/types.ts", "src/typings/session.d.ts"],
|
||||
"include": ["src/", "session.d.ts"],
|
||||
"exclude": ["node_modules/"]
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user