All files / app/api/categories route.ts

0% Statements 0/34
0% Branches 0/18
0% Functions 0/2
0% Lines 0/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105                                                                                                                                                                                                                 
import { NextRequest, NextResponse } from "next/server";
import { connectDB } from "@/lib/mongodb";
import { requireAuth, requireAdmin } from "@/lib/session";
import Category from "@/models/Category";
 
// GET /api/categories - List all categories
export async function GET() {
  try {
    await requireAuth();
    await connectDB();
 
    const categories = await Category.find().sort({ order: 1, name: 1 });
 
    return NextResponse.json({ data: categories });
  } catch (error) {
    if (error instanceof Error && error.message === "Unauthorized") {
      return NextResponse.json(
        { error: { code: "UNAUTHORIZED", message: "Authentication required" } },
        { status: 401 }
      );
    }
    console.error("Error fetching categories:", error);
    return NextResponse.json(
      {
        error: {
          code: "INTERNAL_ERROR",
          message: "Failed to fetch categories",
        },
      },
      { status: 500 }
    );
  }
}
 
// POST /api/categories - Create a new category (admin only)
export async function POST(request: NextRequest) {
  try {
    await requireAdmin();
    await connectDB();
 
    const body = await request.json();
    const { name, slug, description, order } = body;
 
    if (!name || !slug) {
      return NextResponse.json(
        {
          error: {
            code: "VALIDATION_ERROR",
            message: "Name and slug are required",
          },
        },
        { status: 400 }
      );
    }
 
    // Check if slug already exists
    const existing = await Category.findOne({ slug });
    if (existing) {
      return NextResponse.json(
        {
          error: {
            code: "DUPLICATE_SLUG",
            message: "A category with this slug already exists",
          },
        },
        { status: 400 }
      );
    }
 
    const category = await Category.create({
      name,
      slug,
      description,
      order: order || 0,
    });
 
    return NextResponse.json({ data: category }, { status: 201 });
  } catch (error) {
    if (error instanceof Error) {
      if (error.message === "Unauthorized") {
        return NextResponse.json(
          { error: { code: "UNAUTHORIZED", message: "Authentication required" } },
          { status: 401 }
        );
      }
      if (error.message.includes("Admin access required")) {
        return NextResponse.json(
          { error: { code: "FORBIDDEN", message: "Admin access required" } },
          { status: 403 }
        );
      }
    }
    console.error("Error creating category:", error);
    return NextResponse.json(
      {
        error: {
          code: "INTERNAL_ERROR",
          message: "Failed to create category",
        },
      },
      { status: 500 }
    );
  }
}