feat: add database and orm

This commit is contained in:
2026-10-21 10:14:50 +03:00
parent d8fb486709
commit ef2bc6f8b6
30 changed files with 668 additions and 354 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
import { User } from '../../common/store/domain';
import { User } from '../../common/domain';
export interface UserResponse {
id: string;
+3 -3
View File
@@ -8,7 +8,7 @@ import {
} from '@nestjs/common';
import { AuthGuard } from '../common/auth/auth.guard';
import { CurrentUser } from '../common/auth/current-user.decorator';
import { User } from '../common/store/domain';
import { User } from '../common/domain';
import { CreateUserDto } from './dto/create-user.dto';
import { toUserResponse, UserResponse } from './dto/user.response';
import { UsersService } from './users.service';
@@ -19,8 +19,8 @@ export class UsersController {
@Post()
@HttpCode(201)
create(@Body() dto: CreateUserDto): UserResponse {
const user = this.users.register(dto.username, dto.display_name);
async create(@Body() dto: CreateUserDto): Promise<UserResponse> {
const user = await this.users.register(dto.username, dto.display_name);
return toUserResponse(user);
}
+33 -8
View File
@@ -1,17 +1,42 @@
import { Injectable } from '@nestjs/common';
import { ApiError } from '../common/filters/api-error';
import { User } from '../common/store/domain';
import { StoreService } from '../common/store/store.service';
import { User } from '../common/domain';
import { PrismaService, isUniqueViolation } from '../db/prisma.service';
@Injectable()
export class UsersService {
constructor(private readonly store: StoreService) {}
constructor(private readonly prisma: PrismaService) {}
register(username: string, displayName: string): User {
const user = this.store.createUser(username, displayName.trim());
if (!user) {
throw new ApiError('conflict', 'username is already taken');
async register(username: string, displayName: string): Promise<User> {
try {
const row = await this.prisma.user.create({
data: { username, displayName: displayName.trim() },
});
return toUser(row);
} catch (error) {
if (isUniqueViolation(error)) {
throw new ApiError('conflict', 'username is already taken');
}
throw error;
}
return user;
}
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(),
};
}