import { type Article, type InsertArticle, articles } from "@shared/schema"; import { db } from "./db"; import { eq, desc, sql } from "drizzle-orm"; export interface IStorage { getArticles(): Promise; getArticleBySlug(slug: string): Promise
; getArticleById(id: number): Promise
; getFeaturedArticles(): Promise; getPopularArticles(limit: number): Promise; getArticlesByCategory(category: string): Promise; createArticle(article: InsertArticle): Promise
; updateArticle(id: number, article: Partial): Promise
; incrementViews(id: number): Promise; deleteArticle(id: number): Promise; } export class DatabaseStorage implements IStorage { async getArticles(): Promise { return db.select().from(articles).orderBy(desc(articles.publishedAt)); } async getArticleBySlug(slug: string): Promise
{ const [article] = await db.select().from(articles).where(eq(articles.slug, slug)); return article; } async getArticleById(id: number): Promise
{ const [article] = await db.select().from(articles).where(eq(articles.id, id)); return article; } async getFeaturedArticles(): Promise { return db.select().from(articles).where(eq(articles.featured, true)).orderBy(desc(articles.publishedAt)).limit(3); } async getPopularArticles(limit: number): Promise { return db.select().from(articles).orderBy(desc(articles.views)).limit(limit); } async getArticlesByCategory(category: string): Promise { return db.select().from(articles).where(eq(articles.category, category)).orderBy(desc(articles.publishedAt)); } async createArticle(article: InsertArticle): Promise
{ const [created] = await db.insert(articles).values(article).returning(); return created; } async updateArticle(id: number, article: Partial): Promise
{ const [updated] = await db.update(articles).set(article).where(eq(articles.id, id)).returning(); return updated; } async incrementViews(id: number): Promise { await db.update(articles).set({ views: sql`${articles.views} + 1` }).where(eq(articles.id, id)); } async deleteArticle(id: number): Promise { await db.delete(articles).where(eq(articles.id, id)); } } export const storage = new DatabaseStorage();