diff --git a/client/src/router/index.ts b/client/src/router/index.ts index 958689a..cc9ef6c 100644 --- a/client/src/router/index.ts +++ b/client/src/router/index.ts @@ -64,6 +64,14 @@ const router = createRouter({ // this generates a separate chunk (About.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () => import('../views/admin/Trainers.vue') + }, + { + path: '/user/profile', + name: 'UserProfile', + // route level code-splitting + // this generates a separate chunk (About.[hash].js) for this route + // which is lazy-loaded when the route is visited. + component: () => import('../views/user/UserProfile.vue') } ] }) diff --git a/client/src/views/user/UserProfile.vue b/client/src/views/user/UserProfile.vue new file mode 100644 index 0000000..84d1e19 --- /dev/null +++ b/client/src/views/user/UserProfile.vue @@ -0,0 +1,76 @@ + + + + + \ No newline at end of file diff --git a/endpoints.md b/endpoints.md index 3c881f4..1e7b0bb 100644 --- a/endpoints.md +++ b/endpoints.md @@ -27,7 +27,7 @@ | [x] | GET | /order/:id/newtimeslots | User can can get list of new timeslots | User | | [x] | POST | /order/:id/cancel | User can cancel order | User | | [x] | GET | /user | User can get profile information | User | -| [ ] | PUT | /user | User can change profile information | User | +| [x] | PUT | /user | User can change profile information | User | | [ ] | GET | /verify_email | Verify email | | | [ ] | POST | /reset_password | Request password reset | | | [ ] | POST | /new_password | Set new password | | diff --git a/server/src/routes/user.ts b/server/src/routes/user.ts index d4d5631..5628baa 100644 --- a/server/src/routes/user.ts +++ b/server/src/routes/user.ts @@ -4,6 +4,7 @@ import { client } from "../db"; import { DatabaseError } from "pg"; import { UserAuth } from "../middlewares/auth"; import { AuthedRequest } from "../interfaces/auth"; +import Joi from "joi"; const router: Router = express.Router(); @@ -41,4 +42,34 @@ router.get("/user", UserAuth, async (req: AuthedRequest, res: Response) => { } }) +const userProfileSchema = Joi.object({ + first_name: Joi.string().min(2).required(), + last_name: Joi.string().min(1).required() +}); + +router.put("/user", UserAuth, async (req: AuthedRequest, res: Response) => { + try { + const validation = userProfileSchema.validate(req.body, { abortEarly: false }); + if (validation.error !== undefined) { + return res.status(400).send(validation.error.details); + } + + await client.query(` + UPDATE users SET + first_name = $1, + last_name = $2 + WHERE id = $3 + `, [ + validation.value.first_name, + validation.value.last_name, + req.user?.userId + ]); + + return res.sendStatus(204); + } catch (error: DatabaseError | Error | any) { + console.error(error); + return res.sendStatus(500); + } +}) + export default router; \ No newline at end of file