generated from lucien/api-template
add: changed homepage to display most active channels
This commit is contained in:
parent
411aa149ac
commit
dfb6639ecf
10 changed files with 179 additions and 14 deletions
13
back/api/activechannels.js
Normal file
13
back/api/activechannels.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
const express = require('express');
|
||||
const { getConnection, getActiveChannels } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
const connection = await getConnection();
|
||||
const channels = await getActiveChannels(connection);
|
||||
connection.end();
|
||||
res.send(channels);
|
||||
});
|
||||
|
||||
module.exports = router;
|
|
@ -1,5 +1,4 @@
|
|||
const express = require('express');
|
||||
const jwt = require('jsonwebtoken');
|
||||
const { getConnection, getChannels, getChannel, addChannel, getMessages, getMessage, addMessage, deleteMessage, getLastMessages } = require('../libs/mysql');
|
||||
const { checkAuth } = require('../libs/middlewares');
|
||||
|
||||
|
|
14
back/api/searchchannels.js
Normal file
14
back/api/searchchannels.js
Normal file
|
@ -0,0 +1,14 @@
|
|||
const express = require('express');
|
||||
const { getConnection, searchChannels } = require('../libs/mysql');
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', async (req, res) => {
|
||||
const { search } = req.query;
|
||||
const connection = await getConnection();
|
||||
const channels = await searchChannels(connection, search);
|
||||
connection.end();
|
||||
res.send(channels);
|
||||
});
|
||||
|
||||
module.exports = router;
|
|
@ -88,6 +88,46 @@ function getChannels(connection) {
|
|||
});
|
||||
}
|
||||
|
||||
function getActiveChannels(connection) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
`SELECT channels.id, name, description, owner_id, username AS owner_username, count(*) AS message_count
|
||||
FROM messages
|
||||
JOIN channels ON messages.channel_id = channels.id
|
||||
JOIN users ON messages.user_id = users.id
|
||||
WHERE date > (SELECT max(date) FROM messages) - 7 * 24 * 60 * 60
|
||||
GROUP BY channel_id
|
||||
ORDER BY count(*) DESC
|
||||
LIMIT 5;`,
|
||||
(error, result) => {
|
||||
if (error) {
|
||||
reject(new Error(error));
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function searchChannels(connection, search) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
`SELECT channels.id, name, description, owner_id, username AS owner_username
|
||||
FROM channels
|
||||
JOIN users ON channels.owner_id = users.id
|
||||
WHERE name LIKE ?
|
||||
LIMIT 5`,
|
||||
[`%${search}%`], // Use parameterized query
|
||||
(error, result) => {
|
||||
if (error) {
|
||||
reject(new Error(error));
|
||||
}
|
||||
resolve(result);
|
||||
}
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
function getChannel(connection, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
connection.query(
|
||||
|
@ -215,6 +255,8 @@ module.exports = {
|
|||
addUser,
|
||||
getUserLastMessages,
|
||||
getChannels,
|
||||
getActiveChannels,
|
||||
searchChannels,
|
||||
getChannel,
|
||||
addChannel,
|
||||
getMessages,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue