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