Skip to content

Commit

Permalink
Enhance GetAllChats method to filter chats by user ID and add token v…
Browse files Browse the repository at this point in the history
…alidation in GetChats handler
  • Loading branch information
L4B0MB4 committed Dec 5, 2024
1 parent e09c7fb commit 18eb31a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
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
5 changes: 3 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(limit, offset, userId.String())
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 18eb31a

Please sign in to comment.