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 express = require('express');
|
||||||
const jwt = require('jsonwebtoken');
|
|
||||||
const { getConnection, getChannels, getChannel, addChannel, getMessages, getMessage, addMessage, deleteMessage, getLastMessages } = require('../libs/mysql');
|
const { getConnection, getChannels, getChannel, addChannel, getMessages, getMessage, addMessage, deleteMessage, getLastMessages } = require('../libs/mysql');
|
||||||
const { checkAuth } = require('../libs/middlewares');
|
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) {
|
function getChannel(connection, name) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
connection.query(
|
connection.query(
|
||||||
|
@ -215,6 +255,8 @@ module.exports = {
|
||||||
addUser,
|
addUser,
|
||||||
getUserLastMessages,
|
getUserLastMessages,
|
||||||
getChannels,
|
getChannels,
|
||||||
|
getActiveChannels,
|
||||||
|
searchChannels,
|
||||||
getChannel,
|
getChannel,
|
||||||
addChannel,
|
addChannel,
|
||||||
getMessages,
|
getMessages,
|
||||||
|
|
|
@ -8,6 +8,7 @@ import UserPage from './pages/UserPage'
|
||||||
import Login from './pages/Login'
|
import Login from './pages/Login'
|
||||||
import Register from './pages/Register'
|
import Register from './pages/Register'
|
||||||
import CreateChannel from './pages/CreateChannel'
|
import CreateChannel from './pages/CreateChannel'
|
||||||
|
import ChannelsPage from './pages/ChannelsPage'
|
||||||
|
|
||||||
createRoot(document.getElementById('root')!).render(
|
createRoot(document.getElementById('root')!).render(
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
|
@ -18,6 +19,7 @@ createRoot(document.getElementById('root')!).render(
|
||||||
<Route path="/login" element={<Login />} />
|
<Route path="/login" element={<Login />} />
|
||||||
<Route path="/register" element={<Register />} />
|
<Route path="/register" element={<Register />} />
|
||||||
<Route path="/create-channel" element={<CreateChannel />} />
|
<Route path="/create-channel" element={<CreateChannel />} />
|
||||||
|
<Route path="/channels" element={<ChannelsPage />} />
|
||||||
</Routes>
|
</Routes>
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
)
|
)
|
||||||
|
|
|
@ -70,7 +70,7 @@ export default function ChannelPage() {
|
||||||
.get(`/api/channels/${name}/messages`).then((res) => {
|
.get(`/api/channels/${name}/messages`).then((res) => {
|
||||||
setMessages(res.data)
|
setMessages(res.data)
|
||||||
})
|
})
|
||||||
}, 5000)
|
}, 2000)
|
||||||
|
|
||||||
return () => { clearInterval(id) }
|
return () => { clearInterval(id) }
|
||||||
}, [])
|
}, [])
|
||||||
|
|
52
front/src/pages/ChannelsPage.tsx
Normal file
52
front/src/pages/ChannelsPage.tsx
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
import { useState, useEffect } from "react"
|
||||||
|
import { Link } from "react-router-dom"
|
||||||
|
import { Channels } from "../types"
|
||||||
|
import axios from "axios"
|
||||||
|
|
||||||
|
export default function ChannelsPage() {
|
||||||
|
const [channels, setChannels] = useState<Channels>();
|
||||||
|
const [search, setSearch] = useState<string>("");
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
axios
|
||||||
|
.get("/api/channels")
|
||||||
|
.then((res) => {
|
||||||
|
setChannels(res.data)
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
console.error(err.response)
|
||||||
|
})
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const id = setInterval(() => {
|
||||||
|
axios
|
||||||
|
.get("/api/channels").then((res) => {
|
||||||
|
setChannels(res.data)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}, 2000)
|
||||||
|
|
||||||
|
return () => { clearInterval(id) }
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Link to="/">Home</Link>
|
||||||
|
<h1>Channels</h1>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Search channels"
|
||||||
|
value={search}
|
||||||
|
onChange={(e) => setSearch(e.target.value)}
|
||||||
|
/>
|
||||||
|
<ul>
|
||||||
|
{channels?.filter((channel) => channel.name.toLowerCase().includes(search.toLowerCase())).map((channel) => (
|
||||||
|
<li key={channel.id}>
|
||||||
|
<Link to={`/c/${channel.name}`}>{channel.name}</Link>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
|
@ -1,12 +1,14 @@
|
||||||
import { useState, useEffect } from "react"
|
import { useState, useEffect } from "react"
|
||||||
import { Link } from "react-router-dom"
|
import { Link } from "react-router-dom"
|
||||||
import { Channels, User, Messages } from "../types"
|
import { Channels, RecentChannels, User, Messages } from "../types"
|
||||||
import axios from "axios"
|
import axios from "axios"
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
const [user, setUser] = useState<User>();
|
const [user, setUser] = useState<User>();
|
||||||
const [channels, setChannels] = useState<Channels>();
|
const [channels, setChannels] = useState<RecentChannels>();
|
||||||
const [messages , setMessages] = useState<Messages>();
|
const [messages , setMessages] = useState<Messages>();
|
||||||
|
const [searchedChannels, setSearchedChannels] = useState<Channels>([]);
|
||||||
|
const [search, setSearch] = useState<string>("");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const token = localStorage.getItem("token")
|
const token = localStorage.getItem("token")
|
||||||
|
@ -24,7 +26,7 @@ export default function Home() {
|
||||||
}
|
}
|
||||||
|
|
||||||
axios
|
axios
|
||||||
.get("/api/channels")
|
.get("/api/activechannels")
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
setChannels(res.data)
|
setChannels(res.data)
|
||||||
})
|
})
|
||||||
|
@ -45,22 +47,37 @@ export default function Home() {
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const id = setInterval(() => {
|
const id = setInterval(() => {
|
||||||
|
axios
|
||||||
|
.get("/api/activechannels").then((res) => {
|
||||||
|
setChannels(res.data)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
axios
|
axios
|
||||||
.get("/api/lastmessages").then((res) => {
|
.get("/api/lastmessages").then((res) => {
|
||||||
setMessages(res.data)
|
setMessages(res.data)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
axios
|
}, 2000)
|
||||||
.get("/api/channels").then((res) => {
|
|
||||||
setChannels(res.data)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}, 5000)
|
|
||||||
|
|
||||||
return () => { clearInterval(id) }
|
return () => { clearInterval(id) }
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!search) {
|
||||||
|
setSearchedChannels([])
|
||||||
|
return
|
||||||
|
}
|
||||||
|
axios
|
||||||
|
.get(`/api/searchchannels?search=${search}`).then((res) => {
|
||||||
|
setSearchedChannels(res.data)
|
||||||
|
}
|
||||||
|
).catch((err) => {
|
||||||
|
console.error(err.response)
|
||||||
|
})
|
||||||
|
}, [search])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<h1>Home</h1>
|
<h1>Home</h1>
|
||||||
|
@ -79,7 +96,7 @@ export default function Home() {
|
||||||
<Link to="/register">Register</Link>
|
<Link to="/register">Register</Link>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
<h2>Channels</h2>
|
<h2>Recent active channels</h2>
|
||||||
<ul>
|
<ul>
|
||||||
{channels?.map((channel) => (
|
{channels?.map((channel) => (
|
||||||
<li key={channel.id}>
|
<li key={channel.id}>
|
||||||
|
@ -87,6 +104,21 @@ export default function Home() {
|
||||||
</li>
|
</li>
|
||||||
))}
|
))}
|
||||||
</ul>
|
</ul>
|
||||||
|
<h2>Search channels</h2>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
placeholder="Search channels"
|
||||||
|
value={search}
|
||||||
|
onChange={(e) => setSearch(e.target.value)}
|
||||||
|
/>
|
||||||
|
<ul>
|
||||||
|
{searchedChannels?.map((channel) => (
|
||||||
|
<li key={channel.id}>
|
||||||
|
<Link to={`/c/${channel.name}`}>{channel.name}</Link>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
<Link to={'/channels'}>All channels</Link>
|
||||||
<h2>Last messages</h2>
|
<h2>Last messages</h2>
|
||||||
<ul>
|
<ul>
|
||||||
{messages?.map((message) => (
|
{messages?.map((message) => (
|
||||||
|
|
|
@ -24,7 +24,7 @@ export default function UserPage() {
|
||||||
.get(`/api/users/${username}/lastmessages`).then((res) => {
|
.get(`/api/users/${username}/lastmessages`).then((res) => {
|
||||||
setMessages(res.data)
|
setMessages(res.data)
|
||||||
})
|
})
|
||||||
}, 5000)
|
}, 2000)
|
||||||
|
|
||||||
return () => { clearInterval(id) }
|
return () => { clearInterval(id) }
|
||||||
}, [])
|
}, [])
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
export type Channel ={
|
export type Channel = {
|
||||||
id: number,
|
id: number,
|
||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
|
@ -8,6 +8,17 @@ export type Channel ={
|
||||||
|
|
||||||
export type Channels = Channel[]
|
export type Channels = Channel[]
|
||||||
|
|
||||||
|
export type RecentChannel = {
|
||||||
|
id: number,
|
||||||
|
name: string
|
||||||
|
description: string
|
||||||
|
owner_id: number
|
||||||
|
owner_username: string
|
||||||
|
message_count: number
|
||||||
|
}
|
||||||
|
|
||||||
|
export type RecentChannels = RecentChannel[]
|
||||||
|
|
||||||
export type Message = {
|
export type Message = {
|
||||||
id: number,
|
id: number,
|
||||||
user_id: number,
|
user_id: number,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue