changes to user route, somewhat functional for now

This commit is contained in:
Mikayla Dobson
2022-11-19 12:14:37 -06:00
parent bf16841b95
commit 501f1dece8
3 changed files with 90 additions and 54 deletions

View File

@@ -24,23 +24,23 @@ export default class UserCtl {
}
}
// async getOne(id: string) {
// try {
// const user = await UserInstance.getOneByID(id);
// if (!user) throw createError(404, "User not found");
// return user;
// } catch (error) {
// throw new Error(error);
// }
// }
async getOne(id: string) {
try {
const user = await UserInstance.getOneByID(id);
if (!user) throw createError(404, "User not found");
return user;
} catch (error: any) {
throw new Error(error);
}
}
// async updateOne(id: string, data: IUser) {
// try {
// const result = await UserInstance.updateOneByID(id, data);
// if (!result) throw createError(400, "Bad request");
// return result;
// } catch (error) {
// throw new Error(error);
// }
// }
async updateOne(id: string, data: IUser) {
try {
const result = await UserInstance.updateOneByID(id, data);
if (!result) throw createError(400, "Bad request");
return result;
} catch (error: any) {
throw new Error(error);
}
}
}

View File

@@ -9,18 +9,6 @@ export class User {
try {
const statement = `SELECT * FROM recipin.appusers`;
const result = await pool.query(statement);
return result;
} catch (error: any) {
throw new Error(error);
}
}
async post(data: IUser) {
const { firstname, lastname, handle, email, password } = data;
try {
const statement = `INSERT INTO recipin.appusers (firstname, lastname, handle, email, password) VALUES ($1, $2, $3, $4, $5)`;
const params = [firstname, lastname, handle, email, password];
const result = await pool.query(statement, params);
if (result.rows.length) return result.rows;
return null;
} catch (error: any) {
@@ -28,28 +16,55 @@ export class User {
}
}
// async getOneByID(id: string): Promise<Array<IUser | null>> {
// try {
// const statement = `SELECT * FROM users WHERE id = $1;`;
// const values = [id];
// const result = await db.query(statement, values);
// if (result.rows.length) return result.rows[0];
// return [];
// } catch (error) {
// throw new Error(error);
// }
// }
async getOneByID(id: string) {
try {
const statement = `SELECT * FROM recipin.appusers WHERE id = $1`;
const values = [id];
const result = await pool.query(statement, values);
if (result.rows.length) return result.rows[0];
return null;
} catch (error: any) {
throw new Error(error);
}
}
// async updateOneByID(id: string, data: IUser): Promise<IUser | null> {
// try {
// // does this formatting work?
// const condition = pgp.as.format('WHERE id = $1 RETURNING *', id);
// const statement = pgp.helpers.update(data, null, 'users') + condition;
// const result = await db.query(statement);
// if (result.rows.length) return result.rows[0];
// return null;
// } catch (error) {
// throw new Error(error);
// }
// }
async updateOneByID(id: string, data: IUser) {
try {
const statement = `
UPDATE recipin.appusers
SET firstname = $1,
lastname = $2,
handle = $3,
email = $4,
password = $5,
active = $6
WHERE id = $7
RETURNING *;
`
const values = [
data.firstname, data.lastname, data.handle,
data.email, data.password, data.active, id
]
const result = await pool.query(statement, values);
if (result.rows.length) return result.rows[0];
return null;
} catch (error: any) {
throw new Error(error);
}
}
async post(data: IUser) {
const { firstname, lastname, handle, email, password, active } = data;
try {
const statement = `INSERT INTO recipin.appusers (firstname, lastname, handle, email, password, active) VALUES ($1, $2, $3, $4, $5, $6)`;
const params = [firstname, lastname, handle, email, password, active];
const result = await pool.query(statement, params);
if (result.rows.length) return result.rows;
return null;
} catch (error: any) {
throw new Error(error);
}
}
}

View File

@@ -8,10 +8,31 @@ export const userRoute = (app: Express) => {
// get all users
router.get('/', async (req, res) => {
const data = userCtl.getAll();
const data = await userCtl.getAll();
res.status(200).send(data);
})
router.get('/:id', async (req, res, next) => {
const { id } = req.params;
try {
const result = await userCtl.getOne(id);
res.status(200).send(result);
} catch(e) {
next(e);
}
})
router.put('/:id', async (req, res, next) => {
const { id } = req.params;
const data = req.body;
try {
const result = await userCtl.updateOne(id, data);
res.status(200).send(result);
} catch(e) {
next(e);
}
})
router.post('/', async (req, res) => {
const data = req.body;
const response = userCtl.post(data);