Skip to content

Commit 94119ce

Browse files
committed
feat: re-enable user page and add route for getting user data by username
1 parent f4cadb0 commit 94119ce

File tree

5 files changed

+53
-40
lines changed

5 files changed

+53
-40
lines changed

server/src/user/user.controller.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
import { Body, Controller, Get, Inject, Patch, Query } from '@nestjs/common';
1+
import {
2+
Body,
3+
Controller,
4+
Get,
5+
Inject,
6+
Param,
7+
Patch,
8+
Query,
9+
} from '@nestjs/common';
210
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
311
import { PageQueryDTO } from '@shared/validation/common/dto/PageQuery.dto';
412
import { GetUser } from '@shared/validation/user/dto/GetUser.dto';
@@ -19,8 +27,13 @@ export class UserController {
1927
private readonly userService: UserService,
2028
) {}
2129

30+
@Get('/:username')
31+
async getUser(@Param('username') username: string) {
32+
return await this.userService.getUserByEmailOrId({ username: username });
33+
}
34+
2235
@Get('by-query')
23-
async getUser(@Query() query: GetUser) {
36+
async getUserByQuery(@Query() query: GetUser) {
2437
return await this.userService.getUserByEmailOrId(query);
2538
}
2639

server/src/user/user.service.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { CreateUser } from '@shared/validation/user/dto/CreateUser.dto';
66
import { GetUser } from '@shared/validation/user/dto/GetUser.dto';
77
import { UpdateUsernameDto } from '@shared/validation/user/dto/UpdateUsername.dto';
88
import { UpdateUserProfileDto } from '@shared/validation/user/dto/UpdateUserProfile.dto';
9+
import { UserViewDto } from '@shared/validation/user/dto/UserView.dto';
910
import { validate } from 'class-validator';
1011
import { Model } from 'mongoose';
1112

@@ -39,6 +40,12 @@ export class UserService {
3940
return user;
4041
}
4142

43+
public async findByUsername(username: string): Promise<UserDocument | null> {
44+
const user = await this.userModel.findOne({ username }).exec();
45+
46+
return user;
47+
}
48+
4249
public async getUserPaginated(query: PageQueryDTO) {
4350
const { page = 1, limit = 10, sort = 'createdAt', order = 'asc' } = query;
4451

@@ -134,24 +141,24 @@ export class UserService {
134141

135142
public async getUserByEmailOrId(query: GetUser) {
136143
const { email, id, username } = query;
144+
let user;
137145

138146
if (email) {
139-
return await this.findByEmail(email);
147+
user = await this.findByEmail(email);
140148
}
141149

142150
if (id) {
143-
return await this.findByID(id);
151+
user = await this.findByID(id);
144152
}
145153

146154
if (username) {
147-
throw new HttpException(
148-
'Username is not supported yet',
149-
HttpStatus.BAD_REQUEST,
150-
);
155+
user = await this.findByUsername(username);
151156
}
152157

158+
if (user) return new UserViewDto(user);
159+
153160
throw new HttpException(
154-
'You must provide an email or an id',
161+
'You must provide an email, ID or username',
155162
HttpStatus.BAD_REQUEST,
156163
);
157164
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { ErrorBox } from '@web/src/modules/shared/components/client/ErrorBox';
2+
import UserProfile from '@web/src/modules/user/components/UserProfile';
3+
import { getUserProfileData } from '@web/src/modules/user/features/user.util';
4+
5+
const UserPage = async ({ params }: { params: { id: string } }) => {
6+
const { id } = params;
7+
8+
let userData;
9+
10+
try {
11+
userData = await getUserProfileData(id);
12+
} catch {
13+
userData = null;
14+
}
15+
16+
return !userData ? (
17+
<ErrorBox message='Failed to get user data' />
18+
) : (
19+
<UserProfile userData={userData} />
20+
);
21+
};
22+
23+
export default UserPage;

web/src/app/(content)/user/[id]/page_disable.tsx

Lines changed: 0 additions & 30 deletions
This file was deleted.

web/src/modules/user/features/user.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const getUserProfileData = async (
55
id: string,
66
): Promise<UserProfileData | never> => {
77
try {
8-
const res = await axiosInstance.get(`/user/?id=${id}`);
8+
const res = await axiosInstance.get(`/user/${id}`);
99
if (res.status === 200) return res.data as UserProfileData;
1010
else throw new Error('Failed to get user data');
1111
} catch {

0 commit comments

Comments
 (0)