Skip to content

Commit

Permalink
Merge pull request #53 from CS3219-AY2425S1/9-be-user-service-impleme…
Browse files Browse the repository at this point in the history
…nt-get-apis-for-fetching-user-data

Add GET API for Retrieving User Profile
  • Loading branch information
Daviancold authored Sep 22, 2024
2 parents f4b7855 + 1b32b23 commit c5e772c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
28 changes: 28 additions & 0 deletions backend/user-service/__tests__/routes/user.routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ describe('User Routes', () => {
})
})

describe('GET /users', () => {
it('should return 200 for successful get', async () => {
const user1 = await request(app).post('/users').send(CREATE_USER_DTO1)
const response = await request(app).get(`/users/${user1.body.id}`).send()
expect(response.status).toBe(200)
expect(response.body).toEqual(
expect.objectContaining({
id: expect.any(String),
username: 'test1',
email: 'test@gmail.com',
role: Role.ADMIN,
proficiency: Proficiency.INTERMEDIATE,
})
)
})
it('should return 404 for non-existent ids', async () => {
const testID = '000000000000000000000000' // MongoDB ID is 24 char
const response = await request(app).get(`/users/${testID}`).send()
expect(response.status).toBe(404)
expect(response.body).toMatchObject({})
})
it('should return 400 for non-existent/invalid ids', async () => {
const response = await request(app).get('/users/123').send()
expect(response.status).toBe(400)
expect(response.body).toContain('INVALID_ID')
})
})

describe('DELETE /users/:id', () => {
it('should return 200 for successful deletion', async () => {
const user1 = await request(app).post('/users').send(CREATE_USER_DTO1)
Expand Down
16 changes: 16 additions & 0 deletions backend/user-service/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createUser,
deleteUser,
findOneUserById,
findOneUserByEmail,
findOneUserByUsername,
updateUser,
Expand Down Expand Up @@ -62,6 +63,21 @@ export async function handleUpdateProfile(request: TypedRequest<UserProfileDto>,
response.status(200).json(user).send()
}

export async function handleGetCurrentProfile(
request: TypedRequest<UserProfileDto>,
response: Response
): Promise<void> {
const id = request.params.id

const user = await findOneUserById(id)
if (!user) {
response.status(404).send()
} else {
const dto = UserDto.fromModel(user)
response.status(200).json(dto).send()
}
}

export async function handleDeleteUser(request: TypedRequest<void>, response: Response): Promise<void> {
const id = request.params.id
await deleteUser(id)
Expand Down
8 changes: 7 additions & 1 deletion backend/user-service/src/routes/user.routes.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { handleCreateUser, handleDeleteUser, handleUpdateProfile } from '../controllers/user.controller'
import {
handleCreateUser,
handleGetCurrentProfile,
handleDeleteUser,
handleUpdateProfile,
} from '../controllers/user.controller'

import { Router } from 'express'

const router = Router()

router.post('/', handleCreateUser)
router.put('/:id', handleUpdateProfile)
router.get('/:id', handleGetCurrentProfile)
router.delete('/:id', handleDeleteUser)

export default router

0 comments on commit c5e772c

Please sign in to comment.