Skip to content

Commit 5514313

Browse files
committed
fix: Fixed CreateChatRoom bug
Added Query
1 parent e32b644 commit 5514313

File tree

8 files changed

+218
-69
lines changed

8 files changed

+218
-69
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
-- +goose Up
2+
-- +goose StatementBegin
3+
-- For the `public.friends` table
4+
ALTER TABLE public.friends
5+
ALTER COLUMN id SET DATA TYPE integer USING id::integer;
6+
7+
-- For the `public.friend_requests` table
8+
ALTER TABLE public.friend_requests
9+
ALTER COLUMN id SET DATA TYPE integer USING id::integer;
10+
11+
-- For the `public.messages` table
12+
ALTER TABLE public.messages
13+
ALTER COLUMN id SET DATA TYPE integer USING id::integer;
14+
15+
-- For the `public.users_in_chat_rooms` table
16+
ALTER TABLE public.users_in_chat_rooms
17+
ALTER COLUMN id SET DATA TYPE integer USING id::integer;
18+
19+
-- +goose StatementEnd
20+
21+
-- +goose Down
22+
-- +goose StatementBegin
23+
-- For the `public.friends` table
24+
ALTER TABLE public.friends
25+
ALTER COLUMN id SET DATA TYPE integer USING id::integer;
26+
27+
-- For the `public.friend_requests` table
28+
ALTER TABLE public.friend_requests
29+
ALTER COLUMN id SET DATA TYPE integer USING id::integer;
30+
31+
-- For the `public.messages` table
32+
ALTER TABLE public.messages
33+
ALTER COLUMN id SET DATA TYPE integer USING id::integer;
34+
35+
-- For the `public.users_in_chat_rooms` table
36+
ALTER TABLE public.users_in_chat_rooms
37+
ALTER COLUMN id SET DATA TYPE integer USING id::integer;
38+
-- +goose StatementEnd

backend/internal/chat/model.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type ChatRoom struct {
1313
}
1414

1515
type Message struct {
16-
ID int `json:"id"`
16+
ID int32 `json:"id"`
1717
ChatRoomID uuid.UUID `json:"chat_room_id"`
1818
SenderID uuid.UUID `json:"sender_id"`
1919
Content string `json:"content"`
@@ -24,7 +24,7 @@ type Message struct {
2424
}
2525

2626
type UserInChatRoom struct {
27-
ID int `json:"id"`
27+
ID int32 `json:"id"`
2828
UserID uuid.UUID `json:"user_id"`
2929
ChatRoomID uuid.UUID `json:"chat_room_id"`
3030
}

backend/internal/chat/repository.go

+54-51
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package chat
33
import (
44
"context"
55
"database/sql"
6-
"errors"
76
"fmt"
8-
"log"
97
"log/slog"
108
"time"
119

1210
"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"
1413
)
1514

1615
type Repository interface {
@@ -33,76 +32,80 @@ func NewRepository(db *db.Database) Repository {
3332
}
3433

3534
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()
4335

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+
})
4640

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
5044
}
5145

5246
return chatRoom, nil
5347
}
5448

5549
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)
5950

51+
chatRoom, err := r.db.Querier.FindChatRoomByID(ctx, id)
6052
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")
6454
return nil, err
6555
}
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+
6763
}
6864

6965
// FindChatRoomInfoByID implements Repository.
7066
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)
8486
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)
8789
}
88-
defer rows.Close()
8990

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),
98101
}
99-
usersInChatRoom = append(usersInChatRoom, userInChatRoom)
100102
}
101103

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
106109

107110
}
108111

backend/internal/pkg/sqlc/models.go

+8-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/internal/pkg/sqlc/querier.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/internal/pkg/sqlc/query.sql.go

+6-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/internal/pkg/sqlc/sqlc.yaml

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ version: "2"
22
sql:
33
- engine: "postgresql"
44
queries: "query.sql" # This is relative to `internal/pkg/sqlc`
5-
# schema: "../../app/infrastructure/database/migrations" # Relative path to migrations
5+
#schema: "../../app/infrastructure/database/migrations" # Relative path to migrations
66
schema: "schema.sql"
77
gen:
88
go:
@@ -14,3 +14,10 @@ sql:
1414
emit_exact_table_names: false
1515
emit_interface: true
1616
emit_empty_slices: true
17+
overrides:
18+
- db_type: "uuid"
19+
go_type: "github.com/google/uuid.UUID"
20+
- db_type: "text"
21+
go_type: "string"
22+
- db_type: "timestamp"
23+
go_type: "time.Time"

0 commit comments

Comments
 (0)