File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -91,6 +91,21 @@ export class UsersController {
9191 return this . userService . getMyStats ( req . user . id as string ) ;
9292 }
9393
94+ @Get ( 'by-username/:username' )
95+ async getPublicProfileByUsername (
96+ @Param ( 'username' ) username : string ,
97+ ) : Promise < {
98+ id : string ;
99+ username : string | null ;
100+ xp : number ;
101+ badgesCount : number ;
102+ coursesCompleted : number ;
103+ avatarUrl : string | null ;
104+ bio : string | null ;
105+ } > {
106+ return this . userService . findByUsername ( username ) ;
107+ }
108+
94109 @Get ( ':id/public' )
95110 async getPublicProfile ( @Param ( 'id' ) id : string ) : Promise < {
96111 id : string ;
Original file line number Diff line number Diff line change 66 BadRequestException ,
77} from '@nestjs/common' ;
88import { InjectRepository } from '@nestjs/typeorm' ;
9- import { Repository , MoreThan } from 'typeorm' ;
9+ import { ILike , Repository , MoreThan } from 'typeorm' ;
1010import * as crypto from 'crypto' ;
1111import * as bcrypt from 'bcrypt' ;
1212import { RegisterDto } from 'src/auth/dto/register.dto' ;
@@ -302,4 +302,36 @@ export class UserService {
302302 bio : user . bio ?? null ,
303303 } ;
304304 }
305+
306+ async findByUsername ( username : string ) : Promise < {
307+ id : string ;
308+ username : string | null ;
309+ xp : number ;
310+ badgesCount : number ;
311+ coursesCompleted : number ;
312+ avatarUrl : string | null ;
313+ bio : string | null ;
314+ } > {
315+ const user = await this . userRepository . findOne ( {
316+ where : { username : ILike ( username ) } ,
317+ } ) ;
318+
319+ if ( ! user ) {
320+ throw new NotFoundException ( 'User not found' ) ;
321+ }
322+
323+ const badgesCount = await this . userBadgeRepository . count ( {
324+ where : { userId : user . id } ,
325+ } ) ;
326+
327+ return {
328+ id : user . id ,
329+ username : user . username ?? user . name ?? null ,
330+ xp : this . resolveXp ( user ) ,
331+ badgesCount,
332+ coursesCompleted : user . coursesCompleted ?? 0 ,
333+ avatarUrl : user . avatarUrl ?? null ,
334+ bio : user . bio ?? null ,
335+ } ;
336+ }
305337}
You can’t perform that action at this time.
0 commit comments