ibraheem007 commited on
Commit
3f48b45
Β·
verified Β·
1 Parent(s): 64a99f4

Update db/helpers.py

Browse files
Files changed (1) hide show
  1. db/helpers.py +20 -26
db/helpers.py CHANGED
@@ -6,31 +6,31 @@ from sqlalchemy import func
6
  from db.models import User, ContentHistory, Feedback
7
  from db.connection import get_db, SessionLocal
8
 
9
- # ------------------------------------
10
- # βœ… User Management
11
- # ------------------------------------
12
- def create_user(user_id):
13
- """Create a new user in the database"""
14
  with next(get_db()) as db:
15
  try:
16
- user = User(id=user_id)
17
- db.add(user)
18
- db.commit()
19
- print(f"βœ… Created new user: {user_id}")
 
 
 
 
 
 
 
 
 
 
 
20
  return True
21
  except SQLAlchemyError as e:
22
  db.rollback()
23
- # User likely already exists - this is fine
24
  return False
25
 
26
- def ensure_user_exists(user_id):
27
- """Ensure user exists in database, create if needed"""
28
- with next(get_db()) as db:
29
- user = db.query(User).filter_by(id=user_id).first()
30
- if not user:
31
- return create_user(user_id)
32
- return True
33
-
34
  # ------------------------------------
35
  # βœ… Content History
36
  # ------------------------------------
@@ -38,9 +38,6 @@ def get_user_history(user_id):
38
  """Get user's content history from database"""
39
  with next(get_db()) as db:
40
  try:
41
- # Ensure user exists first
42
- ensure_user_exists(user_id)
43
-
44
  history = db.query(ContentHistory).filter_by(user_id=user_id).order_by(ContentHistory.created_at.desc()).all()
45
  print(f"βœ… Loaded {len(history)} history entries for user {user_id}")
46
  return history
@@ -49,15 +46,12 @@ def get_user_history(user_id):
49
  return []
50
 
51
  def save_content_to_history(data):
52
- """Save content to history"""
53
  with next(get_db()) as db:
54
  try:
55
- # Ensure user exists
56
- ensure_user_exists(data["user_id"])
57
-
58
  content = ContentHistory(
59
  id=uuid.uuid4(),
60
- user_id=data["user_id"],
61
  prompt=data["prompt"],
62
  output=data["output"],
63
  user_type=data["user_type"],
 
6
  from db.models import User, ContentHistory, Feedback
7
  from db.connection import get_db, SessionLocal
8
 
9
+ def ensure_user_exists(user_id):
10
+ """Ensure user exists in database - compatibility function for existing code"""
 
 
 
11
  with next(get_db()) as db:
12
  try:
13
+ user = db.query(User).filter_by(id=user_id).first()
14
+ if not user:
15
+ # Create a minimal user record if it doesn't exist
16
+ # This maintains compatibility with existing code that expects this function
17
+ user = User()
18
+ user.id = uuid.UUID(user_id)
19
+ user.fullname = "Legacy User"
20
+ user.username = f"user_{user_id[:8]}"
21
+ user.password_hash = ""
22
+ user.salt = ""
23
+
24
+ db.add(user)
25
+ db.commit()
26
+ print(f"βœ… Created legacy user record: {user_id}")
27
+ return True
28
  return True
29
  except SQLAlchemyError as e:
30
  db.rollback()
31
+ print(f"❌ Error ensuring user exists: {e}")
32
  return False
33
 
 
 
 
 
 
 
 
 
34
  # ------------------------------------
35
  # βœ… Content History
36
  # ------------------------------------
 
38
  """Get user's content history from database"""
39
  with next(get_db()) as db:
40
  try:
 
 
 
41
  history = db.query(ContentHistory).filter_by(user_id=user_id).order_by(ContentHistory.created_at.desc()).all()
42
  print(f"βœ… Loaded {len(history)} history entries for user {user_id}")
43
  return history
 
46
  return []
47
 
48
  def save_content_to_history(data):
49
+ """Save content to history - user_id comes from authenticated session"""
50
  with next(get_db()) as db:
51
  try:
 
 
 
52
  content = ContentHistory(
53
  id=uuid.uuid4(),
54
+ user_id=data["user_id"], # From authenticated session
55
  prompt=data["prompt"],
56
  output=data["output"],
57
  user_type=data["user_type"],