-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Finishes #187354252-implementing-profile-page-settings
- Loading branch information
1 parent
88bc83d
commit f771345
Showing
5 changed files
with
179 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// controllers/userController.ts | ||
import { Request, Response } from 'express'; | ||
import User from '../database/models/user'; | ||
|
||
const getAllUserProfiles = async (req: Request, res: Response): Promise<void> => { | ||
try { | ||
// Find all user profiles | ||
const users = await User.findAll(); | ||
|
||
// Return all user profiles | ||
res.status(200).json(users); | ||
} catch (error) { | ||
// Handle errors | ||
console.error('Error fetching user profiles:', error); | ||
res.status(500).send('Internal Server Error'); | ||
} | ||
}; | ||
|
||
const getUserProfile = async (req: Request, res: Response): Promise<void> => { | ||
const userId: string = req.params.userId; | ||
|
||
try { | ||
// Find the user by ID | ||
const user = await User.findByPk(userId); | ||
if (!user) { | ||
res.status(404).json({ message: 'User not found' }); | ||
return; | ||
} | ||
|
||
// Return user profile | ||
res.status(200).json({ | ||
id: user.id, | ||
firstName: user.firstName, | ||
lastName: user.lastName, | ||
email: user.email, | ||
}); | ||
} catch (error) { | ||
// Handle errors | ||
console.error('Error fetching user profile:', error); | ||
res.status(500).send('Internal Server Error'); | ||
} | ||
}; | ||
|
||
const updateUserProfile = async (req: Request, res: Response): Promise<void> => { | ||
const userId: string = req.params.userId; | ||
const newData = req.body; | ||
|
||
try { | ||
// Find the user by ID | ||
const user = await User.findByPk(userId); | ||
if (!user) { | ||
res.status(404).json({ message: 'User not found' }); | ||
return; | ||
} | ||
|
||
// Update user profile data | ||
await user.update(newData); | ||
|
||
res.status(200).json({ message: 'User profile updated successfully' }); | ||
} catch (error) { | ||
// Handle errors | ||
console.error('Error updating user profile:', error); | ||
res.status(500).send('Internal Server Error'); | ||
} | ||
}; | ||
|
||
export { getUserProfile, updateUserProfile, getAllUserProfiles }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
Profiles: | ||
name: User Profiles API | ||
description: API for managing user profiles | ||
|
||
paths: | ||
/api/profiles/users: | ||
get: | ||
summary: Get all user profiles | ||
description: Retrieve a list of all user profiles | ||
tags: | ||
- Profiles | ||
responses: | ||
'200': | ||
description: A list of user profiles | ||
'500': | ||
description: Internal server error | ||
|
||
/api/profiles/users/{userId}: | ||
get: | ||
summary: Get a user profile by ID | ||
description: Retrieve a user profile by its ID | ||
tags: | ||
- Profiles | ||
parameters: | ||
- in: path | ||
name: userId | ||
required: true | ||
description: ID of the user profile to retrieve | ||
schema: | ||
type: string | ||
responses: | ||
'200': | ||
description: The user profile | ||
'404': | ||
description: User not found | ||
'500': | ||
description: Internal server error | ||
|
||
put: | ||
summary: Update a user profile | ||
description: Update an existing user profile | ||
tags: | ||
- Profiles | ||
parameters: | ||
- in: path | ||
name: userId | ||
required: true | ||
description: ID of the user profile to update | ||
schema: | ||
type: string | ||
- in: body | ||
name: body | ||
required: true | ||
description: New data for the user profile | ||
schema: | ||
type: object | ||
properties: | ||
firstName: | ||
type: string | ||
lastName: | ||
type: string | ||
email: | ||
type: string | ||
responses: | ||
'200': | ||
description: User profile updated successfully | ||
'404': | ||
description: User not found | ||
'500': | ||
description: Internal server error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// routes/userRoutes.ts | ||
import express from 'express'; | ||
import { getAllUserProfiles, getUserProfile, updateUserProfile } from '../controllers/profileController'; | ||
|
||
const router = express.Router(); | ||
router.get('/users/', getAllUserProfiles); | ||
// User profile retrieval endpoint | ||
router.get('/users/:userId/', getUserProfile); | ||
|
||
// User profile update endpoint | ||
router.put('/users/:userId/', updateUserProfile); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters