Skip to content

Commit

Permalink
feat: change UserChatRooms column
Browse files Browse the repository at this point in the history
  • Loading branch information
barabobBOB committed Jun 2, 2024
1 parent 92da221 commit 282b0b3
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 62 deletions.
5 changes: 2 additions & 3 deletions db/migrations/000019_create_chat_room_message.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ CREATE TABLE user_chat_rooms (
id SERIAL PRIMARY KEY,
user_id BIGINT NOT NULL,
room_id BIGINT NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at TIMESTAMPTZ,
joined_at TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
left_at TIMESTAMPTZ,
FOREIGN KEY (room_id) REFERENCES chat_rooms(id)
);
2 changes: 1 addition & 1 deletion internal/chat/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewRoom(name string, roomType chat.RoomType, roomService *service.ChatServi
}

return &Room{
ID: row.ID,
ID: int64(row.ID),
Name: row.Name,
RoomType: row.RoomType,
clients: make(map[*Client]bool),
Expand Down
15 changes: 7 additions & 8 deletions internal/domain/chat/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const (
)

type Room struct {
ID int64 `field:"id" json:"id"`
ID int32 `field:"id" json:"id"`
Name string `field:"name" json:"name"`
RoomType RoomType `field:"RoomType" json:"RoomType"`
CreatedAt time.Time `field:"createdAt" json:"createdAt"`
Expand All @@ -27,7 +27,7 @@ type Room struct {
}

type Message struct {
ID int64 `field:"id" json:"id"`
ID int32 `field:"id" json:"id"`
UserID int64 `field:"userID" json:"userID"`
RoomID int64 `field:"roomID" json:"roomID"`
MessageType MessageType `field:"messageType" json:"messageType"`
Expand All @@ -38,10 +38,9 @@ type Message struct {
}

type UserChatRoom struct {
ID int64 `field:"id" json:"id"`
UserID int64 `field:"userID" json:"userID"`
RoomID int64 `field:"roomID" json:"roomID"`
CreatedAt time.Time `field:"createdAt" json:"createdAt"`
UpdatedAt time.Time `field:"updatedAt" json:"updatedAt"`
DeletedAt time.Time `field:"deletedAt" json:"deletedAt"`
ID int32 `field:"id" json:"id"`
UserID int64 `field:"userID" json:"userID"`
RoomID int64 `field:"roomID" json:"roomID"`
JoinedAt time.Time `field:"joinedAt" json:"joinedAt"`
LeftAt time.Time `field:"leftAt" json:"leftAt"`
}
14 changes: 6 additions & 8 deletions internal/domain/chat/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,16 @@ import (
)

type JoinRoomView struct {
UserID int64
RoomID int64
CreatedAt time.Time
UpdatedAt time.Time
UserID int64
RoomID int64
JoinedAt time.Time
}

func ToJoinRoomView(row databasegen.JoinRoomRow) *JoinRoomView {
return &JoinRoomView{
UserID: row.UserID,
RoomID: row.RoomID,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
UserID: row.UserID,
RoomID: row.RoomID,
JoinedAt: row.JoinedAt,
}
}

Expand Down
31 changes: 14 additions & 17 deletions internal/infra/database/gen/chats.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 7 additions & 18 deletions internal/infra/database/gen/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/service/chat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (s *ChatService) SaveMessage(

// 채팅방 목록 조회
func (s *ChatService) FindRoomByID(ctx context.Context, roomID *int64) (*chat.Room, error) {
row, err := databasegen.New(s.conn).FindRoomByID(ctx, utils.Int64PtrToNullInt64(roomID))
row, err := databasegen.New(s.conn).FindRoomByID(ctx, utils.Int64PtrToNullInt32(roomID))
if err != nil {
return nil, err
}
Expand Down
10 changes: 4 additions & 6 deletions queries/chats.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,19 @@ FROM
WHERE
(chat_rooms.id = sqlc.narg('id'))
AND (chat_rooms.deleted_at IS NULL);
;

-- name: JoinRoom :one
INSERT INTO user_chat_rooms
(user_id,
room_id,
created_at,
updated_at)
VALUES ($1, $2, NOW(), NOW())
RETURNING id, user_id, room_id, created_at, updated_at;
joined_at)
VALUES ($1, $2, NOW())
RETURNING id, user_id, room_id, joined_at;

-- name: LeaveRoom :exec
UPDATE
user_chat_rooms
SET deleted_at = NOW()
SET left_at = NOW()
WHERE user_id = $1
AND room_id = $2;

Expand Down

0 comments on commit 282b0b3

Please sign in to comment.