forked from mtusi2026/mtusi2026lab5
chore: port lab4 solution as lab5 base
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import { Matches } from 'class-validator';
|
||||
import { CodePointLength } from '../../common/dto/code-point-length.validator';
|
||||
import { NotBlank } from '../../common/dto/not-blank.validator';
|
||||
|
||||
export class CreateUserDto {
|
||||
@Matches(/^[a-z0-9_]{3,32}$/, {
|
||||
message: 'username must match [a-z0-9_]{3,32}',
|
||||
})
|
||||
username!: string;
|
||||
|
||||
@NotBlank()
|
||||
@CodePointLength(1, 100)
|
||||
display_name!: string;
|
||||
|
||||
@NotBlank()
|
||||
@CodePointLength(12, 128)
|
||||
password!: string;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { User } from '../../common/domain';
|
||||
|
||||
export interface UserResponse {
|
||||
id: string;
|
||||
username: string;
|
||||
display_name: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export function toUserResponse(user: User): UserResponse {
|
||||
return {
|
||||
id: user.id,
|
||||
username: user.username,
|
||||
display_name: user.displayName,
|
||||
created_at: user.createdAt,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
Post,
|
||||
UseGuards,
|
||||
} from '@nestjs/common';
|
||||
import { AuthGuard } from '../auth/auth.guard';
|
||||
import { CurrentUser } from '../auth/current-user.decorator';
|
||||
import { User } from '../common/domain';
|
||||
import { CreateUserDto } from './dto/create-user.dto';
|
||||
import { toUserResponse, UserResponse } from './dto/user.response';
|
||||
import { UsersService } from './users.service';
|
||||
|
||||
@Controller('api/v1/users')
|
||||
export class UsersController {
|
||||
constructor(private readonly users: UsersService) {}
|
||||
|
||||
@Post()
|
||||
@HttpCode(201)
|
||||
async create(@Body() dto: CreateUserDto): Promise<UserResponse> {
|
||||
const user = await this.users.register(
|
||||
dto.username,
|
||||
dto.display_name,
|
||||
dto.password,
|
||||
);
|
||||
return toUserResponse(user);
|
||||
}
|
||||
|
||||
@Get('me')
|
||||
@UseGuards(AuthGuard)
|
||||
me(@CurrentUser() user: User): UserResponse {
|
||||
return toUserResponse(user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { UsersController } from './users.controller';
|
||||
import { UsersService } from './users.service';
|
||||
|
||||
@Module({
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService],
|
||||
exports: [UsersService],
|
||||
})
|
||||
export class UsersModule {}
|
||||
@@ -0,0 +1,48 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { hashPassword } from '../auth/password';
|
||||
import { ApiError } from '../common/filters/api-error';
|
||||
import { User } from '../common/domain';
|
||||
import { PrismaService, isUniqueViolation } from '../db/prisma.service';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async register(
|
||||
username: string,
|
||||
displayName: string,
|
||||
password: string,
|
||||
): Promise<User> {
|
||||
const passwordHash = await hashPassword(password);
|
||||
try {
|
||||
const row = await this.prisma.user.create({
|
||||
data: { username, displayName: displayName.trim(), passwordHash },
|
||||
});
|
||||
return toUser(row);
|
||||
} catch (error) {
|
||||
if (isUniqueViolation(error)) {
|
||||
throw new ApiError('conflict', 'username is already taken');
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<User | null> {
|
||||
const row = await this.prisma.user.findUnique({ where: { id } });
|
||||
return row ? toUser(row) : null;
|
||||
}
|
||||
}
|
||||
|
||||
export function toUser(row: {
|
||||
id: string;
|
||||
username: string;
|
||||
displayName: string;
|
||||
createdAt: Date;
|
||||
}): User {
|
||||
return {
|
||||
id: row.id,
|
||||
username: row.username,
|
||||
displayName: row.displayName,
|
||||
createdAt: row.createdAt.toISOString(),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user