Add logging and fix channel merging and removing

This commit is contained in:
2024-08-24 00:21:54 +02:00
parent 7055713d4b
commit 3575c910f4
14 changed files with 163 additions and 7 deletions

View File

@@ -1,5 +1,6 @@
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;
@@ -7,7 +8,7 @@ export const createChannel = async (req: Request, res: Response) => {
return res.status(400).json({ error: 'Name is required' });
}
const chan = await ChannelService.createChannel(name);
logger.info(`Channel ${name} created`);
res.json(chan);
}
@@ -19,8 +20,10 @@ export const deleteChannel = async (req: Request, res: Response) => {
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' });
}
@@ -37,6 +40,7 @@ export const mergeChannel = async (req: Request, res: Response) => {
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' });
}
@@ -52,6 +56,7 @@ export const updateChannel = async (req: Request, res: Response) => {
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' });
}

View File

@@ -1,5 +1,6 @@
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;
@@ -16,6 +17,7 @@ export const uploadFile = async (req: Request, res: Response) => {
}
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 });
}

View File

@@ -1,5 +1,6 @@
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;
@@ -8,6 +9,7 @@ export const createMessage = async (req: Request, res: Response) => {
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() });
};
@@ -19,6 +21,10 @@ export const updateMessage = async (req: Request, res: Response) => {
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 });
}
@@ -32,6 +38,7 @@ export const deleteMessage = async (req: Request, res: Response) => {
if (result.changes === 0) {
return res.status(404).json({ error: 'Message not found' });
}
logger.info(`Message ${messageId} deleted`);
res.json({ message: 'Message deleted successfully' });
}

View File

@@ -1,5 +1,6 @@
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;
@@ -7,5 +8,6 @@ export const search = async (req: Request, res: Response) => {
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 });
}

View File

@@ -1,3 +1,4 @@
import type { Message } from "../../types";
import { events, logger } from "../globals"
import { describeImage } from "../services/image-description";
import { getMessage, updateMessage } from "../services/message-service";

View File

@@ -10,7 +10,7 @@ export const createChannel = async (name: string) => {
export const deleteChannel = async (id: string) => {
const query = db.query(`DELETE FROM channels WHERE id = ($channelId)`);
const result = db.run(id);
const result = query.run({$channelId: id});
// No need to manually delete messages and files as they are set to cascade on delete in the schema
events .emit('channel-deleted', id);
return result;

View File

@@ -3,7 +3,7 @@ import { events } from "../globals";
export const uploadFile = async (channelId: string, messageId: string, filePath: string, fileType: string, fileSize: number, originalName: string) => {
const query = db.query(`INSERT INTO files (channelId, filePath, fileType, fileSize, originalName) VALUES ($channelId, $filePath, $fileType, $fileSize, $originalName)`);
const result = query.run({ $channelId: channelId, $filePath: filePath, $fileType: fileType, $fileSize: fileSize, $originalName: originalName } as any);
const result = query.run({ $channelId: channelId, $filePath: filePath, $fileType: fileType, $fileSize: fileSize, $originalName: originalName });
const fileId = result.lastInsertRowid;