From 501f1dece82f21e81acc689e4de39f33048f5ff3 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson <93477693+innocuous-symmetry@users.noreply.github.com> Date: Sat, 19 Nov 2022 12:14:37 -0600 Subject: [PATCH] changes to user route, somewhat functional for now --- server/controllers/UserCtl.ts | 36 +++++++-------- server/models/user.ts | 85 ++++++++++++++++++++--------------- server/routes/users.ts | 23 +++++++++- 3 files changed, 90 insertions(+), 54 deletions(-) diff --git a/server/controllers/UserCtl.ts b/server/controllers/UserCtl.ts index bd63de0..4a39721 100644 --- a/server/controllers/UserCtl.ts +++ b/server/controllers/UserCtl.ts @@ -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); + } + } } \ No newline at end of file diff --git a/server/models/user.ts b/server/models/user.ts index 90bd09d..6f24c45 100644 --- a/server/models/user.ts +++ b/server/models/user.ts @@ -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> { - // 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 { - // 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); + } + } } \ No newline at end of file diff --git a/server/routes/users.ts b/server/routes/users.ts index 0255d66..89a5210 100644 --- a/server/routes/users.ts +++ b/server/routes/users.ts @@ -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);