diff --git a/src/routes/articles/controllers/crud.ts b/src/routes/articles/controllers/crud.ts index 12e1f14..f3f1822 100644 --- a/src/routes/articles/controllers/crud.ts +++ b/src/routes/articles/controllers/crud.ts @@ -1,12 +1,12 @@ import { Request, Response } from 'express'; import axios from "axios"; +import path from "path"; import { prisma } from '../../../lib/prisma'; import { AuthRequest } from '../../../middleware/auth'; import { checkPermission } from '../../../utils/permissions'; import { logger } from '../../../config/logger'; import { Article } from "../../../types"; -import path from "path"; -import {uploadBufferToS3} from "../../../services/s3Service"; +import { uploadBufferToS3 } from "../../../services/s3Service"; const DEFAULT_COVER_IMAGE = '/images/cover-placeholder.webp'; @@ -22,13 +22,27 @@ export async function getArticle(req: Request, res: Response) : Promise { avatarUrl: true, email: true } + }, + gallery: { + orderBy: {order: "asc"}, // Сортировка изображений по порядку + select: { + id: true, + url: true, + caption: true, + alt: true, + width: true, + height: true, + size: true, + format: true, + order: true, // Добавляем порядок сортировки + }, } } }); if (!article) { - logger.warn(`Article not found: ${req.params.id}`); - res.status(404).json({ error: 'Article not found' }); + logger.warn(`Статья не найдена: ${req.params.id}`); + res.status(404).json({ error: 'Статья не найдена' }); return } @@ -184,6 +198,40 @@ export async function activeArticle(req: AuthRequest, res: Response) : Promise { + try { + const { reaction, likes, dislikes } = req.body; + + let newLikes:number = Number(likes); + let newDisLikes:number = Number(dislikes); + + if (reaction === 'like') newLikes++; + if (reaction === 'dislike') newDisLikes++; + + const updatedArticle = await prisma.article.update({ + where: { id: req.params.id }, + data: { + likes: newLikes, + dislikes: newDisLikes + }, + include: { + author: { + select: { + id: true, + displayName: true, + email: true + } + } + } + }); + + res.json(updatedArticle); + } catch (error) { + logger.error('Ошибка установки реакции на статью:', error); + res.status(500).json({ error: 'Серверная ошибка' }); + } +} + export async function deleteArticle(req: AuthRequest, res: Response) : Promise { try { if (!req.user) { diff --git a/src/routes/articles/index.ts b/src/routes/articles/index.ts index 0e02b58..399e70a 100644 --- a/src/routes/articles/index.ts +++ b/src/routes/articles/index.ts @@ -2,7 +2,15 @@ import express from 'express'; import { auth } from '../../middleware/auth'; import { searchArticles } from './controllers/search'; import { listArticles } from './controllers/list'; -import { getArticle, createArticle, updateArticle, deleteArticle, importArticles, activeArticle } from './controllers/crud'; +import { + getArticle, + createArticle, + updateArticle, + deleteArticle, + importArticles, + activeArticle, + reactArticle +} from './controllers/crud'; const router = express.Router(); @@ -20,6 +28,7 @@ router.get('/:id', getArticle); router.post('/', auth, createArticle); router.put('/:id', auth, updateArticle); router.put('/active/:id', auth, activeArticle); +router.put('/react/:id', reactArticle); router.delete('/:id', auth, deleteArticle); export default router; \ No newline at end of file diff --git a/src/routes/other/controllers/other.ts b/src/routes/other/controllers/other.ts new file mode 100644 index 0000000..fc3a940 --- /dev/null +++ b/src/routes/other/controllers/other.ts @@ -0,0 +1,14 @@ +import { Response } from 'express'; +import { userService } from '../../../services/userService'; +import { AuthRequest } from "../../../middleware/auth"; + + +// Изменить количество лайков +export async function updateLikes(req: AuthRequest, res: Response): Promise { + try { + const authors = await userService.getAuthors(); + res.json(authors); + } catch { + res.status(500).json({ error: 'Серверная ошибка' }); + } +} diff --git a/src/routes/other/index.ts b/src/routes/other/index.ts new file mode 100644 index 0000000..0a2f589 --- /dev/null +++ b/src/routes/other/index.ts @@ -0,0 +1,9 @@ +import express from 'express'; +import { updateLikes } from "./controllers/other"; + +const router = express.Router(); + + +router.put('/likes', updateLikes); + +export default router; \ No newline at end of file diff --git a/src/server.ts b/src/server.ts index beeca55..588d5a3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -11,6 +11,7 @@ import articleRoutes from './routes/articles/index'; import galleryRoutes from './routes/gallery/index'; import imagesRoutes from './routes/images/index'; import authorRoutes from './routes/authors/index'; +import otherRoutes from './routes/other/index'; const app = express(); @@ -38,6 +39,7 @@ app.use('/api/authors', authorRoutes); app.use('/api/articles', articleRoutes); app.use('/api/gallery', galleryRoutes); app.use('/api/images', imagesRoutes); +app.use('/api/other', otherRoutes); // Запуск сервера app.listen(PORT, () => { diff --git a/src/services/otherService.ts b/src/services/otherService.ts new file mode 100644 index 0000000..e69de29