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
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"`
}
10 changes: 9 additions & 1 deletion pkg/query/httphandler/controller/chat_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/PRYVT/chats/pkg/models/query"
"github.com/PRYVT/chats/pkg/query/store/repository"
"github.com/PRYVT/chats/pkg/query/utils"
"github.com/PRYVT/utils/pkg/auth"
"github.com/PRYVT/utils/pkg/interfaces"
"github.com/gin-gonic/gin"
)
Expand Down Expand Up @@ -41,7 +42,14 @@ func (ctrl *ChatController) GetChats(c *gin.Context) {
limit := utils.GetLimit(c)
offset := utils.GetOffset(c)

Chats, err := ctrl.ChatRepo.GetAllChats(limit, offset)
token := auth.GetTokenFromHeader(c)
userUuid, err := auth.GetUserUuidFromToken(token)
if err != nil {
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
return
}

Chats, err := ctrl.ChatRepo.GetAllChats(limit, offset, userUuid)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
Expand Down
34 changes: 32 additions & 2 deletions pkg/query/store/repository/chats_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,19 @@ func (repo *ChatRepository) GetChatById(chatId uuid.UUID) (*models.Chat, error)
return &chat, nil
}

func (repo *ChatRepository) GetAllChats(limit, offset int) ([]models.ChatReduced, error) {
func (repo *ChatRepository) GetAllChats(limit, offset int, userId uuid.UUID) ([]models.ChatReduced, error) {
stmt, err := repo.db.Prepare(`
SELECT id, name
FROM Chats
WHERE id IN (SELECT chat_id FROM Users WHERE user_id = ?)
LIMIT ? OFFSET ?
`)
if err != nil {
return nil, err
}
defer stmt.Close()

rows, err := stmt.Query(limit, offset)
rows, err := stmt.Query(userId.String(), limit, offset)
if err != nil {
return nil, err
}
Expand All @@ -133,6 +134,35 @@ func (repo *ChatRepository) GetAllChats(limit, offset int) ([]models.ChatReduced
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
Loading