From dcfc4acae47239ee2321951132069f7ca7ce7cf4 Mon Sep 17 00:00:00 2001 From: parsaaghayi Date: Fri, 12 Jun 2026 23:14:21 +0330 Subject: [PATCH] fix: restore wiki to dictionary, create dynamic blog pages and backend wiki CRUD --- backend/prisma/seed-wiki.ts | 80 ++++++++++++ backend/src/app.module.ts | 2 + backend/src/wiki/dto/create-wiki.dto.ts | 1 + backend/src/wiki/dto/update-wiki.dto.ts | 4 + backend/src/wiki/entities/wiki.entity.ts | 1 + backend/src/wiki/wiki.controller.ts | 17 +++ backend/src/wiki/wiki.module.ts | 9 ++ backend/src/wiki/wiki.service.ts | 21 +++ frontend/application/app/blog/[slug]/page.tsx | 123 ++++++++++++++++++ frontend/application/app/blog/page.tsx | 26 +++- frontend/application/app/wiki/page.tsx | 59 +-------- frontend/application/components/BlogPage.tsx | 76 ++++------- .../application/components/IngredientWiki.tsx | 28 ++-- 13 files changed, 328 insertions(+), 119 deletions(-) create mode 100644 backend/prisma/seed-wiki.ts create mode 100644 backend/src/wiki/dto/create-wiki.dto.ts create mode 100644 backend/src/wiki/dto/update-wiki.dto.ts create mode 100644 backend/src/wiki/entities/wiki.entity.ts create mode 100644 backend/src/wiki/wiki.controller.ts create mode 100644 backend/src/wiki/wiki.module.ts create mode 100644 backend/src/wiki/wiki.service.ts create mode 100644 frontend/application/app/blog/[slug]/page.tsx diff --git a/backend/prisma/seed-wiki.ts b/backend/prisma/seed-wiki.ts new file mode 100644 index 00000000..54e3a9a3 --- /dev/null +++ b/backend/prisma/seed-wiki.ts @@ -0,0 +1,80 @@ +import { PrismaClient } from '@prisma/client'; + +const prisma = new PrismaClient(); + +const WIKI_TERMS = [ + { + id: "green-mussel", + name: "صدف لب‌سبز (Perna Canaliculus)", + description: "این صدف بومی سواحل بکر نیوزیلند است و تنها منبع طبیعی حاوی گلیکوزآمین‌گلیکان‌ها (GAG) در غلظت بسیار بالا محسوب می‌شود.", + benefits: [ + "بازسازی بافت غضروفی تخریب شده", + "خواص ضدالتهابی طبیعی برای رباط‌ها", + "تامین اسیدهای چرب امگا ۳ خاص", + ] + }, + { + id: "silver", + name: "نقره میکروسیلور (Microsilver)", + description: "ذرات نقره خالص با ساختار اسفنجی که به دلیل اندازه ذرات درشت، جذب خون نمی‌شوند اما اثر آنتی‌باکتریال پایداری بر سطح غشاها دارند.", + benefits: [ + "مبارزه با باکتری‌های مقاوم در دهان و پوست", + "عدم آسیب به فلور طبیعی بدن", + "جایگزین ایمن برای آنتی‌بیوتیک‌های موضعی", + ] + }, + { + id: "colostrum", + name: "آغوز (Colostrum)", + description: "اولین شیر مادر که حاوی دوز بالایی از ایمونوگلوبولین‌ها، ویتامین‌ها و مواد معدنی برای فعال‌سازی فوری سیستم ایمنی است.", + benefits: [ + "تقویت سد دفاعی روده", + "انتقال مستقیم آنتی‌بادی به نوزادان", + "تسریع نقاهت بعد از جرافی", + ] + }, + { + id: "peat-extract", + name: "عصاره پیت (Peat Extract)", + description: "ماده‌ای کاملا طبیعی حاصل از تجزیه گیاهان در طول هزاران سال که غنی از اسیدهای هومیک و مواد معدنی است.", + benefits: [ + "جذب سموم در دستگاه گوارش", + "تنظیم فلور روده", + "بهبود اشتها و هضم" + ] + } +]; + +async function main() { + console.log('Seeding Scientific Terms (Wiki)...'); + + for (const term of WIKI_TERMS) { + await prisma.scientificTerm.upsert({ + where: { key: term.id }, + update: { + term: term.name, + definition: term.description, + keywords: JSON.stringify(term.benefits), + wikiId: term.id, + }, + create: { + key: term.id, + term: term.name, + definition: term.description, + keywords: JSON.stringify(term.benefits), + wikiId: term.id, + }, + }); + } + + console.log('Wiki seeded successfully'); +} + +main() + .catch((e) => { + console.error(e); + process.exit(1); + }) + .finally(async () => { + await prisma.$disconnect(); + }); diff --git a/backend/src/app.module.ts b/backend/src/app.module.ts index 40b36ac3..fed0a797 100644 --- a/backend/src/app.module.ts +++ b/backend/src/app.module.ts @@ -13,6 +13,7 @@ import { MetricsController } from './common/metrics.controller'; import { AdminModule } from './admin/admin.module'; import { HomeModule } from './home/home.module'; import { BlogsModule } from './blogs/blogs.module'; +import { WikiModule } from './wiki/wiki.module'; @Module({ imports: [ @@ -31,6 +32,7 @@ import { BlogsModule } from './blogs/blogs.module'; AdminModule, HomeModule, BlogsModule, + WikiModule, ], controllers: [MetricsController], providers: [ diff --git a/backend/src/wiki/dto/create-wiki.dto.ts b/backend/src/wiki/dto/create-wiki.dto.ts new file mode 100644 index 00000000..b3182334 --- /dev/null +++ b/backend/src/wiki/dto/create-wiki.dto.ts @@ -0,0 +1 @@ +export class CreateWikiDto {} diff --git a/backend/src/wiki/dto/update-wiki.dto.ts b/backend/src/wiki/dto/update-wiki.dto.ts new file mode 100644 index 00000000..c86c1a6d --- /dev/null +++ b/backend/src/wiki/dto/update-wiki.dto.ts @@ -0,0 +1,4 @@ +import { PartialType } from '@nestjs/swagger'; +import { CreateWikiDto } from './create-wiki.dto'; + +export class UpdateWikiDto extends PartialType(CreateWikiDto) {} diff --git a/backend/src/wiki/entities/wiki.entity.ts b/backend/src/wiki/entities/wiki.entity.ts new file mode 100644 index 00000000..7c26d717 --- /dev/null +++ b/backend/src/wiki/entities/wiki.entity.ts @@ -0,0 +1 @@ +export class Wiki {} diff --git a/backend/src/wiki/wiki.controller.ts b/backend/src/wiki/wiki.controller.ts new file mode 100644 index 00000000..2614cc9b --- /dev/null +++ b/backend/src/wiki/wiki.controller.ts @@ -0,0 +1,17 @@ +import { Controller, Get, Param } from '@nestjs/common'; +import { WikiService } from './wiki.service'; + +@Controller('wiki') +export class WikiController { + constructor(private readonly wikiService: WikiService) {} + + @Get() + findAll() { + return this.wikiService.findAll(); + } + + @Get(':key') + findOne(@Param('key') key: string) { + return this.wikiService.findOneByKey(key); + } +} diff --git a/backend/src/wiki/wiki.module.ts b/backend/src/wiki/wiki.module.ts new file mode 100644 index 00000000..52d63436 --- /dev/null +++ b/backend/src/wiki/wiki.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { WikiService } from './wiki.service'; +import { WikiController } from './wiki.controller'; + +@Module({ + controllers: [WikiController], + providers: [WikiService], +}) +export class WikiModule {} diff --git a/backend/src/wiki/wiki.service.ts b/backend/src/wiki/wiki.service.ts new file mode 100644 index 00000000..3da46029 --- /dev/null +++ b/backend/src/wiki/wiki.service.ts @@ -0,0 +1,21 @@ +import { Injectable, NotFoundException } from '@nestjs/common'; +import { PrismaService } from '../prisma/prisma.service'; + +@Injectable() +export class WikiService { + constructor(private prisma: PrismaService) {} + + async findAll() { + return this.prisma.scientificTerm.findMany({ + orderBy: { term: 'asc' } + }); + } + + async findOneByKey(key: string) { + const term = await this.prisma.scientificTerm.findUnique({ + where: { key } + }); + if (!term) throw new NotFoundException('Wiki term not found'); + return term; + } +} diff --git a/frontend/application/app/blog/[slug]/page.tsx b/frontend/application/app/blog/[slug]/page.tsx new file mode 100644 index 00000000..eea26d14 --- /dev/null +++ b/frontend/application/app/blog/[slug]/page.tsx @@ -0,0 +1,123 @@ +import React from "react"; +import { ChevronRight, Calendar, User, Sparkles } from "lucide-react"; +import Link from 'next/link'; +import type { Metadata } from 'next'; + +async function getBlog(slug: string) { + try { + const res = await fetch(`http://localhost:4000/api/blogs/${slug}`, { next: { revalidate: 60 } }); + if (!res.ok) throw new Error('Failed to fetch blog'); + const b = await res.json(); + return { + id: b.id, + slug: b.slug, + title: b.title, + excerpt: b.metaDescription || b.content.substring(0, 150).replace(/<[^>]+>/g, '') + '...', + image: b.imageUrl, + date: new Date(b.createdAt).toLocaleDateString('fa-IR'), + author: b.author ? `${b.author.firstName} ${b.author.lastName}` : 'نویسنده کانینا', + content: b.content + }; + } catch (error) { + console.error(error); + return null; + } +} + +export async function generateMetadata({ params }: { params: { slug: string } }): Promise { + const blog = await getBlog(params.slug); + if (!blog) return { title: 'مقاله یافت نشد' }; + + return { + title: blog.title, + description: blog.excerpt, + }; +} + +export default async function BlogPostPage({ params }: { params: { slug: string } }) { + const selectedPost = await getBlog(params.slug); + + if (!selectedPost) { + return ( +
+

مقاله‌ای یافت نشد

+ + + بازگشت به مجله سلامت + +
+ ); + } + + return ( +
+
+ {/* Breadcrumbs */} +
+ خانه + + مجله سلامت + + {selectedPost.title} +
+ +
+
+
+
+ {selectedPost.title} +
+
+
+
تحقیق بالینی
+
+ + {selectedPost.date} +
+
+

+ {selectedPost.title} +

+
+

+ {selectedPost.excerpt} +

+
+
+ +
+
+
+ +
+
+
{selectedPost.author}
+
نویسنده کانینا
+
+
+
+ +
+
+
+
+
+ +
+
+
+ +
+

ارتباط با کارشناسان

+
+ برای دریافت مشاوره تخصصی و انتخاب بهترین مکمل برای حیوان خانگی خود، با ما در تماس باشید. +
+
+
+
+
+
+ ); +} diff --git a/frontend/application/app/blog/page.tsx b/frontend/application/app/blog/page.tsx index d6ad9b68..bf463518 100644 --- a/frontend/application/app/blog/page.tsx +++ b/frontend/application/app/blog/page.tsx @@ -6,6 +6,28 @@ export const metadata: Metadata = { description: 'آخرین مقالات، آموزش‌ها و اخبار علمی در زمینه نگهداری، تغذیه و سلامت سگ و گربه.', }; -export default function Blog() { - return ; +async function getBlogs() { + try { + const res = await fetch('http://localhost:4000/api/blogs', { next: { revalidate: 60 } }); + if (!res.ok) throw new Error('Failed to fetch blogs'); + const data = await res.json(); + return data.map((b: any) => ({ + id: b.id, + slug: b.slug, + title: b.title, + excerpt: b.metaDescription || b.content.substring(0, 150).replace(/<[^>]+>/g, '') + '...', + image: b.imageUrl, + date: new Date(b.createdAt).toLocaleDateString('fa-IR'), + author: b.author ? `${b.author.firstName} ${b.author.lastName}` : 'نویسنده کانینا', + content: b.content + })); + } catch (error) { + console.error(error); + return []; + } +} + +export default async function Blog() { + const blogs = await getBlogs(); + return ; } diff --git a/frontend/application/app/wiki/page.tsx b/frontend/application/app/wiki/page.tsx index bd553fd2..350aa8bb 100644 --- a/frontend/application/app/wiki/page.tsx +++ b/frontend/application/app/wiki/page.tsx @@ -1,60 +1,11 @@ -import Link from 'next/link'; +import IngredientWiki from "../../components/IngredientWiki"; import type { Metadata } from 'next'; export const metadata: Metadata = { - title: 'دانشنامه و مقالات علمی', - description: 'مقالات تخصصی و آموزشی درباره تغذیه و سلامت حیوانات خانگی', + title: 'دانشنامه ترکیبات دارویی', + description: 'اطلاعات کامل در مورد مواد موثره، ویتامین‌ها و ترکیبات گیاهی به کار رفته در محصولات تخصصی کانینا ایران.', }; -async function getBlogs() { - try { - const res = await fetch('http://localhost:4000/api/blogs', { next: { revalidate: 60 } }); - if (!res.ok) throw new Error('Failed to fetch blogs'); - return res.json(); - } catch (error) { - console.error(error); - return []; - } -} - -export default async function WikiPage() { - const blogs = await getBlogs(); - - return ( -
-
-

دانشنامه و مقالات علمی

- -
- {blogs.map((blog: any) => ( - -
- {blog.imageUrl && ( -
- {blog.title} -
- )} -
-

- {blog.title} -

-
-
- {new Date(blog.createdAt).toLocaleDateString('fa-IR')} - مطالعه مقاله ← -
-
-
- - ))} - - {blogs.length === 0 && ( -
- مقاله‌ای یافت نشد. -
- )} -
-
-
- ); +export default function WikiPage() { + return ; } diff --git a/frontend/application/components/BlogPage.tsx b/frontend/application/components/BlogPage.tsx index 976067f3..9c3de60b 100644 --- a/frontend/application/components/BlogPage.tsx +++ b/frontend/application/components/BlogPage.tsx @@ -5,48 +5,22 @@ import { ChevronRight, Calendar, User, ArrowLeft, Sparkles, BookOpen } from "luc import { Product } from "../lib/data/products"; import { productService } from "../lib/services/productService"; -const BLOG_POSTS = [ - { - id: "p1", - title: "مزایای شگفت‌انگیز صدف لب‌سبز نیوزیلند برای مفاصل پت", - excerpt: "صدف لب‌سبز (Perna Canaliculus) تنها منبع طبیعی حاوی گلیکوزآمین‌گلیکان‌ها (GAG) در غلظت بسیار بالا محسوب می‌شود.", - image: "https://images.unsplash.com/photo-1599443015574-be5fe8a05783?auto=format&fit=crop&q=80&w=800", - date: "۲۵ اردیبهشت ۱۴۰۳", - author: "دکتر کلاوس هنینگ", - content: ` - صدف لب‌سبز نیوزیلندی قلب تپنده محصولات ارتوپدی کانینا است. این صدف که منحصراً در آب‌های فوق‌العاده تمیز نیوزیلند تکثیر می‌شود، حاوی ترکیبات ضدالتهابی طبیعی است که در هیچ منبع دیگری به این غلظت یافت نمی‌شود. - - چرا برای سگ‌های نژاد بزرگ حیاتی است؟ - در سگ‌های نژاد بزرگ، سرعت رشد اسکلتی بسیار بالاست. اگر مفاصل و تاندون‌ها با این سرعت هماهنگ نشوند، بدشکلی‌های ماندگار ایجاد می‌شود. GAG موجود در صدف، مانند چسب بیولوژیک عمل کرده و بافت غضروفی را ترمیم می‌کند. - - مکمل Canhydrox GAG با دارا بودن ۱۵٪ پودر صدف لب‌سبز خالص، استاندارد طلایی در دامپزشکی آلمان است. - ` - }, - { - id: "p2", - title: "چگونه سیستم ایمنی پت خود را در برابر بیماری‌های فصلی تقویت کنیم؟", - excerpt: "آغوز (Colostrum) اولین سد دفاعی طبیعت است. با ایمیون‌بوستر کانی‌نا، پت شما در برابر ویروس‌ها بیمه می‌شود.", - image: "https://images.unsplash.com/photo-1576091160550-217359f42f8c?auto=format&fit=crop&q=80&w=800", - date: "۲۰ اردیبهشت ۱۴۰۳", - author: "دکتر النا اشمیت", - content: "ایمنی بدن یک مکانیزم پیچیده است. استفاده از Immun-Booster به ویژه در دوران واکسیناسیون یا جابجایی، به بدن پت کمک می‌کند تا آنتی‌بادی‌های لازم را با سرعت بیشتری تولید کند." - }, - { - id: "p3", - title: "اهمیت اسیدهای چرب امگا ۳ در درخشش پوشش مویی", - excerpt: "ریزش موی شدید در سگ و گربه معمولاً نشانه کمبود اسیدهای چرب ضروری EPA و DHA است. روغن سالمون پرس سرد راه حل نهایی است.", - image: "https://images.unsplash.com/photo-1516734212186-a967f81ad0d7?auto=format&fit=crop&q=80&w=800", - date: "۱۵ اردیبهشت ۱۴۰۳", - author: "دکتر ماری کونیگ", - content: "روغن سالمون کانی‌نا به روش پرس سرد تهیه شده تا تمامی ویتامین‌های حساس به حرارت حفظ شوند. این روغن نه تنها پوست را مرطوب نگه می‌دارد، بلکه التهابات مفصلی را نیز کاهش می‌دهد." - } -]; +export interface BlogPost { + id: string; + slug: string; + title: string; + excerpt: string; + image: string; + date: string; + author: string; + content: string; +} import { useRouter } from 'next/navigation'; -export default function BlogPage() { +export default function BlogPage({ blogs = [] }: { blogs: BlogPost[] }) { const router = useRouter(); - const [selectedPost, setSelectedPost] = React.useState(null); + const [selectedPost, setSelectedPost] = React.useState(null); const [products, setProducts] = React.useState([]); React.useEffect(() => { @@ -144,10 +118,10 @@ export default function BlogPage() {

محبوب‌ترین مقالات

- {BLOG_POSTS.map(p => ( + {blogs.map(p => (
setSelectedPost(p)} + onClick={() => router.push(`/blog/${p.slug}`)} className="flex items-center gap-4 cursor-pointer group" >
@@ -194,17 +168,18 @@ export default function BlogPage() {
{/* Featured Post */} + {blogs.length > 0 && (
setSelectedPost(BLOG_POSTS[0])} + onClick={() => router.push(`/blog/${blogs[0].slug}`)} >
{BLOG_POSTS[0].title}
@@ -213,14 +188,14 @@ export default function BlogPage() {
تخصصی
- {BLOG_POSTS[0].date} + {blogs[0].date}

- {BLOG_POSTS[0].title} + {blogs[0].title}

- {BLOG_POSTS[0].excerpt} + {blogs[0].excerpt}

@@ -228,8 +203,8 @@ export default function BlogPage() {
-
{BLOG_POSTS[0].author}
-
محقق ارشد کانی‌نا
+
{blogs[0].author}
+
نویسنده کانینا
@@ -240,10 +215,11 @@ export default function BlogPage() {
+ )} {/* Post Grid */}
- {BLOG_POSTS.slice(1).map((post, idx) => ( + {blogs.slice(1).map((post, idx) => ( setSelectedPost(post)} + onClick={() => router.push(`/blog/${post.slug}`)} >
state.texts); + const [ingredientsWiki, setIngredientsWiki] = useState([]); const [products, setProducts] = useState([]); useEffect(() => { productService.getProducts().then(data => setProducts(data)); + + fetch('http://localhost:4000/api/wiki') + .then(res => res.json()) + .then(data => { + const mapped = data.map((item: any) => ({ + id: item.key, + name: item.term, + description: item.definition, + benefits: item.keywords ? JSON.parse(item.keywords) : [] + })); + setIngredientsWiki(mapped); + }) + .catch(err => console.error("Failed to fetch wiki:", err)); }, []); - const ingredientsWiki = useMemo(() => { - const jsonStr = texts['ingredients_wiki']; - if (jsonStr) { - try { - return JSON.parse(jsonStr) as typeof INGREDIENTS_WIKI; - } catch (e) { - console.error("Failed to parse ingredients_wiki from DB setting:", e); - } - } - return INGREDIENTS_WIKI; - }, [texts]); - return (