aryanshinde commited on
Commit
a6cb2ef
·
verified ·
1 Parent(s): 11d0f36

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +13 -10
main.py CHANGED
@@ -471,13 +471,13 @@ async def serve_shelf():
471
  async def api_get_shelf():
472
  """API endpoint to get all shelf items with their progress."""
473
  rows = await sheets.get_all_rows_sync_fallback()
474
- progress_data = progress.get_all_progress()
475
  return {"status": "success", "data": rows, "progress": progress_data}
476
 
477
  @app.get("/api/progress")
478
  async def api_get_progress():
479
  """API endpoint to get all user progress."""
480
- progress_data = progress.get_all_progress()
481
  return {"status": "success", "progress": progress_data}
482
 
483
  class ProgressUpdate(BaseModel):
@@ -488,11 +488,12 @@ class ProgressUpdate(BaseModel):
488
  @app.post("/api/progress")
489
  async def api_update_progress(update: ProgressUpdate):
490
  """API endpoint to update user progress for an item."""
491
- success = progress.update_progress(update.content_hash, update.progress_seconds, update.is_completed)
492
  if success:
493
  return {"status": "success"}
494
  else:
495
- return {"status": "error", "message": "Failed to update progress"}, 500
 
496
 
497
  class NoteCreate(BaseModel):
498
  content_hash: str
@@ -502,16 +503,17 @@ class NoteCreate(BaseModel):
502
  @app.get("/api/notes/{content_hash}")
503
  async def api_get_notes(content_hash: str):
504
  """API endpoint to get all notes for a specific content item."""
505
- notes = progress.get_notes(content_hash)
506
  return {"status": "success", "notes": notes}
507
 
508
  @app.post("/api/notes")
509
  async def api_create_note(note: NoteCreate):
510
  """API endpoint to create a new timestamped note."""
511
- new_note = progress.add_note(note.content_hash, note.timestamp_seconds, note.note_text)
512
  if new_note:
513
  return {"status": "success", "note": new_note}
514
- return {"status": "error", "message": "Failed to create note"}, 500
 
515
 
516
  class GenerateNoteRequest(BaseModel):
517
  title: str
@@ -519,14 +521,15 @@ class GenerateNoteRequest(BaseModel):
519
  @app.post("/api/notes/{content_hash}/generate")
520
  async def api_generate_notes_summary(content_hash: str, req: GenerateNoteRequest):
521
  """API endpoint to generate an AI summary from existing notes."""
522
- notes = progress.get_notes(content_hash)
 
523
  if not notes:
524
- return {"status": "error", "message": "No notes found"}, 400
525
 
526
  summary = await ai_client.generate_notes_summary(req.title, notes)
527
  if summary:
528
  return {"status": "success", "summary": summary}
529
- return {"status": "error", "message": "Failed to generate summary"}, 500
530
 
531
  @app.get("/", response_class=HTMLResponse)
532
  async def serve_dashboard():
 
471
  async def api_get_shelf():
472
  """API endpoint to get all shelf items with their progress."""
473
  rows = await sheets.get_all_rows_sync_fallback()
474
+ progress_data = await asyncio.to_thread(progress.get_all_progress)
475
  return {"status": "success", "data": rows, "progress": progress_data}
476
 
477
  @app.get("/api/progress")
478
  async def api_get_progress():
479
  """API endpoint to get all user progress."""
480
+ progress_data = await asyncio.to_thread(progress.get_all_progress)
481
  return {"status": "success", "progress": progress_data}
482
 
483
  class ProgressUpdate(BaseModel):
 
488
  @app.post("/api/progress")
489
  async def api_update_progress(update: ProgressUpdate):
490
  """API endpoint to update user progress for an item."""
491
+ success = await asyncio.to_thread(progress.update_progress, update.content_hash, update.progress_seconds, update.is_completed)
492
  if success:
493
  return {"status": "success"}
494
  else:
495
+ from fastapi import HTTPException
496
+ raise HTTPException(status_code=500, detail="Failed to update progress")
497
 
498
  class NoteCreate(BaseModel):
499
  content_hash: str
 
503
  @app.get("/api/notes/{content_hash}")
504
  async def api_get_notes(content_hash: str):
505
  """API endpoint to get all notes for a specific content item."""
506
+ notes = await asyncio.to_thread(progress.get_notes, content_hash)
507
  return {"status": "success", "notes": notes}
508
 
509
  @app.post("/api/notes")
510
  async def api_create_note(note: NoteCreate):
511
  """API endpoint to create a new timestamped note."""
512
+ new_note = await asyncio.to_thread(progress.add_note, note.content_hash, note.timestamp_seconds, note.note_text)
513
  if new_note:
514
  return {"status": "success", "note": new_note}
515
+ from fastapi import HTTPException
516
+ raise HTTPException(status_code=500, detail="Failed to create note")
517
 
518
  class GenerateNoteRequest(BaseModel):
519
  title: str
 
521
  @app.post("/api/notes/{content_hash}/generate")
522
  async def api_generate_notes_summary(content_hash: str, req: GenerateNoteRequest):
523
  """API endpoint to generate an AI summary from existing notes."""
524
+ notes = await asyncio.to_thread(progress.get_notes, content_hash)
525
+ from fastapi import HTTPException
526
  if not notes:
527
+ raise HTTPException(status_code=400, detail="No notes found")
528
 
529
  summary = await ai_client.generate_notes_summary(req.title, notes)
530
  if summary:
531
  return {"status": "success", "summary": summary}
532
+ raise HTTPException(status_code=500, detail="Failed to generate summary")
533
 
534
  @app.get("/", response_class=HTMLResponse)
535
  async def serve_dashboard():