Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions backend/routes/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,16 @@ func CreateRoomHandler(c *gin.Context) {
}

// Query user document using email
userCollection := db.MongoClient.Database("DebateAI").Collection("users")
userCollection := db.MongoDatabase.Collection("users")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

emailStr, ok := email.(string)
if !ok {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Invalid email format"})
return
}

var user struct {
ID primitive.ObjectID `bson:"_id"`
Email string `bson:"email"`
Expand All @@ -73,7 +79,7 @@ func CreateRoomHandler(c *gin.Context) {
AvatarURL string `bson:"avatarUrl"`
}

err := userCollection.FindOne(ctx, bson.M{"email": email}).Decode(&user)
err := userCollection.FindOne(ctx, bson.M{"email": emailStr}).Decode(&user)
if err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
return
Expand All @@ -96,7 +102,7 @@ func CreateRoomHandler(c *gin.Context) {
Participants: []Participant{creatorParticipant},
}

roomCollection := db.MongoClient.Database("DebateAI").Collection("rooms")
roomCollection := db.MongoDatabase.Collection("rooms")
_, err = roomCollection.InsertOne(ctx, newRoom)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create room"})
Expand All @@ -109,7 +115,7 @@ func CreateRoomHandler(c *gin.Context) {
// GetRoomsHandler handles GET /rooms and returns all rooms.
func GetRoomsHandler(c *gin.Context) {

collection := db.MongoClient.Database("DebateAI").Collection("rooms")
collection := db.MongoDatabase.Collection("rooms")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

Expand Down Expand Up @@ -140,7 +146,7 @@ func JoinRoomHandler(c *gin.Context) {
}

// Query user document using email
userCollection := db.MongoClient.Database("DebateAI").Collection("users")
userCollection := db.MongoDatabase.Collection("users")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

Expand Down Expand Up @@ -174,7 +180,7 @@ func JoinRoomHandler(c *gin.Context) {
}

// Use atomic operation to join room
roomCollection := db.MongoClient.Database("DebateAI").Collection("rooms")
roomCollection := db.MongoDatabase.Collection("rooms")
filter := bson.M{"_id": roomId}
update := bson.M{
"$addToSet": bson.M{"participants": participant},
Expand Down Expand Up @@ -206,8 +212,8 @@ func GetRoomParticipantsHandler(c *gin.Context) {
}

// Query room document
roomCollection := db.MongoClient.Database("DebateAI").Collection("rooms")
userCollection := db.MongoClient.Database("DebateAI").Collection("users")
roomCollection := db.MongoDatabase.Collection("rooms")
userCollection := db.MongoDatabase.Collection("users")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

Expand Down
9 changes: 2 additions & 7 deletions backend/services/transcriptservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,20 +369,15 @@ func extractTopicFromTranscripts(transcripts map[string]string) string {
}

func lookupRoomTopic(ctx context.Context, roomID string) string {
if db.MongoClient == nil {
if db.MongoClient == nil || db.MongoDatabase == nil {
return ""
}

var database = db.MongoDatabase
if database == nil {
database = db.MongoClient.Database("DebateAI")
}

var room struct {
Topic string `bson:"topic"`
CurrentTopic string `bson:"currentTopic"`
}
if err := database.Collection("rooms").FindOne(ctx, bson.M{"_id": roomID}).Decode(&room); err == nil {
if err := db.MongoDatabase.Collection("rooms").FindOne(ctx, bson.M{"_id": roomID}).Decode(&room); err == nil {
if topic := strings.TrimSpace(room.Topic); topic != "" {
return topic
}
Expand Down