Доработан код удаления статьи. Изменение в логировании для включения записи в лог.
This commit is contained in:
parent
584b2bc614
commit
3a495766ce
@ -54,6 +54,7 @@ const fileRotateTransport = new winston.transports.DailyRotateFile({
|
||||
zippedArchive: true,
|
||||
maxSize: '20m',
|
||||
maxFiles: '14d',
|
||||
level: 'info', // Устанавливаем уровень логирования для файла
|
||||
format: winston.format.combine(
|
||||
winston.format.uncolorize(), // Убираем цвета для файлового формата
|
||||
logFormat // Используем текстовый формат без JSON
|
||||
|
@ -2,6 +2,7 @@ import { Request, Response } from 'express';
|
||||
import axios from "axios";
|
||||
import path from "path";
|
||||
import { prisma } from '../../../lib/prisma';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { AuthRequest } from '../../../middleware/auth';
|
||||
import { checkPermission } from '../../../utils/permissions';
|
||||
import { logger } from '../../../config/logger';
|
||||
@ -107,7 +108,7 @@ export async function createArticle(req: AuthRequest, res: Response) : Promise<v
|
||||
|
||||
export async function updateArticle(req: AuthRequest, res: Response) : Promise<void> {
|
||||
try {
|
||||
const { title, excerpt, content, categoryId, cityId, coverImage, readTime } = req.body;
|
||||
const { title, excerpt, content, categoryId, cityId, coverImage, readTime, author } = req.body;
|
||||
|
||||
if (!req.user) {
|
||||
res.status(401).json({ error: 'Пользователь не вошел в систему' });
|
||||
@ -115,7 +116,8 @@ export async function updateArticle(req: AuthRequest, res: Response) : Promise<v
|
||||
}
|
||||
|
||||
const article = await prisma.article.findUnique({
|
||||
where: { id: req.params.id }
|
||||
where: { id: req.params.id },
|
||||
select: { authorId: true } // Берём только автора
|
||||
});
|
||||
|
||||
if (!article) {
|
||||
@ -128,17 +130,23 @@ export async function updateArticle(req: AuthRequest, res: Response) : Promise<v
|
||||
return
|
||||
}
|
||||
|
||||
const updateData: Prisma.ArticleUpdateInput = {
|
||||
title,
|
||||
excerpt,
|
||||
content,
|
||||
categoryId: Number(categoryId),
|
||||
cityId,
|
||||
coverImage,
|
||||
readTime
|
||||
};
|
||||
|
||||
if (author?.id && author.id !== article.authorId) {
|
||||
updateData.author = { connect: { id: author.id } };
|
||||
}
|
||||
|
||||
const updatedArticle = await prisma.article.update({
|
||||
where: { id: req.params.id },
|
||||
data: {
|
||||
title,
|
||||
excerpt,
|
||||
content,
|
||||
categoryId: Number(categoryId),
|
||||
cityId,
|
||||
coverImage,
|
||||
readTime
|
||||
},
|
||||
data: updateData,
|
||||
include: {
|
||||
author: {
|
||||
select: {
|
||||
@ -178,7 +186,7 @@ export async function activeArticle(req: AuthRequest, res: Response) : Promise<v
|
||||
const updatedArticle = await prisma.article.update({
|
||||
where: { id: req.params.id },
|
||||
data: {
|
||||
isActive: !isActive
|
||||
isActive: isActive
|
||||
},
|
||||
include: {
|
||||
author: {
|
||||
@ -253,9 +261,18 @@ export async function deleteArticle(req: AuthRequest, res: Response) : Promise<v
|
||||
return
|
||||
}
|
||||
|
||||
await prisma.article.delete({
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
// Транзакция: удаляем зависимые данные перед удалением статьи - галерея и реакции
|
||||
await prisma.$transaction([
|
||||
prisma.galleryImage.deleteMany({
|
||||
where: { articleId: req.params.id },
|
||||
}),
|
||||
prisma.userReaction.deleteMany({
|
||||
where: { articleId: req.params.id },
|
||||
}),
|
||||
prisma.article.delete({
|
||||
where: { id: req.params.id },
|
||||
}),
|
||||
]);
|
||||
|
||||
res.json({ message: 'Статья успешно удалена' });
|
||||
} catch (error) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user