diff --git a/src/database/templates.ts b/src/database/templates.ts new file mode 100644 index 0000000..acfba0c --- /dev/null +++ b/src/database/templates.ts @@ -0,0 +1,3 @@ +export const getMessageById = "SELECT messages.id, messages.content, messages.channel_id, messages.created_at, messages.author_id, users.username AS author_username FROM messages JOIN users ON messages.author_id = users.id WHERE messages.id = $1"; +export const getMessagesByChannelFirstPage = "SELECT messages.id, messages.content, messages.channel_id, messages.created_at, messages.author_id, users.username AS author_username FROM messages JOIN users ON messages.author_id = users.id WHERE messages.channel_id = $1 ORDER BY id DESC LIMIT 50"; +export const getMessagesByChannelPage = "SELECT messages.id, messages.content, messages.channel_id, messages.created_at, messages.author_id, users.username AS author_username FROM messages JOIN users ON messages.author_id = users.id WHERE messages.id < $1 AND messages.channel_id = $2 ORDER BY id DESC LIMIT 50"; diff --git a/src/routes/api/v1/channels.ts b/src/routes/api/v1/channels.ts index a4c336e..81862f2 100644 --- a/src/routes/api/v1/channels.ts +++ b/src/routes/api/v1/channels.ts @@ -2,6 +2,7 @@ import express from "express"; import { body, param, validationResult } from "express-validator"; import { authenticateRoute } from "../../../auth"; import { query } from "../../../database"; +import { getMessageById, getMessagesByChannelFirstPage, getMessagesByChannelPage } from "../../../database/templates"; import { errors } from "../../../errors"; import { dispatch, dispatchChannelSubscribe } from "../../../gateway"; import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype"; @@ -190,6 +191,7 @@ router.post( content, channel_id: channelId, author_id: authorId, + author_username: req.user.username, created_at: createdAt }; @@ -218,10 +220,10 @@ router.get( let finalRows = []; if (before) { - const result = await query("SELECT id, content, channel_id, author_id, created_at FROM messages WHERE id < $1 AND channel_id = $2 ORDER BY id DESC LIMIT 50", [before, channelId]); + const result = await query(getMessagesByChannelPage, [before, channelId]); finalRows = result.rows; } else { - const result = await query("SELECT id, content, channel_id, author_id, created_at FROM messages WHERE channel_id = $1 ORDER BY id DESC LIMIT 50", [channelId]); + const result = await query(getMessagesByChannelFirstPage, [channelId]); finalRows = result.rows; } diff --git a/src/routes/api/v1/messages.ts b/src/routes/api/v1/messages.ts index d836c8e..d80f28d 100644 --- a/src/routes/api/v1/messages.ts +++ b/src/routes/api/v1/messages.ts @@ -2,6 +2,7 @@ import express from "express"; import { body, param, validationResult } from "express-validator"; import { authenticateRoute } from "../../../auth"; import { query } from "../../../database"; +import { getMessageById } from "../../../database/templates"; import { errors } from "../../../errors"; import { dispatch } from "../../../gateway"; import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype"; @@ -64,7 +65,7 @@ router.put( const { content } = req.body; const id = parseInt(req.params.id); // TODO: ?? - const permissionCheckResult = await query("SELECT author_id, channel_id, created_at FROM messages WHERE id = $1", [id]); + const permissionCheckResult = await query(getMessageById, [id]); if (permissionCheckResult.rowCount < 1) { return res.status(404).json({ ...errors.NOT_FOUND @@ -84,11 +85,8 @@ router.put( } const returnObject = { - id, - content, - channel_id: permissionCheckResult.rows[0].channel_id, - author_id: permissionCheckResult.rows[0].author_id, - created_at: permissionCheckResult.rows[0].created_at + ...permissionCheckResult.rows[0], + content }; dispatch(`channel:${permissionCheckResult.rows[0].channel_id}`, { @@ -111,7 +109,7 @@ router.get( } const { id } = req.params; - const result = await query("SELECT id, content, channel_id, author_id, created_at FROM messages WHERE id = $1", [id]); + const result = await query(getMessageById, [id]); if (result.rowCount < 1) { return res.status(404).json({ ...errors.NOT_FOUND diff --git a/test.rest b/test.rest index 35f62d7..abab5c2 100644 --- a/test.rest +++ b/test.rest @@ -75,7 +75,7 @@ Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6M ### -PUT http://localhost:3000/api/v1/messages/2 HTTP/1.1 +PUT http://localhost:3000/api/v1/messages/3 HTTP/1.1 content-type: application/json Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NzAwMTE0LCJleHAiOjE2NDk4NzI5MTR9.EOn8MBHZLCxfU5fHc0ZY2x9p3y-_RdD7X915L1B6Ftc @@ -85,7 +85,7 @@ Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6M ### -GET http://localhost:3000/api/v1/messages/2 +GET http://localhost:3000/api/v1/messages/3 Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NzAwMTE0LCJleHAiOjE2NDk4NzI5MTR9.EOn8MBHZLCxfU5fHc0ZY2x9p3y-_RdD7X915L1B6Ftc ###