Skip to content

Commit ae2b4d8

Browse files
authored
Merge pull request #39 from igh9410/dev
Implemented transaction rollback for create
2 parents 056a678 + 682f938 commit ae2b4d8

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

backend/internal/chat/repository.go

+34-21
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Repository interface {
1818
FindChatRoomInfoByID(ctx context.Context, chatRoomID uuid.UUID) (*ChatRoomInfo, error)
1919
JoinChatRoomByID(ctx context.Context, chatRoomID uuid.UUID, userID uuid.UUID) (*ChatRoom, error)
2020
FindChatRoomList(ctx context.Context) ([]*ChatRoom, error)
21-
SaveMessage(ctx context.Context, message *Message) error
21+
2222
GetPaginatedMessages(ctx context.Context, chatRoomID uuid.UUID, cursor *time.Time, pageSize int) ([]Message, error)
2323
GetFirstPageMessages(ctx context.Context, chatRoomID uuid.UUID, pageSize int) ([]Message, error)
2424
}
@@ -28,6 +28,7 @@ type DBTX interface {
2828
PrepareContext(context.Context, string) (*sql.Stmt, error)
2929
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
3030
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
31+
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
3132
}
3233

3334
type repository struct {
@@ -39,6 +40,11 @@ func NewRepository(db DBTX) Repository {
3940
}
4041

4142
func (r *repository) CreateChatRoom(ctx context.Context, chatRoom *ChatRoom) (*ChatRoom, error) {
43+
tx, err := r.db.BeginTx(ctx, nil)
44+
if err != nil {
45+
slog.Error("Creating Chatroom transaction failed")
46+
return nil, err // handle error appropriately
47+
}
4248
// Generate a UUID for user ID
4349
chatRoom.ID = uuid.New()
4450

@@ -47,10 +53,10 @@ func (r *repository) CreateChatRoom(ctx context.Context, chatRoom *ChatRoom) (*C
4753

4854
query := "INSERT INTO chat_rooms(id, name, created_at) VALUES ($1, $2, $3) RETURNING id"
4955

50-
err := r.db.QueryRowContext(ctx, query, chatRoom.ID, chatRoom.Name, chatRoom.CreatedAt).Scan(&chatRoom.ID)
56+
err = r.db.QueryRowContext(ctx, query, chatRoom.ID, chatRoom.Name, chatRoom.CreatedAt).Scan(&chatRoom.ID)
5157
if err != nil {
5258
log.Printf("Error creating chat room: %v", err)
53-
59+
tx.Rollback()
5460
return nil, errors.New("failed to create chat room")
5561
}
5662

@@ -122,11 +128,33 @@ func (r *repository) JoinChatRoomByID(ctx context.Context, chatRoomID uuid.UUID,
122128
// INSERT into chat rooms, UNIQUE constraint will prevent duplicates
123129
query := `INSERT INTO users_in_chat_rooms (user_id, chat_room_id) VALUES ($1, $2)`
124130

125-
_, err2 := r.db.ExecContext(ctx, query, userID, chatRoomID)
131+
tx, err := r.db.BeginTx(ctx, nil)
132+
if err != nil {
133+
slog.Error("Joining chat room transaction failed")
134+
return nil, err // handle error appropriately
135+
}
136+
137+
// Ensure rollback in case of error
138+
defer func() {
139+
if err != nil {
140+
if rbErr := tx.Rollback(); rbErr != nil {
141+
slog.Error("Transaction rollback failed: %v", rbErr)
142+
}
143+
}
144+
}()
126145

127-
if err2 != nil {
128-
return nil, err2
146+
_, err = r.db.ExecContext(ctx, query, userID, chatRoomID)
147+
if err != nil {
148+
slog.Error("Error joining chat room, db execcontext: ", err)
149+
return nil, err
129150
}
151+
152+
// Commit transaction
153+
if err = tx.Commit(); err != nil {
154+
slog.Error("Transaction commit failed: ", err)
155+
return nil, err
156+
}
157+
130158
return chatRoom, nil
131159

132160
}
@@ -167,21 +195,6 @@ func (r *repository) FindChatRoomList(ctx context.Context) ([]*ChatRoom, error)
167195

168196
}
169197

170-
func (r *repository) SaveMessage(ctx context.Context, message *Message) error {
171-
query := `
172-
INSERT INTO messages (id, chat_room_id, sender_id, content, media_url, created_at, read_at, deleted_by_user_id)
173-
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
174-
`
175-
176-
_, err := r.db.ExecContext(ctx, query, message.ID, message.ChatRoomID, message.SenderID, message.Content, message.MediaURL, message.CreatedAt, message.ReadAt, message.DeletedByUserID)
177-
if err != nil {
178-
log.Printf("Problem occured related to saving the message into the db, err: %v", err)
179-
return err
180-
}
181-
182-
return nil
183-
}
184-
185198
func (r *repository) GetPaginatedMessages(ctx context.Context, chatRoomID uuid.UUID, cursor *time.Time, pageSize int) ([]Message, error) {
186199
query := `
187200
SELECT id, chat_room_id, sender_id, content, media_url, created_at, read_at, deleted_by_user_id

frontend/src/hooks/useWebSocketConnection.ts

-2
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ export const useWebSocketConnection = (chatRoomId: string) => {
6464
img: serverMessage.media_url || undefined,
6565
};
6666

67-
console.log('Receiving messages..', receivedMessage);
68-
6967
addMessage(receivedMessage);
7068
};
7169

0 commit comments

Comments
 (0)