Skip to content

Commit

Permalink
Add UserIds field to ChatReduced model and update GetAllChats to retr…
Browse files Browse the repository at this point in the history
…ieve associated user IDs
  • Loading branch information
L4B0MB4 committed Dec 6, 2024
1 parent e42b4c7 commit f12c850
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/models/query/chat_reduced.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type ChatReduced struct {
Id uuid.UUID `json:"id" binding:"required"`
Name string `json:"name" binding:"required"`
Id uuid.UUID `json:"id" binding:"required"`
Name string `json:"name" binding:"required"`
UserIds []uuid.UUID `json:"user_ids" binding:"required"`
}
29 changes: 29 additions & 0 deletions pkg/query/store/repository/chats_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,35 @@ func (repo *ChatRepository) GetAllChats(limit, offset int, userId uuid.UUID) ([]
if err := rows.Scan(&chat.Id, &chat.Name); err != nil {
return nil, err
}

userStmt, err := repo.db.Prepare(`
SELECT user_id
FROM Users
WHERE chat_id = ?
`)
if err != nil {
return nil, err
}
defer userStmt.Close()

userRows, err := userStmt.Query(chat.Id.String())
if err != nil {
return nil, err
}
defer userRows.Close()

for userRows.Next() {
var userId uuid.UUID
if err := userRows.Scan(&userId); err != nil {
return nil, err
}
chat.UserIds = append(chat.UserIds, userId)
}

if err := userRows.Err(); err != nil {
return nil, err
}

chats = append(chats, chat)
}

Expand Down

0 comments on commit f12c850

Please sign in to comment.