102 lines
3.1 KiB
TypeScript
102 lines
3.1 KiB
TypeScript
import { ChatService } from "@/services/chatService.js";
|
|
import {
|
|
AddChatRoomMemberRequest,
|
|
AuthenticatedRequest,
|
|
CreateChatRoomRequest,
|
|
GetMessagesQuery,
|
|
RemoveChatRoomMemberRequest,
|
|
UpdateChatRoomRequest,
|
|
} from "@/types/index.js";
|
|
import type { Response } from "express";
|
|
|
|
export class ChatController {
|
|
static async getChatRooms(req: AuthenticatedRequest, res: Response) {
|
|
if (!req.user) {
|
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
|
return;
|
|
}
|
|
|
|
const result = await ChatService.getChatRooms(req.user.userId);
|
|
res.status(result.success ? 200 : 500).json(result);
|
|
}
|
|
|
|
static async createChatRoom(req: AuthenticatedRequest, res: Response) {
|
|
if (!req.user) {
|
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
|
return;
|
|
}
|
|
|
|
const data: CreateChatRoomRequest = req.body;
|
|
const result = await ChatService.createChatRoom(req.user.userId, data);
|
|
|
|
res.status(result.success ? 201 : 500).json(result);
|
|
}
|
|
|
|
static async getMessages(req: AuthenticatedRequest, res: Response) {
|
|
if (!req.user) {
|
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
|
return;
|
|
}
|
|
|
|
const query: GetMessagesQuery = {
|
|
roomId: req.params.roomId,
|
|
page: req.query.page as string,
|
|
limit: req.query.limit as string,
|
|
};
|
|
|
|
const result = await ChatService.getMessages(query);
|
|
res.status(result.success ? 200 : 500).json(result);
|
|
}
|
|
|
|
static async updateChatRoom(req: AuthenticatedRequest, res: Response) {
|
|
if (!req.user) {
|
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
|
return;
|
|
}
|
|
|
|
const roomId = req.params.roomId;
|
|
const data: UpdateChatRoomRequest = req.body;
|
|
|
|
const result = await ChatService.updateChatRoom(req.user.userId, roomId, data);
|
|
res.status(result.success ? 200 : result.status || 400).json(result);
|
|
}
|
|
|
|
static async deleteChatRoom(req: AuthenticatedRequest, res: Response) {
|
|
if (!req.user) {
|
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
|
return;
|
|
}
|
|
|
|
const roomId = req.params.roomId;
|
|
|
|
const result = await ChatService.deleteChatRoom(req.user.userId, roomId);
|
|
res.status(result.success ? 200 : result.status || 500).json(result);
|
|
}
|
|
|
|
static async addChatRoomMember(req: AuthenticatedRequest, res: Response) {
|
|
if (!req.user) {
|
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
|
return;
|
|
}
|
|
|
|
const roomId = req.params.roomId;
|
|
const data: AddChatRoomMemberRequest = req.body;
|
|
|
|
const result = await ChatService.addChatRoomMember(req.user.userId, roomId, data);
|
|
res.status(result.success ? 200 : result.status || 500).json(result);
|
|
}
|
|
|
|
static async removeChatRoomMember(req: AuthenticatedRequest, res: Response) {
|
|
if (!req.user) {
|
|
res.status(401).json({ success: false, error: "Unauthorized" });
|
|
return;
|
|
}
|
|
|
|
const roomId = req.params.roomId;
|
|
const data: RemoveChatRoomMemberRequest = req.body;
|
|
|
|
const result = await ChatService.removeChatRoomMember(req.user.userId, roomId, data);
|
|
res.status(result.success ? 200 : result.status || 500).json(result);
|
|
}
|
|
}
|