Версия 1.0.7 - Добавлена таблица и api для анонсов в шапке сайта.
This commit is contained in:
parent
3e1ebf2b11
commit
ef1ca0b25b
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "russcult_server",
|
"name": "russcult_server",
|
||||||
"version": "1.0.6",
|
"version": "1.0.7",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "tsc",
|
"build": "tsc",
|
||||||
|
|||||||
@ -0,0 +1,10 @@
|
|||||||
|
-- CreateTable
|
||||||
|
CREATE TABLE "Announcement" (
|
||||||
|
"id" TEXT NOT NULL,
|
||||||
|
"message" TEXT NOT NULL,
|
||||||
|
"isActive" BOOLEAN NOT NULL DEFAULT true,
|
||||||
|
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
"updatedAt" TIMESTAMP(3) NOT NULL,
|
||||||
|
|
||||||
|
CONSTRAINT "Announcement_pkey" PRIMARY KEY ("id")
|
||||||
|
);
|
||||||
@ -1,3 +1,3 @@
|
|||||||
# Please do not edit this file manually
|
# Please do not edit this file manually
|
||||||
# It should be added in your version-control system (e.g., Git)
|
# It should be added in your version-control system (e.g., Git)
|
||||||
provider = "postgresql"
|
provider = "postgresql"
|
||||||
|
|||||||
@ -102,3 +102,11 @@ model UserReaction {
|
|||||||
|
|
||||||
@@unique([userId, articleId])
|
@@unique([userId, articleId])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
model Announcement {
|
||||||
|
id String @id @default(uuid())
|
||||||
|
message String
|
||||||
|
isActive Boolean @default(true)
|
||||||
|
createdAt DateTime @default(now())
|
||||||
|
updatedAt DateTime @updatedAt
|
||||||
|
}
|
||||||
|
|||||||
88
src/routes/announcements/controllers/announcements.ts
Normal file
88
src/routes/announcements/controllers/announcements.ts
Normal file
@ -0,0 +1,88 @@
|
|||||||
|
import { Request, Response } from 'express';
|
||||||
|
import { prisma } from '../../../lib/prisma';
|
||||||
|
|
||||||
|
export async function getActiveAnnouncement(req: Request, res: Response) {
|
||||||
|
try {
|
||||||
|
const announcement = await prisma.announcement.findFirst({
|
||||||
|
where: { isActive: true },
|
||||||
|
orderBy: { createdAt: 'desc' },
|
||||||
|
select: {
|
||||||
|
id: true,
|
||||||
|
message: true
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!announcement) {
|
||||||
|
return res.status(204).send();
|
||||||
|
}
|
||||||
|
|
||||||
|
res.json(announcement);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: 'Server error' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function createAnnouncement(req: Request, res: Response) {
|
||||||
|
try {
|
||||||
|
const { message } = req.body;
|
||||||
|
|
||||||
|
if (!message || typeof message !== 'string' || message.trim().length === 0) {
|
||||||
|
return res.status(400).json({ error: 'Message is required' });
|
||||||
|
}
|
||||||
|
|
||||||
|
const announcement = await prisma.announcement.create({
|
||||||
|
data: {
|
||||||
|
message: message.trim()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(201).json(announcement);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: 'Server error' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateAnnouncement(req: Request, res: Response) {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
const { message, isActive } = req.body;
|
||||||
|
|
||||||
|
const announcement = await prisma.announcement.update({
|
||||||
|
where: { id },
|
||||||
|
data: {
|
||||||
|
...(message && { message: message.trim() }),
|
||||||
|
...(typeof isActive === 'boolean' && { isActive })
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json(announcement);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: 'Server error' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteAnnouncement(req: Request, res: Response) {
|
||||||
|
try {
|
||||||
|
const { id } = req.params;
|
||||||
|
|
||||||
|
await prisma.announcement.delete({
|
||||||
|
where: { id }
|
||||||
|
});
|
||||||
|
|
||||||
|
res.status(204).send();
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: 'Server error' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getAllAnnouncements(req: Request, res: Response) {
|
||||||
|
try {
|
||||||
|
const announcements = await prisma.announcement.findMany({
|
||||||
|
orderBy: { createdAt: 'desc' }
|
||||||
|
});
|
||||||
|
|
||||||
|
res.json(announcements);
|
||||||
|
} catch (error) {
|
||||||
|
res.status(500).json({ error: 'Server error' });
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/routes/announcements/index.ts
Normal file
20
src/routes/announcements/index.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { Router } from 'express';
|
||||||
|
import { auth } from '../../middleware/auth';
|
||||||
|
import {
|
||||||
|
getActiveAnnouncement,
|
||||||
|
createAnnouncement,
|
||||||
|
updateAnnouncement,
|
||||||
|
deleteAnnouncement,
|
||||||
|
getAllAnnouncements
|
||||||
|
} from './controllers/announcements';
|
||||||
|
|
||||||
|
const router = Router();
|
||||||
|
|
||||||
|
router.get('/active', getActiveAnnouncement);
|
||||||
|
|
||||||
|
router.get('/', auth, getAllAnnouncements);
|
||||||
|
router.post('/', auth, createAnnouncement);
|
||||||
|
router.put('/:id', auth, updateAnnouncement);
|
||||||
|
router.delete('/:id', auth, deleteAnnouncement);
|
||||||
|
|
||||||
|
export default router;
|
||||||
@ -12,6 +12,7 @@ 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';
|
import otherRoutes from './routes/other/index';
|
||||||
|
import announcementRoutes from './routes/announcements/index';
|
||||||
|
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
@ -40,6 +41,7 @@ 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.use('/api/other', otherRoutes);
|
||||||
|
app.use('/api/announcements', announcementRoutes);
|
||||||
|
|
||||||
// Запуск сервера
|
// Запуск сервера
|
||||||
app.listen(PORT, () => {
|
app.listen(PORT, () => {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user