testing
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -5,6 +5,7 @@ from models import JournalEntryCreate, JournalEntryUpdate, JournalEntry, Entries
|
||||
from datetime import datetime, timedelta
|
||||
from typing import List, Optional
|
||||
from bson import ObjectId
|
||||
from bson.errors import InvalidId
|
||||
from utils import format_ist_timestamp
|
||||
|
||||
router = APIRouter()
|
||||
@@ -50,7 +51,10 @@ async def create_entry(user_id: str, entry_data: JournalEntryCreate):
|
||||
|
||||
try:
|
||||
user_oid = ObjectId(user_id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=400, detail="Invalid user ID format")
|
||||
|
||||
try:
|
||||
# Verify user exists
|
||||
user = db.users.find_one({"_id": user_oid})
|
||||
if not user:
|
||||
@@ -91,9 +95,6 @@ async def create_entry(user_id: str, entry_data: JournalEntryCreate):
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
if "invalid ObjectId" in str(e).lower():
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Invalid user ID format")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to create entry: {str(e)}")
|
||||
|
||||
@@ -113,7 +114,10 @@ async def get_user_entries(
|
||||
|
||||
try:
|
||||
user_oid = ObjectId(user_id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=400, detail="Invalid user ID format")
|
||||
|
||||
try:
|
||||
# Verify user exists
|
||||
user = db.users.find_one({"_id": user_oid})
|
||||
if not user:
|
||||
@@ -142,10 +146,9 @@ async def get_user_entries(
|
||||
"hasMore": has_more
|
||||
}
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
if "invalid ObjectId" in str(e).lower():
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Invalid user ID format")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to fetch entries: {str(e)}")
|
||||
|
||||
@@ -158,7 +161,10 @@ async def get_entry(user_id: str, entry_id: str):
|
||||
try:
|
||||
user_oid = ObjectId(user_id)
|
||||
entry_oid = ObjectId(entry_id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=400, detail="Invalid ID format")
|
||||
|
||||
try:
|
||||
entry = db.entries.find_one({
|
||||
"_id": entry_oid,
|
||||
"userId": user_oid
|
||||
@@ -168,9 +174,9 @@ async def get_entry(user_id: str, entry_id: str):
|
||||
raise HTTPException(status_code=404, detail="Entry not found")
|
||||
|
||||
return _format_entry(entry)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
if "invalid ObjectId" in str(e).lower():
|
||||
raise HTTPException(status_code=400, detail="Invalid ID format")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to fetch entry: {str(e)}")
|
||||
|
||||
@@ -183,7 +189,10 @@ async def update_entry(user_id: str, entry_id: str, entry_data: JournalEntryUpda
|
||||
try:
|
||||
user_oid = ObjectId(user_id)
|
||||
entry_oid = ObjectId(entry_id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=400, detail="Invalid ID format")
|
||||
|
||||
try:
|
||||
update_data = entry_data.model_dump(exclude_unset=True)
|
||||
update_data["updatedAt"] = datetime.utcnow()
|
||||
|
||||
@@ -206,9 +215,9 @@ async def update_entry(user_id: str, entry_id: str, entry_data: JournalEntryUpda
|
||||
# Fetch and return updated entry
|
||||
entry = db.entries.find_one({"_id": entry_oid})
|
||||
return _format_entry(entry)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
if "invalid ObjectId" in str(e).lower():
|
||||
raise HTTPException(status_code=400, detail="Invalid ID format")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to update entry: {str(e)}")
|
||||
|
||||
@@ -221,7 +230,10 @@ async def delete_entry(user_id: str, entry_id: str):
|
||||
try:
|
||||
user_oid = ObjectId(user_id)
|
||||
entry_oid = ObjectId(entry_id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=400, detail="Invalid ID format")
|
||||
|
||||
try:
|
||||
result = db.entries.delete_one({
|
||||
"_id": entry_oid,
|
||||
"userId": user_oid
|
||||
@@ -231,9 +243,9 @@ async def delete_entry(user_id: str, entry_id: str):
|
||||
raise HTTPException(status_code=404, detail="Entry not found")
|
||||
|
||||
return {"message": "Entry deleted successfully"}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
if "invalid ObjectId" in str(e).lower():
|
||||
raise HTTPException(status_code=400, detail="Invalid ID format")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to delete entry: {str(e)}")
|
||||
|
||||
@@ -249,7 +261,10 @@ async def get_entries_by_date(user_id: str, date_str: str):
|
||||
|
||||
try:
|
||||
user_oid = ObjectId(user_id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=400, detail="Invalid user ID format")
|
||||
|
||||
try:
|
||||
# Parse date
|
||||
target_date = datetime.strptime(date_str, "%Y-%m-%d")
|
||||
next_date = target_date + timedelta(days=1)
|
||||
@@ -274,10 +289,9 @@ async def get_entries_by_date(user_id: str, date_str: str):
|
||||
except ValueError:
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Invalid date format. Use YYYY-MM-DD")
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
if "invalid ObjectId" in str(e).lower():
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Invalid user ID format")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to fetch entries: {str(e)}")
|
||||
|
||||
@@ -293,7 +307,10 @@ async def get_entries_by_month(user_id: str, year: int, month: int, limit: int =
|
||||
|
||||
try:
|
||||
user_oid = ObjectId(user_id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=400, detail="Invalid user ID format")
|
||||
|
||||
try:
|
||||
if not (1 <= month <= 12):
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Month must be between 1 and 12")
|
||||
@@ -325,10 +342,9 @@ async def get_entries_by_month(user_id: str, year: int, month: int, limit: int =
|
||||
}
|
||||
except ValueError:
|
||||
raise HTTPException(status_code=400, detail="Invalid year or month")
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
if "invalid ObjectId" in str(e).lower():
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Invalid user ID format")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to fetch entries: {str(e)}")
|
||||
|
||||
@@ -347,6 +363,8 @@ async def convert_utc_to_ist(data: dict):
|
||||
"utc": utc_timestamp,
|
||||
"ist": ist_timestamp
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
||||
except Exception as e:
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
"""User management routes"""
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from pymongo.errors import DuplicateKeyError, WriteError
|
||||
from db import get_database
|
||||
from models import UserCreate, UserUpdate, User
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from bson import ObjectId
|
||||
from bson.errors import InvalidId
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@@ -56,6 +56,8 @@ async def register_user(user_data: UserCreate):
|
||||
"updatedAt": user["updatedAt"].isoformat(),
|
||||
"message": "User registered successfully" if result.upserted_id else "User already exists"
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Registration failed: {str(e)}")
|
||||
@@ -80,6 +82,8 @@ async def get_user_by_email(email: str):
|
||||
"createdAt": user["createdAt"].isoformat(),
|
||||
"updatedAt": user["updatedAt"].isoformat()
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to fetch user: {str(e)}")
|
||||
@@ -91,7 +95,12 @@ async def get_user_by_id(user_id: str):
|
||||
db = get_database()
|
||||
|
||||
try:
|
||||
user = db.users.find_one({"_id": ObjectId(user_id)})
|
||||
user_oid = ObjectId(user_id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=400, detail="Invalid user ID format")
|
||||
|
||||
try:
|
||||
user = db.users.find_one({"_id": user_oid})
|
||||
if not user:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
@@ -104,10 +113,9 @@ async def get_user_by_id(user_id: str):
|
||||
"createdAt": user["createdAt"].isoformat(),
|
||||
"updatedAt": user["updatedAt"].isoformat()
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
if "invalid ObjectId" in str(e).lower():
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Invalid user ID format")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to fetch user: {str(e)}")
|
||||
|
||||
@@ -117,13 +125,18 @@ async def update_user(user_id: str, user_data: UserUpdate):
|
||||
"""Update user profile."""
|
||||
db = get_database()
|
||||
|
||||
try:
|
||||
user_oid = ObjectId(user_id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=400, detail="Invalid user ID format")
|
||||
|
||||
try:
|
||||
# Prepare update data (exclude None values)
|
||||
update_data = user_data.model_dump(exclude_unset=True)
|
||||
update_data["updatedAt"] = datetime.utcnow()
|
||||
|
||||
result = db.users.update_one(
|
||||
{"_id": ObjectId(user_id)},
|
||||
{"_id": user_oid},
|
||||
{"$set": update_data}
|
||||
)
|
||||
|
||||
@@ -131,7 +144,7 @@ async def update_user(user_id: str, user_data: UserUpdate):
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
# Fetch and return updated user
|
||||
user = db.users.find_one({"_id": ObjectId(user_id)})
|
||||
user = db.users.find_one({"_id": user_oid})
|
||||
return {
|
||||
"id": str(user["_id"]),
|
||||
"email": user["email"],
|
||||
@@ -142,10 +155,9 @@ async def update_user(user_id: str, user_data: UserUpdate):
|
||||
"updatedAt": user["updatedAt"].isoformat(),
|
||||
"message": "User updated successfully"
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
if "invalid ObjectId" in str(e).lower():
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Invalid user ID format")
|
||||
raise HTTPException(status_code=500, detail=f"Update failed: {str(e)}")
|
||||
|
||||
|
||||
@@ -154,33 +166,27 @@ async def delete_user(user_id: str):
|
||||
"""Delete user account and all associated data."""
|
||||
db = get_database()
|
||||
|
||||
try:
|
||||
user_oid = ObjectId(user_id)
|
||||
except InvalidId:
|
||||
raise HTTPException(status_code=400, detail="Invalid user ID format")
|
||||
|
||||
try:
|
||||
# Delete user
|
||||
user_result = db.users.delete_one({"_id": ObjectId(user_id)})
|
||||
user_result = db.users.delete_one({"_id": user_oid})
|
||||
if user_result.deleted_count == 0:
|
||||
raise HTTPException(status_code=404, detail="User not found")
|
||||
|
||||
# Delete all user's entries
|
||||
entry_result = db.entries.delete_many({"userId": ObjectId(user_id)})
|
||||
entry_result = db.entries.delete_many({"userId": user_oid})
|
||||
|
||||
return {
|
||||
"message": "User deleted successfully",
|
||||
"user_deleted": user_result.deleted_count,
|
||||
"entries_deleted": entry_result.deleted_count
|
||||
}
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
if "invalid ObjectId" in str(e).lower():
|
||||
raise HTTPException(
|
||||
status_code=400, detail="Invalid user ID format")
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Deletion failed: {str(e)}")
|
||||
|
||||
# Delete all entries by user
|
||||
db.entries.delete_many({"userId": user_id})
|
||||
|
||||
# Delete user settings
|
||||
db.settings.delete_one({"userId": user_id})
|
||||
|
||||
return {"message": "User and associated data deleted"}
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=500, detail=str(e))
|
||||
|
||||
Reference in New Issue
Block a user