144 lines
5.1 KiB
JavaScript
144 lines
5.1 KiB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
};
|
|
import { NestFactory } from '@nestjs/core';
|
|
import { Module, Controller, Get, Post, Body, Param, ParseIntPipe, NotFoundException, BadRequestException } from '@nestjs/common';
|
|
import { PrismaService } from './prisma.service.js';
|
|
import 'dotenv/config';
|
|
import * as bcrypt from 'bcrypt';
|
|
let AppController = class AppController {
|
|
getHello() {
|
|
return 'Travel Planning API is running!';
|
|
}
|
|
};
|
|
__decorate([
|
|
Get(),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", String)
|
|
], AppController.prototype, "getHello", null);
|
|
AppController = __decorate([
|
|
Controller()
|
|
], AppController);
|
|
let AuthController = class AuthController {
|
|
constructor(prisma) {
|
|
this.prisma = prisma;
|
|
}
|
|
async getStatus() {
|
|
const userCount = await this.prisma.user.count();
|
|
return { isInitialSetup: userCount === 0 };
|
|
}
|
|
async signup(body) {
|
|
const { email, password, name } = body;
|
|
const existingUser = await this.prisma.user.findUnique({ where: { email } });
|
|
if (existingUser)
|
|
throw new BadRequestException('Email đã được sử dụng');
|
|
const userCount = await this.prisma.user.count();
|
|
const shouldBeAdmin = userCount === 0;
|
|
const passwordHash = await bcrypt.hash(password, 10);
|
|
return this.prisma.user.create({
|
|
data: { email, passwordHash, name, isAdmin: shouldBeAdmin },
|
|
select: { id: true, email: true, name: true, isAdmin: true }
|
|
});
|
|
}
|
|
};
|
|
__decorate([
|
|
Get('status'),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", Promise)
|
|
], AuthController.prototype, "getStatus", null);
|
|
__decorate([
|
|
Post('signup'),
|
|
__param(0, Body()),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Object]),
|
|
__metadata("design:returntype", Promise)
|
|
], AuthController.prototype, "signup", null);
|
|
AuthController = __decorate([
|
|
Controller('v1/auth'),
|
|
__metadata("design:paramtypes", [PrismaService])
|
|
], AuthController);
|
|
let TourController = class TourController {
|
|
constructor(prisma) {
|
|
this.prisma = prisma;
|
|
}
|
|
async getPublicTours() {
|
|
return this.prisma.tour.findMany({
|
|
take: 20,
|
|
include: {
|
|
photos: { take: 1 },
|
|
legs: {
|
|
take: 1,
|
|
include: {
|
|
places: { take: 1 }
|
|
}
|
|
}
|
|
}
|
|
});
|
|
}
|
|
async getTourDetails(id) {
|
|
const tour = await this.prisma.tour.findUnique({
|
|
where: { id },
|
|
include: {
|
|
members: true,
|
|
photos: true,
|
|
legs: {
|
|
orderBy: { sequenceNumber: 'asc' },
|
|
include: {
|
|
places: { orderBy: { sequenceInLeg: 'asc' } },
|
|
expenses: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
if (!tour)
|
|
throw new NotFoundException(`Không tìm thấy Tour với ID ${id}`);
|
|
return tour;
|
|
}
|
|
};
|
|
__decorate([
|
|
Get('explore'),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", []),
|
|
__metadata("design:returntype", Promise)
|
|
], TourController.prototype, "getPublicTours", null);
|
|
__decorate([
|
|
Get(':id'),
|
|
__param(0, Param('id', ParseIntPipe)),
|
|
__metadata("design:type", Function),
|
|
__metadata("design:paramtypes", [Number]),
|
|
__metadata("design:returntype", Promise)
|
|
], TourController.prototype, "getTourDetails", null);
|
|
TourController = __decorate([
|
|
Controller('v1/tours'),
|
|
__metadata("design:paramtypes", [PrismaService])
|
|
], TourController);
|
|
let AppModule = class AppModule {
|
|
};
|
|
AppModule = __decorate([
|
|
Module({
|
|
controllers: [AppController, AuthController, TourController],
|
|
providers: [PrismaService],
|
|
exports: [PrismaService]
|
|
})
|
|
], AppModule);
|
|
async function bootstrap() {
|
|
const app = await NestFactory.create(AppModule);
|
|
app.enableCors();
|
|
app.setGlobalPrefix('api');
|
|
const port = process.env.PORT || 3001;
|
|
await app.listen(port);
|
|
console.log(`🚀 Server is running on: http://localhost:${port}`);
|
|
}
|
|
bootstrap();
|
|
//# sourceMappingURL=main.js.map
|