add: added cats, admin rights and owner_username display

This commit is contained in:
Lukian 2025-03-24 23:58:40 +01:00
parent 7dea3612af
commit 0e2139f408
9 changed files with 15 additions and 5 deletions

View file

@ -44,7 +44,7 @@ router.post('/me', async (req, res) => {
const users = await getUser(connection, decoded.id); const users = await getUser(connection, decoded.id);
connection.end(); connection.end();
if (users[0]) { if (users[0]) {
res.send({ id: users[0].id, username: users[0].username }); res.send({ id: users[0].id, username: users[0].username, admin: users[0].admin });
} else { } else {
res.status(401).send({ error: 'Invalid token' }); res.status(401).send({ error: 'Invalid token' });
} }

View file

@ -77,7 +77,7 @@ router.post('/:name/messages/delete', async (req, res) => {
return res.status(400).send({ error: 'No channel found' }); return res.status(400).send({ error: 'No channel found' });
} }
if (user[0].id !== channel[0].owner_id && user[0].id !== message_id) { if (user[0].id !== channel[0].owner_id && user[0].id !== message_id && user[0].admin !== 1) {
connection.end(); connection.end();
return res.status(401).send({ error: 'Unauthorized' }); return res.status(401).send({ error: 'Unauthorized' });
} }

View file

@ -10,7 +10,7 @@ router.get('/:id', async (req, res) => {
const users = await getUser(connection, id); const users = await getUser(connection, id);
connection.end(); connection.end();
if (users[0]) { if (users[0]) {
res.send({id: users[0].id, username: users[0].username}); res.send({id: users[0].id, username: users[0].username, admin: users[0].admin});
} else { } else {
res.send('No user found'); res.send('No user found');
} }

View file

@ -68,7 +68,7 @@ function getChannels(connection) {
function getChannel(connection, name) { function getChannel(connection, name) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
connection.query( connection.query(
`SELECT * FROM channels WHERE name = "${name}"`, `SELECT channels.id, name, description, owner_id, username AS owner_username FROM channels JOIN users ON channels.owner_id = users.id WHERE name = "${name}"`,
(error, result) => { (error, result) => {
if (error) { if (error) {
reject(new Error(error)); reject(new Error(error));

BIN
front/public/cat.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB

View file

@ -0,0 +1,3 @@
.cat {
width: 100px;
}

View file

@ -73,6 +73,10 @@ export default function Channel() {
<Link to="/">Home</Link> <Link to="/">Home</Link>
<h1>Channel {channel.name}</h1> <h1>Channel {channel.name}</h1>
<p>{channel.description}</p> <p>{channel.description}</p>
<p>Owner: {channel.owner_username}</p>
{channel.name.toLowerCase().includes("cat") && (
<img src="cat.jpg" alt="cat" className="cat" />
)}
{ {
token ? ( token ? (
<form onSubmit={handleSubmit}> <form onSubmit={handleSubmit}>
@ -95,7 +99,7 @@ export default function Channel() {
{messages.slice(0, maxMessageToShown).map((message) => ( {messages.slice(0, maxMessageToShown).map((message) => (
<li key={message.id}> <li key={message.id}>
<p>{message.username}: {message.content}</p> <p>{message.username}: {message.content}</p>
{(user?.id === message.user_id || user?.id === channel.owner_id) && ( {(user?.id === message.user_id || user?.id === channel.owner_id || user?.admin === 1) && (
<button onClick={() => {deleteMessage(message.id)}}>Delete</button> <button onClick={() => {deleteMessage(message.id)}}>Delete</button>
)} )}
</li> </li>

View file

@ -52,6 +52,7 @@ export default function Home() {
</li> </li>
))} ))}
</ul> </ul>
<img src="cat.jpg" alt="cat" className="cat"/>
</div> </div>
) )
} }

View file

@ -3,6 +3,7 @@ export type Channel_type ={
name: string name: string
description: string description: string
owner_id: number owner_id: number
owner_username: string
} }
export type Channels = Channel_type[] export type Channels = Channel_type[]
@ -20,4 +21,5 @@ export type Messages = Message[]
export type User = { export type User = {
id: number, id: number,
username: string username: string
admin: number
} }