Initial move
This commit is contained in:
62
backend/src/controllers/channel-controller.ts
Normal file
62
backend/src/controllers/channel-controller.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import type { Request, Response } from "express";
|
||||
import * as ChannelService from "../services/channel-service";
|
||||
import { logger } from "../globals";
|
||||
|
||||
export const createChannel = async (req: Request, res: Response) => {
|
||||
const { name } = req.body;
|
||||
if (!name) {
|
||||
return res.status(400).json({ error: 'Name is required' });
|
||||
}
|
||||
const chan = await ChannelService.createChannel(name);
|
||||
logger.info(`Channel ${name} created`);
|
||||
res.json(chan);
|
||||
}
|
||||
|
||||
export const deleteChannel = async (req: Request, res: Response) => {
|
||||
const { channelId } = req.params;
|
||||
if (!channelId) {
|
||||
return res.status(400).json({ error: 'Channel ID is required' });
|
||||
}
|
||||
const result = await ChannelService.deleteChannel(channelId);
|
||||
|
||||
if (result.changes === 0) {
|
||||
logger.warn(`Channel ${channelId} not found while deleting`);
|
||||
return res.status(404).json({ error: 'Channel not found' });
|
||||
}
|
||||
logger.info(`Channel ${channelId} deleted`);
|
||||
|
||||
res.json({ message: 'Channel deleted successfully' });
|
||||
}
|
||||
|
||||
export const getChannels = async (req: Request, res: Response) => {
|
||||
const channels = await ChannelService.getChannels();
|
||||
res.json({ channels });
|
||||
}
|
||||
|
||||
export const mergeChannel = async (req: Request, res: Response) => {
|
||||
const { channelId } = req.params;
|
||||
const { targetChannelId } = req.body;
|
||||
if (!channelId || !targetChannelId) {
|
||||
return res.status(400).json({ error: 'Channel ID and targetChannelId are required' });
|
||||
}
|
||||
const result = await ChannelService.mergeChannel(channelId, targetChannelId);
|
||||
logger.info(`Channel ${targetChannelId} merged into ${channelId}`);
|
||||
|
||||
res.json({ message: 'Channels merged successfully' });
|
||||
}
|
||||
|
||||
export const updateChannel = async (req: Request, res: Response) => {
|
||||
const { channelId } = req.params;
|
||||
const { name } = req.body;
|
||||
if (!channelId || !name) {
|
||||
return res.status(400).json({ error: 'Channel ID and name are required' });
|
||||
}
|
||||
const result = await ChannelService.updateChannel(channelId, name);
|
||||
|
||||
if (result.changes === 0) {
|
||||
return res.status(404).json({ error: 'Channel not found' });
|
||||
}
|
||||
logger.info(`Channel ${channelId} updated as ${name}`);
|
||||
|
||||
res.json({ message: 'Channel updated successfully' });
|
||||
}
|
||||
33
backend/src/controllers/file-controller.ts
Normal file
33
backend/src/controllers/file-controller.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import type { Request, Response } from "express";
|
||||
import * as FileService from "../services/file-service";
|
||||
import { logger } from "../globals";
|
||||
|
||||
export const uploadFile = async (req: Request, res: Response) => {
|
||||
const { channelId, messageId } = req.params;
|
||||
const filePath = (req.file as Express.Multer.File).path;
|
||||
const fileType = req.file?.mimetype;
|
||||
const fileSize = req.file?.size;
|
||||
const originalName = req.file?.originalname;
|
||||
|
||||
if (!channelId || !messageId) {
|
||||
return res.status(400).json({ error: 'Channel ID and message ID are required' });
|
||||
}
|
||||
if (!filePath || !fileType || !fileSize || !originalName) {
|
||||
return res.status(400).json({ error: 'File is required' });
|
||||
}
|
||||
|
||||
const result = await FileService.uploadFile(channelId, messageId, filePath, fileType!, fileSize!, originalName!);
|
||||
logger.info(`File ${originalName} uploaded to message ${messageId} as ${filePath}`);
|
||||
res.json({ id: result.lastInsertRowid, channelId, messageId, filePath, fileType });
|
||||
}
|
||||
|
||||
|
||||
export const getFiles = async (req: Request, res: Response) => {
|
||||
const { messageId } = req.params;
|
||||
if (!messageId) {
|
||||
return res.status(400).json({ error: 'Message ID is required' });
|
||||
}
|
||||
const files = await FileService.getFiles(messageId);
|
||||
res.json({ files });
|
||||
}
|
||||
|
||||
54
backend/src/controllers/message-controller.ts
Normal file
54
backend/src/controllers/message-controller.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import type { Request, Response } from "express";
|
||||
import * as MessageService from "../services/message-service";
|
||||
import { logger } from "../globals";
|
||||
|
||||
export const createMessage = async (req: Request, res: Response) => {
|
||||
const { content } = req.body;
|
||||
const { channelId } = req.params;
|
||||
if (!content || !channelId) {
|
||||
return res.status(400).json({ error: 'Content and channel ID are required' });
|
||||
}
|
||||
const messageId = await MessageService.createMessage(channelId, content);
|
||||
logger.info(`Message ${messageId} created in channel ${channelId}`);
|
||||
|
||||
res.json({ id: messageId, channelId, content, createdAt: new Date().toISOString() });
|
||||
};
|
||||
|
||||
export const updateMessage = async (req: Request, res: Response) => {
|
||||
const { content } = req.body;
|
||||
const { messageId } = req.params;
|
||||
if (!content || !messageId) {
|
||||
return res.status(400).json({ error: 'Content and message ID are required ' });
|
||||
}
|
||||
const result = await MessageService.updateMessage(messageId, content);
|
||||
if (result.changes === 0) {
|
||||
return res.status(404).json({ error: 'Message not found' });
|
||||
}
|
||||
logger.info(`Message ${messageId} updated`);
|
||||
|
||||
res.json({ id: messageId, content });
|
||||
}
|
||||
|
||||
export const deleteMessage = async (req: Request, res: Response) => {
|
||||
const { messageId } = req.params;
|
||||
if (!messageId) {
|
||||
return res.status(400).json({ error: 'Message ID is required' });
|
||||
}
|
||||
const result = await MessageService.deleteMessage(messageId);
|
||||
if (result.changes === 0) {
|
||||
return res.status(404).json({ error: 'Message not found' });
|
||||
}
|
||||
logger.info(`Message ${messageId} deleted`);
|
||||
|
||||
res.json({ message: 'Message deleted successfully' });
|
||||
}
|
||||
|
||||
export const getMessages = async (req: Request, res: Response) => {
|
||||
const { channelId } = req.params;
|
||||
if (!channelId) {
|
||||
return res.status(400).json({ error: 'Channel ID is required' });
|
||||
}
|
||||
const messages = await MessageService.getMessages(channelId);
|
||||
|
||||
res.json({ messages });
|
||||
}
|
||||
13
backend/src/controllers/search-controller.ts
Normal file
13
backend/src/controllers/search-controller.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import type { Request, Response } from "express";
|
||||
import * as SearchService from "../services/search-service";
|
||||
import { logger } from "../globals";
|
||||
|
||||
export const search = async (req: Request, res: Response) => {
|
||||
const { query, channelId } = req.query;
|
||||
if (!query) {
|
||||
return res.status(400).json({ error: 'Query is required' });
|
||||
}
|
||||
const results = await SearchService.search(query as string, channelId as string);
|
||||
logger.info(`Searched for ${query}`);
|
||||
res.json({ results });
|
||||
}
|
||||
29
backend/src/controllers/websocket-controller.ts
Normal file
29
backend/src/controllers/websocket-controller.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { events } from "../globals";
|
||||
import { WebSocket } from "ws";
|
||||
|
||||
export const attachEvents = (ws: WebSocket) => {
|
||||
events.on('file-uploaded', (id, channelId, messageId, filePath, fileType, fileSize, originalName) => {
|
||||
ws.send(JSON.stringify({ type: 'file-uploaded', data: {id, channelId, messageId, filePath, fileType, fileSize, originalName }}));
|
||||
});
|
||||
events.on('message-created', (id, channelId, content) => {
|
||||
ws.send(JSON.stringify({ type: 'message-created', data: {id, channelId, content }}));
|
||||
});
|
||||
events.on('message-updated', (id, content) => {
|
||||
ws.send(JSON.stringify({ type: 'message-updated', data: {id, content }}));
|
||||
});
|
||||
events.on('message-deleted', (id) => {
|
||||
ws.send(JSON.stringify({ type: 'message-deleted', data: {id }}));
|
||||
});
|
||||
events.on('channel-created', (channel) => {
|
||||
ws.send(JSON.stringify({ type: 'channel-created', data: {channel }}));
|
||||
});
|
||||
events.on('channel-deleted', (id) => {
|
||||
ws.send(JSON.stringify({ type: 'channel-deleted', data: {id} }));
|
||||
});
|
||||
events.on('channel-merged', (channelId, targetChannelId) => {
|
||||
ws.send(JSON.stringify({ type: 'channel-merged', data: {channelId, targetChannelId }}));
|
||||
});
|
||||
events.on('channel-updated', (id, name) => {
|
||||
ws.send(JSON.stringify({ type: 'channel-updated', data: {id, name }}));
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user