Реакция пользователей (like/dislike)
This commit is contained in:
parent
9aef927eeb
commit
809cbc6c32
@ -1,12 +1,12 @@
|
|||||||
import { Request, Response } from 'express';
|
import { Request, Response } from 'express';
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import path from "path";
|
||||||
import { prisma } from '../../../lib/prisma';
|
import { prisma } from '../../../lib/prisma';
|
||||||
import { AuthRequest } from '../../../middleware/auth';
|
import { AuthRequest } from '../../../middleware/auth';
|
||||||
import { checkPermission } from '../../../utils/permissions';
|
import { checkPermission } from '../../../utils/permissions';
|
||||||
import { logger } from '../../../config/logger';
|
import { logger } from '../../../config/logger';
|
||||||
import { Article } from "../../../types";
|
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';
|
const DEFAULT_COVER_IMAGE = '/images/cover-placeholder.webp';
|
||||||
|
|
||||||
@ -22,13 +22,27 @@ export async function getArticle(req: Request, res: Response) : Promise<void> {
|
|||||||
avatarUrl: true,
|
avatarUrl: true,
|
||||||
email: 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) {
|
if (!article) {
|
||||||
logger.warn(`Article not found: ${req.params.id}`);
|
logger.warn(`Статья не найдена: ${req.params.id}`);
|
||||||
res.status(404).json({ error: 'Article not found' });
|
res.status(404).json({ error: 'Статья не найдена' });
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -184,6 +198,40 @@ export async function activeArticle(req: AuthRequest, res: Response) : Promise<v
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function reactArticle(req: AuthRequest, res: Response) : Promise<void> {
|
||||||
|
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<void> {
|
export async function deleteArticle(req: AuthRequest, res: Response) : Promise<void> {
|
||||||
try {
|
try {
|
||||||
if (!req.user) {
|
if (!req.user) {
|
||||||
|
@ -2,7 +2,15 @@ import express from 'express';
|
|||||||
import { auth } from '../../middleware/auth';
|
import { auth } from '../../middleware/auth';
|
||||||
import { searchArticles } from './controllers/search';
|
import { searchArticles } from './controllers/search';
|
||||||
import { listArticles } from './controllers/list';
|
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();
|
const router = express.Router();
|
||||||
|
|
||||||
@ -20,6 +28,7 @@ router.get('/:id', getArticle);
|
|||||||
router.post('/', auth, createArticle);
|
router.post('/', auth, createArticle);
|
||||||
router.put('/:id', auth, updateArticle);
|
router.put('/:id', auth, updateArticle);
|
||||||
router.put('/active/:id', auth, activeArticle);
|
router.put('/active/:id', auth, activeArticle);
|
||||||
|
router.put('/react/:id', reactArticle);
|
||||||
router.delete('/:id', auth, deleteArticle);
|
router.delete('/:id', auth, deleteArticle);
|
||||||
|
|
||||||
export default router;
|
export default router;
|
14
src/routes/other/controllers/other.ts
Normal file
14
src/routes/other/controllers/other.ts
Normal file
@ -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<void> {
|
||||||
|
try {
|
||||||
|
const authors = await userService.getAuthors();
|
||||||
|
res.json(authors);
|
||||||
|
} catch {
|
||||||
|
res.status(500).json({ error: 'Серверная ошибка' });
|
||||||
|
}
|
||||||
|
}
|
9
src/routes/other/index.ts
Normal file
9
src/routes/other/index.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import express from 'express';
|
||||||
|
import { updateLikes } from "./controllers/other";
|
||||||
|
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
|
||||||
|
router.put('/likes', updateLikes);
|
||||||
|
|
||||||
|
export default router;
|
@ -11,6 +11,7 @@ import articleRoutes from './routes/articles/index';
|
|||||||
import galleryRoutes from './routes/gallery/index';
|
import galleryRoutes from './routes/gallery/index';
|
||||||
import imagesRoutes from './routes/images/index';
|
import imagesRoutes from './routes/images/index';
|
||||||
import authorRoutes from './routes/authors/index';
|
import authorRoutes from './routes/authors/index';
|
||||||
|
import otherRoutes from './routes/other/index';
|
||||||
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -38,6 +39,7 @@ app.use('/api/authors', authorRoutes);
|
|||||||
app.use('/api/articles', articleRoutes);
|
app.use('/api/articles', articleRoutes);
|
||||||
app.use('/api/gallery', galleryRoutes);
|
app.use('/api/gallery', galleryRoutes);
|
||||||
app.use('/api/images', imagesRoutes);
|
app.use('/api/images', imagesRoutes);
|
||||||
|
app.use('/api/other', otherRoutes);
|
||||||
|
|
||||||
// Запуск сервера
|
// Запуск сервера
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
|
0
src/services/otherService.ts
Normal file
0
src/services/otherService.ts
Normal file
Loading…
x
Reference in New Issue
Block a user