@@ -3,14 +3,13 @@ package chat
3
3
import (
4
4
"context"
5
5
"database/sql"
6
- "errors"
7
6
"fmt"
8
- "log"
9
7
"log/slog"
10
8
"time"
11
9
12
10
"github.com/google/uuid"
13
- "github.com/igh9410/blabber-hive/backend/internal/app/infrastructure/database"
11
+ db "github.com/igh9410/blabber-hive/backend/internal/app/infrastructure/database"
12
+ "github.com/igh9410/blabber-hive/backend/internal/pkg/sqlc"
14
13
)
15
14
16
15
type Repository interface {
@@ -33,76 +32,80 @@ func NewRepository(db *db.Database) Repository {
33
32
}
34
33
35
34
func (r * repository ) CreateChatRoom (ctx context.Context , chatRoom * ChatRoom ) (* ChatRoom , error ) {
36
- tx , err := r .db .Pool .Begin (ctx )
37
- if err != nil {
38
- slog .Error ("Creating Chatroom transaction failed: " , err .Error (), " in CreateChatRoom" )
39
- return nil , err // handle error appropriately
40
- }
41
- // Generate a UUID for user ID
42
- chatRoom .ID = uuid .New ()
43
35
44
- // Set the current timestamp for CreatedAt
45
- chatRoom .CreatedAt = time .Now ()
36
+ err := r .db .Querier .CreateChatRoom (ctx , sqlc.CreateChatRoomParams {
37
+ Name : sqlc .StringToPgtype (chatRoom .Name ),
38
+ CreatedAt : sqlc .TimeToPgtype (time .Now ()),
39
+ })
46
40
47
- if rbErr := tx . Rollback ( ctx ); rbErr != nil {
48
- slog .Error ("Transaction rollback failed: %v " , rbErr .Error (), " in CreateChatRoom" )
49
- return nil , rbErr
41
+ if err != nil {
42
+ slog .Error ("Error creating chat room: " , err .Error (), " in CreateChatRoom" )
43
+ return nil , err
50
44
}
51
45
52
46
return chatRoom , nil
53
47
}
54
48
55
49
func (r * repository ) FindChatRoomByID (ctx context.Context , id uuid.UUID ) (* ChatRoom , error ) {
56
- chatRoom := & ChatRoom {}
57
- query := "SELECT id,created_at FROM chat_rooms WHERE id = $1"
58
- err := r .db .Pool .QueryRow (ctx , query , id ).Scan (& chatRoom .ID , & chatRoom .CreatedAt )
59
50
51
+ chatRoom , err := r .db .Querier .FindChatRoomByID (ctx , id )
60
52
if err != nil {
61
- if err == sql .ErrNoRows {
62
- return nil , errors .New ("chat room not found" ) // or a custom error indicating not found
63
- }
53
+ slog .Error ("Error finding chat room by ID: " , err .Error (), " in FindChatRoomByID" )
64
54
return nil , err
65
55
}
66
- return chatRoom , nil
56
+
57
+ return & ChatRoom {
58
+ ID : chatRoom .ID ,
59
+ Name : sqlc .PgtypeToString (chatRoom .Name ),
60
+ CreatedAt : sqlc .PgtypeToTime (chatRoom .CreatedAt ),
61
+ }, nil
62
+
67
63
}
68
64
69
65
// FindChatRoomInfoByID implements Repository.
70
66
func (r * repository ) FindChatRoomInfoByID (ctx context.Context , chatRoomID uuid.UUID ) (* ChatRoomInfo , error ) {
71
- chatRoomInfo := & ChatRoomInfo {}
72
- query := `
73
- SELECT
74
- uicr.id,
75
- uicr.user_id,
76
- uicr.chat_room_id,
77
- cr.created_at
78
- FROM users_in_chat_rooms AS uicr
79
- INNER JOIN chat_rooms AS cr ON uicr.chat_room_id = cr.id
80
- WHERE cr.id = $1
81
- GROUP BY uicr.id, uicr.user_id, uicr.chat_room_id, cr.created_at
82
- `
83
- rows , err := r .db .Pool .Query (ctx , query , chatRoomID )
67
+ /*chatRoomInfo := &ChatRoomInfo{}
68
+ query := `
69
+ SELECT
70
+ uicr.id,
71
+ uicr.user_id,
72
+ uicr.chat_room_id,
73
+ cr.created_at
74
+ FROM users_in_chat_rooms AS uicr
75
+ INNER JOIN chat_rooms AS cr ON uicr.chat_room_id = cr.id
76
+ WHERE cr.id = $1
77
+ GROUP BY uicr.id, uicr.user_id, uicr.chat_room_id, cr.created_at
78
+ `
79
+ rows, err := r.db.Pool.Query(ctx, query, chatRoomID)
80
+ if err != nil {
81
+ log.Printf("Failed to fetch chat room info, err: %v", err)
82
+ return nil, err
83
+ }
84
+ defer rows.Close() */
85
+ sqlcRows , err := r .db .Querier .FindChatRoomInfoByID (ctx , chatRoomID )
84
86
if err != nil {
85
- log . Printf ( "Failed to fetch chat room info, err: %v" , err )
86
- return nil , err
87
+ slog . Error ( "Error finding chat room info by ID" , "error" , err , "chatRoomID" , chatRoomID )
88
+ return nil , fmt . Errorf ( "failed to find chat room info: %w" , err )
87
89
}
88
- defer rows .Close ()
89
90
90
- usersInChatRoom := make ([]UserInChatRoom , 0 )
91
- var createdAt time.Time
92
- for rows .Next () {
93
- var userInChatRoom UserInChatRoom
94
- err := rows .Scan (& userInChatRoom .ID , & userInChatRoom .UserID , & userInChatRoom .ChatRoomID , & createdAt )
95
- if err != nil {
96
- log .Printf ("Failed to scan chat room info, err: %v" , err .Error ())
97
- return nil , err
91
+ if len (sqlcRows ) == 0 {
92
+ return nil , sql .ErrNoRows
93
+ }
94
+
95
+ usersInChatRoom := make ([]UserInChatRoom , len (sqlcRows ))
96
+ for i , row := range sqlcRows {
97
+ usersInChatRoom [i ] = UserInChatRoom {
98
+ ID : row .ID ,
99
+ UserID : sqlc .PgtypeToUUID (row .UserID ),
100
+ ChatRoomID : sqlc .PgtypeToUUID (row .ChatRoomID ),
98
101
}
99
- usersInChatRoom = append (usersInChatRoom , userInChatRoom )
100
102
}
101
103
102
- chatRoomInfo .ID = chatRoomID
103
- chatRoomInfo .UserList = usersInChatRoom
104
- chatRoomInfo .CreatedAt = createdAt
105
- return chatRoomInfo , nil
104
+ return & ChatRoomInfo {
105
+ ID : chatRoomID ,
106
+ UserList : usersInChatRoom ,
107
+ CreatedAt : sqlc .PgtypeToTime (sqlcRows [0 ].CreatedAt ),
108
+ }, nil
106
109
107
110
}
108
111
0 commit comments