Sửa đổi dữ liệu của hệ thống theo ARCHITECTURE.md
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import 'reflect-metadata';
|
||||
import { NestFactory } from '@nestjs/core';
|
||||
import { Module, Controller, Get, Post, Body, Param, Patch, Delete, ParseIntPipe, NotFoundException, BadRequestException, UnauthorizedException, UseGuards } from '@nestjs/common';
|
||||
import { Module, Controller, Get, Post, Body, Param, Patch, Delete, ParseUUIDPipe, NotFoundException, BadRequestException, UnauthorizedException, UseGuards } from '@nestjs/common';
|
||||
import { PrismaService } from './prisma.service.js';
|
||||
import 'dotenv/config';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
@@ -70,6 +71,7 @@ class AuthController {
|
||||
const passwordHash = await bcrypt.hash(password, 10);
|
||||
|
||||
return this.prisma.user.create({
|
||||
// Bỏ createdTours: {} vì đây là quan hệ 1-n, Prisma sẽ tự hiểu là mảng rỗng
|
||||
data: { email, passwordHash, name, isAdmin: shouldBeAdmin },
|
||||
select: { id: true, email: true, name: true, isAdmin: true }
|
||||
});
|
||||
@@ -90,7 +92,7 @@ class TourController {
|
||||
legs: {
|
||||
take: 1,
|
||||
include: {
|
||||
places: { take: 1 }
|
||||
locations: { take: 1 }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,16 +100,16 @@ class TourController {
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async getTourDetails(@Param('id', ParseIntPipe) id: number) {
|
||||
async getTourDetails(@Param('id', ParseUUIDPipe) id: string) {
|
||||
const tour = await this.prisma.tour.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
members: true,
|
||||
participants: true,
|
||||
photos: true,
|
||||
legs: {
|
||||
orderBy: { sequenceNumber: 'asc' },
|
||||
orderBy: { sequence: 'asc' },
|
||||
include: {
|
||||
places: { orderBy: { sequenceInLeg: 'asc' } },
|
||||
locations: { orderBy: { plannedStart: 'asc' } },
|
||||
expenses: true,
|
||||
},
|
||||
},
|
||||
@@ -132,7 +134,7 @@ class UserController {
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
async updateUser(@Param('id', ParseIntPipe) id: number, @Body() data: any) {
|
||||
async updateUser(@Param('id', ParseUUIDPipe) id: string, @Body() data: any) {
|
||||
// Nếu đổi mật khẩu thì cần hash
|
||||
if (data.password) {
|
||||
data.passwordHash = await bcrypt.hash(data.password, 10);
|
||||
@@ -146,7 +148,7 @@ class UserController {
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
async deleteUser(@Param('id', ParseIntPipe) id: number) {
|
||||
async deleteUser(@Param('id', ParseUUIDPipe) id: string) {
|
||||
// Không cho phép tự xóa chính mình hoặc xóa admin cuối cùng (logic đơn giản)
|
||||
const user = await this.prisma.user.findUnique({ where: { id } });
|
||||
if (user?.isAdmin) {
|
||||
@@ -158,7 +160,7 @@ class UserController {
|
||||
}
|
||||
|
||||
@Post('block/:id')
|
||||
async toggleBlock(@Param('id', ParseIntPipe) id: number) {
|
||||
async toggleBlock(@Param('id', ParseUUIDPipe) id: string) {
|
||||
const user = await this.prisma.user.findUnique({ where: { id } });
|
||||
if (!user) throw new NotFoundException('Người dùng không tồn tại');
|
||||
return this.prisma.user.update({
|
||||
|
||||
Reference in New Issue
Block a user