File size: 16,393 Bytes
98f6c3a
7378c28
 
 
98f6c3a
 
7378c28
98f6c3a
 
 
 
7378c28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98f6c3a
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
7378c28
 
 
 
 
 
98f6c3a
7378c28
 
98f6c3a
7378c28
 
 
 
98f6c3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
7378c28
 
 
 
98f6c3a
7378c28
 
 
 
 
 
 
 
 
 
 
98f6c3a
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7378c28
 
 
98f6c3a
 
 
 
 
 
 
 
7378c28
98f6c3a
7378c28
 
98f6c3a
7378c28
 
 
 
 
 
 
 
 
 
98f6c3a
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7378c28
 
 
 
98f6c3a
 
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
7378c28
 
 
 
98f6c3a
7378c28
 
 
 
 
 
 
 
 
 
 
98f6c3a
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
 
 
 
 
 
7378c28
 
 
 
98f6c3a
 
 
 
 
 
 
7378c28
98f6c3a
 
7378c28
98f6c3a
 
7378c28
 
 
 
 
98f6c3a
7378c28
 
98f6c3a
7378c28
 
 
 
98f6c3a
 
 
7378c28
98f6c3a
 
7378c28
98f6c3a
 
7378c28
98f6c3a
 
 
 
 
 
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
7378c28
 
 
 
98f6c3a
7378c28
 
 
 
 
 
 
 
 
 
98f6c3a
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
7378c28
98f6c3a
 
 
 
 
 
 
 
 
 
 
 
 
7378c28
 
 
 
98f6c3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7378c28
 
 
 
98f6c3a
7378c28
 
 
 
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from fastapi import APIRouter, HTTPException, Depends, status
from typing import List, Optional
from pydantic import BaseModel
import json
import time
import logging
from .auth_router import get_current_user
from app.utils import db_http

# Configure logging
logger = logging.getLogger("auth-server")

router = APIRouter()

# Models
class ProjectBase(BaseModel):
    title: str
    description: Optional[str] = None
    geojson: Optional[str] = None

class ProjectCreate(ProjectBase):
    pass

class ProjectUpdate(ProjectBase):
    title: Optional[str] = None

class ProjectResponse(ProjectBase):
    id: int
    owner_id: int
    storage_bucket: str
    created_at: str
    updated_at: str

# Routes
@router.post("/", response_model=ProjectResponse)
async def create_project(
    project: ProjectCreate,
    current_user = Depends(get_current_user)
):
    operation_id = f"create_project_{int(time.time())}"
    logger.info(f"[{operation_id}] Creating new project: {project.title}")

    try:
        # Get user ID based on the type of current_user
        if isinstance(current_user, dict):
            user_id = current_user.get("id")
        else:
            user_id = current_user[0]

        logger.info(f"[{operation_id}] User ID: {user_id}")

        # Validate GeoJSON if provided
        if project.geojson:
            try:
                geojson_data = json.loads(project.geojson)
                # Basic validation
                if not isinstance(geojson_data, dict) or "type" not in geojson_data:
                    logger.warning(f"[{operation_id}] Invalid GeoJSON format")
                    raise ValueError("Invalid GeoJSON format")
            except json.JSONDecodeError:
                logger.warning(f"[{operation_id}] Invalid GeoJSON format (JSON decode error)")
                raise HTTPException(
                    status_code=status.HTTP_400_BAD_REQUEST,
                    detail="Invalid GeoJSON format"
                )

        logger.info(f"[{operation_id}] GeoJSON validation passed")

        # Prepare project data
        project_data = {
            "owner_id": user_id,
            "title": project.title,
            "description": project.description,
            "geojson": project.geojson,
            "storage_bucket": "default"  # Default storage bucket
        }

        # Insert the new project using HTTP API
        logger.info(f"[{operation_id}] Inserting new project")
        project_id = db_http.insert_record("projects", project_data, operation_id=operation_id)

        if not project_id:
            logger.error(f"[{operation_id}] Failed to insert project")
            raise HTTPException(
                status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                detail="Failed to create project"
            )

        logger.info(f"[{operation_id}] Project inserted with ID: {project_id}")

        # Get the newly created project
        new_project = db_http.get_record_by_id("projects", project_id, operation_id=operation_id)

        if not new_project:
            logger.warning(f"[{operation_id}] Project not found after insert, trying by owner and title")

            # Try to get by owner and title as fallback
            projects = db_http.select_records(
                "projects",
                condition="owner_id = ? AND title = ?",
                condition_params=[
                    {"type": "integer", "value": str(user_id)},
                    {"type": "text", "value": project.title}
                ],
                order_by="id DESC",
                limit=1,
                operation_id=operation_id
            )

            if projects:
                new_project = projects[0]
                project_id = new_project.get("id")
                logger.info(f"[{operation_id}] Found project by owner and title with ID: {project_id}")
            else:
                logger.error(f"[{operation_id}] Project not found after insert")
                raise HTTPException(
                    status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                    detail="Project was created but could not be retrieved"
                )

        logger.info(f"[{operation_id}] Project created successfully with ID: {project_id}")

        return {
            "id": new_project.get("id"),
            "owner_id": new_project.get("owner_id"),
            "title": new_project.get("title"),
            "description": new_project.get("description"),
            "geojson": new_project.get("geojson"),
            "storage_bucket": new_project.get("storage_bucket", "default"),
            "created_at": new_project.get("created_at", ""),
            "updated_at": new_project.get("updated_at", "")
        }
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"[{operation_id}] Error creating project: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=str(e)
        )

@router.get("/", response_model=List[ProjectResponse])
async def get_projects(
    current_user = Depends(get_current_user),
    skip: int = 0,
    limit: int = 100
):
    operation_id = f"get_projects_{int(time.time())}"
    logger.info(f"[{operation_id}] Getting projects (skip={skip}, limit={limit})")

    try:
        # Get user ID based on the type of current_user
        if isinstance(current_user, dict):
            user_id = current_user.get("id")
        else:
            user_id = current_user[0]

        logger.info(f"[{operation_id}] User ID: {user_id}")

        # Get projects using HTTP API
        projects = db_http.select_records(
            "projects",
            condition="owner_id = ?",
            condition_params=[{"type": "integer", "value": str(user_id)}],
            order_by="updated_at DESC",
            limit=limit,
            offset=skip,
            operation_id=operation_id
        )

        logger.info(f"[{operation_id}] Found {len(projects)} projects")

        # Projects are already in dictionary format from the HTTP API
        # Just need to ensure all required fields are present
        result = []
        for project in projects:
            result.append({
                "id": project.get("id"),
                "owner_id": project.get("owner_id"),
                "title": project.get("title"),
                "description": project.get("description"),
                "geojson": project.get("geojson"),
                "storage_bucket": project.get("storage_bucket", "default"),
                "created_at": project.get("created_at", ""),
                "updated_at": project.get("updated_at", "")
            })

        return result
    except Exception as e:
        logger.error(f"[{operation_id}] Error getting projects: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=str(e)
        )

@router.get("/{project_id}", response_model=ProjectResponse)
async def get_project(
    project_id: int,
    current_user = Depends(get_current_user)
):
    operation_id = f"get_project_{int(time.time())}"
    logger.info(f"[{operation_id}] Getting project with ID: {project_id}")

    try:
        # Get user ID based on the type of current_user
        if isinstance(current_user, dict):
            user_id = current_user.get("id")
        else:
            user_id = current_user[0]

        logger.info(f"[{operation_id}] User ID: {user_id}")

        # Get project using HTTP API
        projects = db_http.select_records(
            "projects",
            condition="id = ? AND owner_id = ?",
            condition_params=[
                {"type": "integer", "value": str(project_id)},
                {"type": "integer", "value": str(user_id)}
            ],
            limit=1,
            operation_id=operation_id
        )

        if not projects:
            logger.warning(f"[{operation_id}] Project not found with ID: {project_id}")
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="Project not found"
            )

        project = projects[0]
        logger.info(f"[{operation_id}] Found project: {project.get('title')}")

        return {
            "id": project.get("id"),
            "owner_id": project.get("owner_id"),
            "title": project.get("title"),
            "description": project.get("description"),
            "geojson": project.get("geojson"),
            "storage_bucket": project.get("storage_bucket", "default"),
            "created_at": project.get("created_at", ""),
            "updated_at": project.get("updated_at", "")
        }
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"[{operation_id}] Error getting project: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=str(e)
        )

@router.patch("/{project_id}", response_model=ProjectResponse)
async def update_project(
    project_id: int,
    project_update: ProjectUpdate,
    current_user = Depends(get_current_user)
):
    operation_id = f"update_project_{int(time.time())}"
    logger.info(f"[{operation_id}] Updating project with ID: {project_id}")

    try:
        # Get user ID based on the type of current_user
        if isinstance(current_user, dict):
            user_id = current_user.get("id")
        else:
            user_id = current_user[0]

        logger.info(f"[{operation_id}] User ID: {user_id}")

        # Check if project exists and belongs to user
        projects = db_http.select_records(
            "projects",
            condition="id = ? AND owner_id = ?",
            condition_params=[
                {"type": "integer", "value": str(project_id)},
                {"type": "integer", "value": str(user_id)}
            ],
            limit=1,
            operation_id=operation_id
        )

        if not projects:
            logger.warning(f"[{operation_id}] Project not found with ID: {project_id}")
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="Project not found"
            )

        existing_project = projects[0]
        logger.info(f"[{operation_id}] Found project: {existing_project.get('title')}")

        # Prepare update data
        update_data = {}

        if project_update.title is not None:
            update_data["title"] = project_update.title

        if project_update.description is not None:
            update_data["description"] = project_update.description

        if project_update.geojson is not None:
            # Validate GeoJSON
            try:
                geojson_data = json.loads(project_update.geojson)
                if not isinstance(geojson_data, dict) or "type" not in geojson_data:
                    logger.warning(f"[{operation_id}] Invalid GeoJSON format")
                    raise ValueError("Invalid GeoJSON format")
            except json.JSONDecodeError:
                logger.warning(f"[{operation_id}] Invalid GeoJSON format (JSON decode error)")
                raise HTTPException(
                    status_code=status.HTTP_400_BAD_REQUEST,
                    detail="Invalid GeoJSON format"
                )

            update_data["geojson"] = project_update.geojson

        # Add updated_at field
        update_data["updated_at"] = time.strftime('%Y-%m-%d %H:%M:%S')

        # If no fields to update, return the existing project
        if len(update_data) <= 1:  # Only updated_at
            logger.info(f"[{operation_id}] No fields to update")
            return {
                "id": existing_project.get("id"),
                "owner_id": existing_project.get("owner_id"),
                "title": existing_project.get("title"),
                "description": existing_project.get("description"),
                "geojson": existing_project.get("geojson"),
                "storage_bucket": existing_project.get("storage_bucket", "default"),
                "created_at": existing_project.get("created_at", ""),
                "updated_at": existing_project.get("updated_at", "")
            }

        # Update the project using HTTP API
        logger.info(f"[{operation_id}] Updating project with data: {update_data}")
        success = db_http.update_record(
            "projects",
            update_data,
            "id = ? AND owner_id = ?",
            [
                {"type": "integer", "value": str(project_id)},
                {"type": "integer", "value": str(user_id)}
            ],
            operation_id=operation_id
        )

        if not success:
            logger.error(f"[{operation_id}] Failed to update project")
            raise HTTPException(
                status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                detail="Failed to update project"
            )

        logger.info(f"[{operation_id}] Project updated successfully")

        # Get the updated project
        updated_project = db_http.get_record_by_id("projects", project_id, operation_id=operation_id)

        if not updated_project:
            logger.warning(f"[{operation_id}] Updated project not found")
            raise HTTPException(
                status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                detail="Project was updated but could not be retrieved"
            )

        return {
            "id": updated_project.get("id"),
            "owner_id": updated_project.get("owner_id"),
            "title": updated_project.get("title"),
            "description": updated_project.get("description"),
            "geojson": updated_project.get("geojson"),
            "storage_bucket": updated_project.get("storage_bucket", "default"),
            "created_at": updated_project.get("created_at", ""),
            "updated_at": updated_project.get("updated_at", "")
        }
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"[{operation_id}] Error updating project: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=str(e)
        )

@router.delete("/{project_id}", status_code=status.HTTP_204_NO_CONTENT)
async def delete_project(
    project_id: int,
    current_user = Depends(get_current_user)
):
    operation_id = f"delete_project_{int(time.time())}"
    logger.info(f"[{operation_id}] Deleting project with ID: {project_id}")

    try:
        # Get user ID based on the type of current_user
        if isinstance(current_user, dict):
            user_id = current_user.get("id")
        else:
            user_id = current_user[0]

        logger.info(f"[{operation_id}] User ID: {user_id}")

        # Check if project exists and belongs to user
        projects = db_http.select_records(
            "projects",
            condition="id = ? AND owner_id = ?",
            condition_params=[
                {"type": "integer", "value": str(project_id)},
                {"type": "integer", "value": str(user_id)}
            ],
            limit=1,
            operation_id=operation_id
        )

        if not projects:
            logger.warning(f"[{operation_id}] Project not found with ID: {project_id}")
            raise HTTPException(
                status_code=status.HTTP_404_NOT_FOUND,
                detail="Project not found"
            )

        logger.info(f"[{operation_id}] Found project to delete: {projects[0].get('title')}")

        # Delete the project using HTTP API
        success = db_http.delete_record(
            "projects",
            "id = ?",
            [{"type": "integer", "value": str(project_id)}],
            operation_id=operation_id
        )

        if not success:
            logger.error(f"[{operation_id}] Failed to delete project")
            raise HTTPException(
                status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
                detail="Failed to delete project"
            )

        logger.info(f"[{operation_id}] Project deleted successfully")

        return None
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"[{operation_id}] Error deleting project: {str(e)}")
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=str(e)
        )