Skip to content

Commit c392295

Browse files
committed
News with pictures
1 parent 68efe7a commit c392295

19 files changed

Lines changed: 1366 additions & 85 deletions

File tree

backend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"axios": "^1.8.4",
2424
"bcryptjs": "^3.0.2",
2525
"big-integer": "^1.6.52",
26+
"body-parser": "^2.2.0",
2627
"cors": "^2.8.5",
2728
"dotenv": "^16.5.0",
2829
"drizzle-kit": "^0.30.6",

backend/server.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
const express = require('express');
2-
const cors = require('cors');
1+
import express from 'express';
2+
import cors from 'cors';
3+
import bodyParser from 'body-parser';
34

45
import dotenv from 'dotenv';
6+
import path from "path";
7+
58
import authRoutes from './src/routes/auth.routes';
69
import roleRoutes from './src/routes/role.routes';
710
import userRoutes from './src/routes/user.routes';
@@ -27,10 +30,11 @@ dotenv.config();
2730
async function startServer() {
2831
const app = express();
2932

30-
// Configuration des middlewares
33+
// Configuration des middlewares
3134
app.use(cors({ origin: "*" }));
32-
app.use(express.json());
35+
app.use(bodyParser.json());
3336
app.use(express.urlencoded({ extended: true }));
37+
3438

3539
try {
3640
// Initialisation de la base de données
@@ -53,6 +57,7 @@ async function startServer() {
5357
app.use('/api/email',authenticateUser, emailRoutes);
5458
app.use('/api/news',authenticateUser, newsRoutes);
5559
app.use('/api/discord',authenticateUser, discordRoutes);
60+
app.use("/uploads/imgnews", authenticateUser, express.static(path.join(__dirname, "/uploads/imgnews")));
5661

5762
// Démarrage du serveur
5863
app.listen(server_port, () => {

backend/src/controllers/news.controller.ts

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,25 @@ import { Ok, Error } from "../utils/responses";
44
import * as template from "../utils/emailtemplates";
55
import * as email_service from "../services/email.service";
66
import * as user_service from '../services/user.service'
7+
import path from "path";
8+
import fs from "fs";
79

810
export const createNews = async (req: Request, res: Response) => {
11+
12+
913
const { title, description, type, published, target } = req.body;
14+
const file = req.file;
1015

1116
try {
12-
const news = await news_service.createNews(title, description, type, published, target);
17+
18+
const image_url = file ? `/uploads/imgnews/${file.filename}` : undefined;
19+
const news = await news_service.createNews(
20+
title,
21+
description,
22+
type,
23+
published,
24+
target,
25+
image_url);
1326
Ok(res, { msg: "Actu créée avec succès", data: news });
1427
} catch (err) {
1528
console.error(err);
@@ -51,17 +64,19 @@ export const listPublishedNewsByType = async (req: Request, res: Response) => {
5164
};
5265

5366
export const publishNews = async (req: Request, res: Response) => {
54-
const { id, title, description, type, target } = req.body;
67+
const { id} = req.body;
5568

5669
try {
5770
await news_service.publishNewsandNotify(id);
5871

72+
const news = await news_service.getNewsById(Number(id));
73+
5974
// Génération du mail HTML
60-
const html = template.compileTemplate({ title, description }, template.templateNotifyNews);
75+
const html = template.compileTemplate({title : news.title}, template.templateNotifyNews);
6176

62-
const recipients = target === "Tous"
77+
const recipients = news.target === "Tous"
6378
? (await user_service.getUsersAdmin()).map(u => u.email)
64-
: (await user_service.getUsersbyPermission(target)).map(u => u.email);
79+
: (await user_service.getUsersbyPermission(news.target)).map(u => u.email);
6580

6681
if(recipients.length === 0){
6782
Error(res, {msg : "No recipients"});
@@ -71,7 +86,7 @@ export const publishNews = async (req: Request, res: Response) => {
7186
const email = {
7287
from: "integration@utt.fr",
7388
to: [],
74-
subject: `[INTEGRATION UTT] Nouvelle actu : ${title}`,
89+
subject: `[INTEGRATION UTT] Nouvelle actu : ${news.title}`,
7590
html : html,
7691
cc: [],
7792
bcc: recipients,
@@ -91,9 +106,17 @@ export const deleteNews = async (req: Request, res: Response) => {
91106
const {newsId} = req.query
92107

93108
try {
94-
await news_service.deleteNews(Number(newsId));
95-
Ok(res, { msg: "Actus supprimée avec succès !" });
96-
return;
109+
110+
const existing = await news_service.getNewsById(Number(newsId));
111+
if (existing?.image_url) {
112+
const imagePath = path.join(__dirname, "../../", existing.image_url);
113+
if (fs.existsSync(imagePath)) {
114+
fs.unlinkSync(imagePath);
115+
}
116+
}
117+
await news_service.deleteNews(Number(newsId));
118+
Ok(res, { msg: "Actus supprimée avec succès !" });
119+
return;
97120

98121
} catch (error) {
99122
Error(res, { msg: "Erreur lors de la suppression de l'actus" });
@@ -102,25 +125,33 @@ export const deleteNews = async (req: Request, res: Response) => {
102125

103126

104127
export const updateNews = async (req: Request, res: Response) => {
105-
const { id, title, description, type, target } = req.body;
106-
107-
try {
108-
const updated = await news_service.updateNews(Number(id), {
109-
title,
110-
description,
111-
type,
112-
target,
113-
});
114-
115-
if (!updated) {
116-
Error(res, { msg: "Aucune actu trouvée à modifier" });
117-
return;
128+
const { id, title, description, type, target } = req.body;
129+
const file = req.file;
130+
const image_url = file ? `/uploads/imgnews/${file.filename}` : undefined;
131+
132+
try {
133+
const existing = await news_service.getNewsById(Number(id));
134+
if (!existing) {
135+
return Error(res, { msg: "Actu introuvable" });
136+
}
137+
138+
// Supprimer l'ancienne image si une nouvelle est uploadée
139+
if (file && existing.image_url) {
140+
const oldPath = path.join(__dirname, "../../", existing.image_url);
141+
if (fs.existsSync(oldPath)) {
142+
fs.unlinkSync(oldPath);
118143
}
119-
120-
Ok(res, { msg: "Actu mise à jour avec succès", data: updated });
121-
} catch (err) {
122-
console.error(err);
123-
Error(res, { msg: "Erreur lors de la mise à jour de l'actu" });
124144
}
145+
146+
const updates: any = { title, description, type, target };
147+
if (image_url) updates.image_url = image_url;
148+
149+
const updated = await news_service.updateNews(Number(id), updates);
150+
151+
Ok(res, { msg: "Actu mise à jour avec succès", data: updated });
152+
} catch (err) {
153+
console.error(err);
154+
Error(res, { msg: "Erreur lors de la mise à jour de l'actu" });
155+
}
125156
};
126157

backend/src/database/db.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@ import * as event from '../schemas/Basic/event.schema';
1010
import * as faction from '../schemas/Basic/faction.schema';
1111
import * as role from '../schemas/Basic/role.schema';
1212
import * as challenge from '../schemas/Basic/challenge.schema';
13+
import * as permanence from '../schemas/Basic/permanence.schema';
1314

1415
import * as userTeam from '../schemas/Relational/userteams.schema';
1516
import * as teamFaction from '../schemas/Relational/teamfaction.schema';
1617
import * as teamShotgun from '../schemas/Relational/teamshotgun.schema';
1718
import * as userPermanence from '../schemas/Relational/userpermanences.schema';
1819
import * as userRole from '../schemas/Relational/userroles.schema';
1920
import * as challengValidation from '../schemas/Relational/challengevalidation.schema';
21+
import * as busattribution from "../schemas/Relational/busattribution.schema";
22+
import * as registration from "../schemas/Relational/registration.schema";
2023

2124

2225
const client = new Client({
@@ -34,13 +37,16 @@ export const db = drizzle(client, {
3437
...role,
3538
...perm,
3639
...challenge,
40+
...permanence,
3741

3842
...userTeam,
3943
...teamFaction,
4044
...teamShotgun,
4145
...userPermanence,
4246
...userRole,
4347
...challengValidation,
48+
...busattribution,
49+
...registration
4450

4551
},
4652
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE "news" ADD COLUMN "image_url" text;

0 commit comments

Comments
 (0)