File size: 3,322 Bytes
5301c48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
import { NextRequest, NextResponse } from 'next/server'

interface CreateProjectRequest {
  project_name: string
  project_description: string
  template_name?: string
}

interface ProjectData {
  name: string
  description: string
  template_name?: string
}


// Get backend URL from environment
const PYTHON_BACKEND_URL = process.env.PYTHON_BACKEND_HOST || 'http://localhost:8002'

export async function POST(request: NextRequest) {
  try {
    const body: CreateProjectRequest = await request.json()
    
    // Validate required fields
    if (!body.project_name || !body.project_description || !body.template_name) {
      return NextResponse.json(
        { error: 'Project name, description and template name are required' },
        { status: 400 }
      )
    }

    // Prepare project data with both project and template information
    const projectData: ProjectData = {
      name: body.project_name.trim(),
      description: body.project_description.trim(),
      template_name: body.template_name || undefined,
    }

    // Send to Python backend
    const backendResponse = await fetch(`${PYTHON_BACKEND_URL}/project/create`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(projectData)
    })

    if (!backendResponse.ok) {
      // Try to get error details from backend
      let errorMessage = `Backend error: ${backendResponse.statusText}`
      try {
        const errorData = await backendResponse.json()
        errorMessage = errorData.error || errorMessage
      } catch {
        // If we can't parse the error, use the status text
      }

      console.error(`Error creating project in backend: ${backendResponse.status} - ${errorMessage}`)
      
      return NextResponse.json(
        { error: `Failed to create project: ${errorMessage}` },
        { status: backendResponse.status }
      )
    }

    // Get the created project from backend response
    const createdProject = await backendResponse.json()

    // Log successful creation with template info
    console.log("Project created successfully:", {
      projectName: createdProject.name,
      templateName: createdProject.template_name || 'none'
    })

    return NextResponse.json(createdProject, { status: 201 })

  } catch (error: unknown) {
    console.error('Error in createProject API route:', error)
    
    // Handle different types of errors
    if (error instanceof SyntaxError) {
      return NextResponse.json(
        { error: 'Invalid JSON in request body' },
        { status: 400 }
      )
    }

    if (error instanceof TypeError && error.message.includes('fetch')) {
      return NextResponse.json(
        { error: 'Unable to connect to backend service' },
        { status: 503 }
      )
    }

    const message = error instanceof Error ? error.message : 'An unknown error occurred'
    return NextResponse.json(
      { error: `Failed to create project: ${message}` },
      { status: 500 }
    )
  }
}

// Optional: Handle GET requests to return creation form or validation
export async function GET() {
  return NextResponse.json({
    message: 'Project creation endpoint',
    required_fields: ['project_name', 'project_description'],
    optional_fields: ['template_name', 'template_description'],
    method: 'POST'
  })
}