Thêm tính năng bình luận ở mỗi điểm của chặng
This commit is contained in:
Vendored
+8
@@ -1 +1,9 @@
|
||||
import 'reflect-metadata';
|
||||
import { OnGatewayConnection } from '@nestjs/websockets';
|
||||
import { Server, Socket } from 'socket.io';
|
||||
export declare class CommentGateway implements OnGatewayConnection {
|
||||
server: Server;
|
||||
handleConnection(client: Socket): void;
|
||||
handleJoinTour(client: Socket, tourId: string): void;
|
||||
notifyNewComment(tourId: string, data: any): void;
|
||||
}
|
||||
|
||||
Vendored
+97
-4
@@ -45,6 +45,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
return function (target, key) { decorator(target, key, paramIndex); }
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.CommentGateway = void 0;
|
||||
const dotenv = __importStar(require("dotenv"));
|
||||
const path = __importStar(require("path"));
|
||||
const envPath = path.resolve(process.cwd(), '..', '.env');
|
||||
@@ -52,6 +53,8 @@ dotenv.config({ path: envPath });
|
||||
require("reflect-metadata");
|
||||
const core_1 = require("@nestjs/core");
|
||||
const common_1 = require("@nestjs/common");
|
||||
const websockets_1 = require("@nestjs/websockets");
|
||||
const socket_io_1 = require("socket.io");
|
||||
const prisma_service_1 = require("../prisma/prisma.service");
|
||||
const bcrypt = __importStar(require("bcrypt"));
|
||||
const admin_guard_1 = require("./auth/admin.guard");
|
||||
@@ -358,7 +361,10 @@ let TourController = class TourController {
|
||||
legs: {
|
||||
orderBy: { sequence: 'asc' },
|
||||
include: {
|
||||
locations: { orderBy: { plannedStart: 'asc' } }
|
||||
locations: {
|
||||
orderBy: { plannedStart: 'asc' },
|
||||
include: { _count: { select: { comments: true } } }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -379,7 +385,10 @@ let TourController = class TourController {
|
||||
legs: {
|
||||
orderBy: { sequence: 'asc' },
|
||||
include: {
|
||||
locations: { orderBy: { plannedStart: 'asc' } },
|
||||
locations: {
|
||||
orderBy: { plannedStart: 'asc' },
|
||||
include: { _count: { select: { comments: true } } }
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
@@ -1014,6 +1023,90 @@ UserController = __decorate([
|
||||
(0, common_1.Controller)('users'),
|
||||
__metadata("design:paramtypes", [prisma_service_1.PrismaService])
|
||||
], UserController);
|
||||
let CommentGateway = class CommentGateway {
|
||||
handleConnection(client) {
|
||||
console.log(`[WS] Client connected: ${client.id}`);
|
||||
}
|
||||
handleJoinTour(client, tourId) {
|
||||
client.join(`tour_${tourId}`);
|
||||
console.log(`[WS] Client ${client.id} joined room: tour_${tourId}`);
|
||||
}
|
||||
notifyNewComment(tourId, data) {
|
||||
this.server.to(`tour_${tourId}`).emit('commentAdded', data);
|
||||
}
|
||||
};
|
||||
exports.CommentGateway = CommentGateway;
|
||||
__decorate([
|
||||
(0, websockets_1.WebSocketServer)(),
|
||||
__metadata("design:type", socket_io_1.Server)
|
||||
], CommentGateway.prototype, "server", void 0);
|
||||
__decorate([
|
||||
(0, websockets_1.SubscribeMessage)('joinTour'),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [socket_io_1.Socket, String]),
|
||||
__metadata("design:returntype", void 0)
|
||||
], CommentGateway.prototype, "handleJoinTour", null);
|
||||
exports.CommentGateway = CommentGateway = __decorate([
|
||||
(0, websockets_1.WebSocketGateway)({ cors: { origin: '*' } })
|
||||
], CommentGateway);
|
||||
let CommentController = class CommentController {
|
||||
constructor(prisma, commentGateway) {
|
||||
this.prisma = prisma;
|
||||
this.commentGateway = commentGateway;
|
||||
}
|
||||
async getComments(locationId) {
|
||||
return this.prisma.comment.findMany({
|
||||
where: { locationId },
|
||||
include: {
|
||||
user: { select: { name: true } }
|
||||
},
|
||||
orderBy: { createdAt: 'asc' }
|
||||
});
|
||||
}
|
||||
async addComment(locationId, body, req) {
|
||||
const comment = await this.prisma.comment.create({
|
||||
data: {
|
||||
content: body.content,
|
||||
locationId,
|
||||
userId: req.user.id
|
||||
},
|
||||
include: { user: { select: { name: true } } }
|
||||
});
|
||||
const location = await this.prisma.location.findUnique({
|
||||
where: { id: locationId },
|
||||
include: { leg: { select: { tourId: true } } }
|
||||
});
|
||||
if (location?.leg?.tourId) {
|
||||
this.commentGateway.notifyNewComment(location.leg.tourId, {
|
||||
...comment,
|
||||
locationId
|
||||
});
|
||||
}
|
||||
return comment;
|
||||
}
|
||||
};
|
||||
__decorate([
|
||||
(0, common_1.Get)(':locationId/comments'),
|
||||
__param(0, (0, common_1.Param)('locationId', common_1.ParseUUIDPipe)),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [String]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], CommentController.prototype, "getComments", null);
|
||||
__decorate([
|
||||
(0, common_1.Post)(':locationId/comments'),
|
||||
__param(0, (0, common_1.Param)('locationId', common_1.ParseUUIDPipe)),
|
||||
__param(1, (0, common_1.Body)()),
|
||||
__param(2, (0, common_1.Req)()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [String, Object, Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], CommentController.prototype, "addComment", null);
|
||||
CommentController = __decorate([
|
||||
(0, common_1.Controller)('locations'),
|
||||
(0, common_1.UseGuards)(jwt_auth_guard_1.JwtAuthGuard),
|
||||
__metadata("design:paramtypes", [prisma_service_1.PrismaService,
|
||||
CommentGateway])
|
||||
], CommentController);
|
||||
let AppModule = class AppModule {
|
||||
};
|
||||
AppModule = __decorate([
|
||||
@@ -1024,8 +1117,8 @@ AppModule = __decorate([
|
||||
signOptions: { expiresIn: '1d' },
|
||||
}),
|
||||
],
|
||||
controllers: [AppController, AuthController, TourController, UserController, RoutingController, LegController, LocationController],
|
||||
providers: [prisma_service_1.PrismaService, jwt_strategy_1.JwtStrategy, rbac_middleware_1.TourRoleGuard, jwt_auth_guard_1.JwtAuthGuard, admin_guard_1.AdminGuard],
|
||||
controllers: [AppController, AuthController, TourController, UserController, RoutingController, LegController, LocationController, CommentController],
|
||||
providers: [prisma_service_1.PrismaService, jwt_strategy_1.JwtStrategy, rbac_middleware_1.TourRoleGuard, jwt_auth_guard_1.JwtAuthGuard, admin_guard_1.AdminGuard, CommentGateway],
|
||||
exports: [prisma_service_1.PrismaService]
|
||||
})
|
||||
], AppModule);
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user