11import { NextResponse , NextRequest } from "next/server" ;
22import { db } from "@/lib/db" ;
33import { categories , categoryOptionValues , hostelOptions } from "@/lib/schema" ;
4- import { sql , eq } from "drizzle-orm" ;
4+ import { eq } from "drizzle-orm" ;
5+ import { requireAdmin } from "@/lib/auth/server" ;
6+ import { CreateCategorySchema , UpdateCategorySchema } from "@/lib/validation" ;
57
6- export async function GET ( req : NextRequest ) {
8+ export async function GET ( ) {
79 try {
810 const allCategories = await db . select ( ) . from ( categories ) ;
911
@@ -23,99 +25,14 @@ export async function GET(req: NextRequest) {
2325 return NextResponse . json ( categoriesWithOptions ) ;
2426 } catch ( error ) {
2527 console . error ( 'Error fetching categories:' , error ) ;
26-
27- // Return sample categories for testing when database is not connected
28- if ( error instanceof Error && error . message . includes ( 'DATABASE_URL' ) ) {
29- console . log ( 'Database not connected, returning sample categories for testing' ) ;
30- return NextResponse . json ( [
31- {
32- categoryId : 1 ,
33- categoryName : "Amenities" ,
34- options : [
35- { optionId : 1 , optionName : "Free WiFi" } ,
36- { optionId : 2 , optionName : "Kitchen" } ,
37- { optionId : 3 , optionName : "Laundry" } ,
38- { optionId : 4 , optionName : "Common Room" } ,
39- { optionId : 5 , optionName : "Garden/Terrace" } ,
40- { optionId : 6 , optionName : "Bar" } ,
41- { optionId : 7 , optionName : "Breakfast Included" } ,
42- { optionId : 8 , optionName : "Air Conditioning" } ,
43- { optionId : 9 , optionName : "Heating" } ,
44- { optionId : 10 , optionName : "Luggage Storage" } ,
45- { optionId : 11 , optionName : "24/7 Reception" } ,
46- { optionId : 12 , optionName : "Security Lockers" } ,
47- { optionId : 13 , optionName : "Bicycle Rental" } ,
48- { optionId : 14 , optionName : "Tour Desk" } ,
49- { optionId : 15 , optionName : "BBQ Area" }
50- ]
51- } ,
52- {
53- categoryId : 2 ,
54- categoryName : "Room Type" ,
55- options : [
56- { optionId : 16 , optionName : "Dormitory" } ,
57- { optionId : 17 , optionName : "Private Room" } ,
58- { optionId : 18 , optionName : "Double Room" } ,
59- { optionId : 19 , optionName : "Twin Room" } ,
60- { optionId : 20 , optionName : "Single Room" } ,
61- { optionId : 21 , optionName : "Family Room" } ,
62- { optionId : 22 , optionName : "Female Only Dorm" } ,
63- { optionId : 23 , optionName : "Male Only Dorm" } ,
64- { optionId : 24 , optionName : "Mixed Dorm" }
65- ]
66- } ,
67- {
68- categoryId : 3 ,
69- categoryName : "Location Type" ,
70- options : [
71- { optionId : 25 , optionName : "City Center" } ,
72- { optionId : 26 , optionName : "Near Train Station" } ,
73- { optionId : 27 , optionName : "Near Airport" } ,
74- { optionId : 28 , optionName : "Beachfront" } ,
75- { optionId : 29 , optionName : "Mountain View" } ,
76- { optionId : 30 , optionName : "Rural Area" } ,
77- { optionId : 31 , optionName : "University District" } ,
78- { optionId : 32 , optionName : "Shopping District" } ,
79- { optionId : 33 , optionName : "Historic District" } ,
80- { optionId : 34 , optionName : "Business District" }
81- ]
82- } ,
83- {
84- categoryId : 4 ,
85- categoryName : "Price Range" ,
86- options : [
87- { optionId : 35 , optionName : "Budget ($10-25)" } ,
88- { optionId : 36 , optionName : "Economy ($25-50)" } ,
89- { optionId : 37 , optionName : "Mid-range ($50-100)" } ,
90- { optionId : 38 , optionName : "Premium ($100-200)" } ,
91- { optionId : 39 , optionName : "Luxury ($200+)" }
92- ]
93- } ,
94- {
95- categoryId : 5 ,
96- categoryName : "Atmosphere" ,
97- options : [
98- { optionId : 40 , optionName : "Party/Social" } ,
99- { optionId : 41 , optionName : "Quiet/Relaxed" } ,
100- { optionId : 42 , optionName : "Family Friendly" } ,
101- { optionId : 43 , optionName : "Backpacker" } ,
102- { optionId : 44 , optionName : "Digital Nomad" } ,
103- { optionId : 45 , optionName : "Student" } ,
104- { optionId : 46 , optionName : "Eco-friendly" } ,
105- { optionId : 47 , optionName : "Boutique" } ,
106- { optionId : 48 , optionName : "Traditional" }
107- ]
108- }
109- ] ) ;
110- }
111-
11228 return NextResponse . json ( { message : 'Failed to fetch categories.' } , { status : 500 } ) ;
11329 }
11430}
11531
11632export async function POST ( req : NextRequest ) {
11733 try {
118- const { categoryName, options } = await req . json ( ) ;
34+ await requireAdmin ( req ) ;
35+ const { categoryName, options } = CreateCategorySchema . parse ( await req . json ( ) ) ;
11936
12037 const [ newCategory ] = await db . insert ( categories ) . values ( {
12138 category : categoryName
@@ -139,7 +56,8 @@ export async function POST(req: NextRequest) {
13956
14057export async function PUT ( req : NextRequest ) {
14158 try {
142- const { categoryId, categoryName, options } = await req . json ( ) ;
59+ await requireAdmin ( req ) ;
60+ const { categoryId, categoryName, options } = UpdateCategorySchema . parse ( await req . json ( ) ) ;
14361
14462 console . log ( 'PUT /api/categories - Incoming data:' , { categoryId, categoryName, options } ) ;
14563
@@ -208,6 +126,7 @@ export async function PUT(req: NextRequest) {
208126
209127export async function DELETE ( req : NextRequest ) {
210128 try {
129+ await requireAdmin ( req ) ;
211130 const { categoryId } = await req . json ( ) ;
212131
213132 if ( ! categoryId ) {
0 commit comments