update all messages endpoints to also return author_username
for easier display
This commit is contained in:
parent
94192979a8
commit
5acb3ceaf0
4 changed files with 14 additions and 11 deletions
3
src/database/templates.ts
Normal file
3
src/database/templates.ts
Normal file
|
@ -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";
|
|
@ -2,6 +2,7 @@ import express from "express";
|
||||||
import { body, param, validationResult } from "express-validator";
|
import { body, param, validationResult } from "express-validator";
|
||||||
import { authenticateRoute } from "../../../auth";
|
import { authenticateRoute } from "../../../auth";
|
||||||
import { query } from "../../../database";
|
import { query } from "../../../database";
|
||||||
|
import { getMessageById, getMessagesByChannelFirstPage, getMessagesByChannelPage } from "../../../database/templates";
|
||||||
import { errors } from "../../../errors";
|
import { errors } from "../../../errors";
|
||||||
import { dispatch, dispatchChannelSubscribe } from "../../../gateway";
|
import { dispatch, dispatchChannelSubscribe } from "../../../gateway";
|
||||||
import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype";
|
import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype";
|
||||||
|
@ -190,6 +191,7 @@ router.post(
|
||||||
content,
|
content,
|
||||||
channel_id: channelId,
|
channel_id: channelId,
|
||||||
author_id: authorId,
|
author_id: authorId,
|
||||||
|
author_username: req.user.username,
|
||||||
created_at: createdAt
|
created_at: createdAt
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -218,10 +220,10 @@ router.get(
|
||||||
let finalRows = [];
|
let finalRows = [];
|
||||||
|
|
||||||
if (before) {
|
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;
|
finalRows = result.rows;
|
||||||
} else {
|
} 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;
|
finalRows = result.rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ import express from "express";
|
||||||
import { body, param, validationResult } from "express-validator";
|
import { body, param, validationResult } from "express-validator";
|
||||||
import { authenticateRoute } from "../../../auth";
|
import { authenticateRoute } from "../../../auth";
|
||||||
import { query } from "../../../database";
|
import { query } from "../../../database";
|
||||||
|
import { getMessageById } from "../../../database/templates";
|
||||||
import { errors } from "../../../errors";
|
import { errors } from "../../../errors";
|
||||||
import { dispatch } from "../../../gateway";
|
import { dispatch } from "../../../gateway";
|
||||||
import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype";
|
import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype";
|
||||||
|
@ -64,7 +65,7 @@ router.put(
|
||||||
const { content } = req.body;
|
const { content } = req.body;
|
||||||
const id = parseInt(req.params.id); // TODO: ??
|
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) {
|
if (permissionCheckResult.rowCount < 1) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
...errors.NOT_FOUND
|
...errors.NOT_FOUND
|
||||||
|
@ -84,11 +85,8 @@ router.put(
|
||||||
}
|
}
|
||||||
|
|
||||||
const returnObject = {
|
const returnObject = {
|
||||||
id,
|
...permissionCheckResult.rows[0],
|
||||||
content,
|
content
|
||||||
channel_id: permissionCheckResult.rows[0].channel_id,
|
|
||||||
author_id: permissionCheckResult.rows[0].author_id,
|
|
||||||
created_at: permissionCheckResult.rows[0].created_at
|
|
||||||
};
|
};
|
||||||
|
|
||||||
dispatch(`channel:${permissionCheckResult.rows[0].channel_id}`, {
|
dispatch(`channel:${permissionCheckResult.rows[0].channel_id}`, {
|
||||||
|
@ -111,7 +109,7 @@ router.get(
|
||||||
}
|
}
|
||||||
|
|
||||||
const { id } = req.params;
|
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) {
|
if (result.rowCount < 1) {
|
||||||
return res.status(404).json({
|
return res.status(404).json({
|
||||||
...errors.NOT_FOUND
|
...errors.NOT_FOUND
|
||||||
|
|
|
@ -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
|
content-type: application/json
|
||||||
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NzAwMTE0LCJleHAiOjE2NDk4NzI5MTR9.EOn8MBHZLCxfU5fHc0ZY2x9p3y-_RdD7X915L1B6Ftc
|
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
|
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NzAwMTE0LCJleHAiOjE2NDk4NzI5MTR9.EOn8MBHZLCxfU5fHc0ZY2x9p3y-_RdD7X915L1B6Ftc
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
Loading…
Reference in a new issue