import type { Express } from "express"; import { createServer, type Server } from "http"; import { storage } from "./storage"; import { z } from "zod"; import { updateVideoSchema } from "@shared/schema"; export async function registerRoutes(app: Express): Promise { // Get videos with pagination and filtering app.get("/api/videos", async (req, res) => { try { const limit = parseInt(req.query.limit as string) || 20; const offset = parseInt(req.query.offset as string) || 0; const search = req.query.search as string; const videos = await storage.getVideos(limit, offset, search); const total = await storage.getVideoCount(search); res.json({ videos, total, hasMore: offset + limit < total }); } catch (error) { res.status(500).json({ message: "Failed to fetch videos" }); } }); // Get single video by ID app.get("/api/videos/:id", async (req, res) => { try { const video = await storage.getVideo(req.params.id); if (!video) { return res.status(404).json({ message: "Video not found" }); } res.json(video); } catch (error) { res.status(500).json({ message: "Failed to fetch video" }); } }); // Update video views app.post("/api/videos/:id/view", async (req, res) => { try { await storage.updateVideoViews(req.params.id); res.json({ success: true }); } catch (error) { res.status(500).json({ message: "Failed to update views" }); } }); // Update video metadata (title, description, etc.) app.patch("/api/videos/:id", async (req, res) => { try { const updates = updateVideoSchema.parse(req.body); const updatedVideo = await storage.updateVideo(req.params.id, updates); if (!updatedVideo) { return res.status(404).json({ message: "Video not found" }); } res.json(updatedVideo); } catch (error) { if (error instanceof z.ZodError) { return res.status(400).json({ message: "Invalid request data", errors: error.errors }); } res.status(500).json({ message: "Failed to update video" }); } }); const httpServer = createServer(app); return httpServer; }