diff --git a/db/migrations/000019_create_chat_room_message.up.sql b/db/migrations/000019_create_chat_room_message.up.sql index af48d1f9..fd3263aa 100644 --- a/db/migrations/000019_create_chat_room_message.up.sql +++ b/db/migrations/000019_create_chat_room_message.up.sql @@ -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) ); \ No newline at end of file diff --git a/internal/chat/room.go b/internal/chat/room.go index ba6d141a..3ac54f59 100644 --- a/internal/chat/room.go +++ b/internal/chat/room.go @@ -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), diff --git a/internal/domain/chat/model.go b/internal/domain/chat/model.go index 41be98b3..110de467 100644 --- a/internal/domain/chat/model.go +++ b/internal/domain/chat/model.go @@ -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"` @@ -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"` @@ -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"` } diff --git a/internal/domain/chat/view.go b/internal/domain/chat/view.go index ecf9f184..0936d1a3 100644 --- a/internal/domain/chat/view.go +++ b/internal/domain/chat/view.go @@ -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, } } diff --git a/internal/infra/database/gen/chats.sql.go b/internal/infra/database/gen/chats.sql.go index 62125419..fdc8edde 100644 --- a/internal/infra/database/gen/chats.sql.go +++ b/internal/infra/database/gen/chats.sql.go @@ -27,7 +27,7 @@ type CreateRoomParams struct { } type CreateRoomRow struct { - ID int64 + ID int32 Name string RoomType string CreatedAt time.Time @@ -69,7 +69,7 @@ type FindMessageByRoomIDParams struct { } type FindMessageByRoomIDRow struct { - ID int64 + ID int32 UserID int64 RoomID int64 MessageType string @@ -120,14 +120,14 @@ WHERE ` type FindRoomByIDRow struct { - ID int64 + ID int32 Name string RoomType string CreatedAt time.Time UpdatedAt time.Time } -func (q *Queries) FindRoomByID(ctx context.Context, id sql.NullInt64) (FindRoomByIDRow, error) { +func (q *Queries) FindRoomByID(ctx context.Context, id sql.NullInt32) (FindRoomByIDRow, error) { row := q.db.QueryRowContext(ctx, findRoomByID, id) var i FindRoomByIDRow err := row.Scan( @@ -144,10 +144,9 @@ const joinRoom = `-- 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 ` type JoinRoomParams struct { @@ -156,11 +155,10 @@ type JoinRoomParams struct { } type JoinRoomRow struct { - ID int64 - UserID int64 - RoomID int64 - CreatedAt time.Time - UpdatedAt time.Time + ID int32 + UserID int64 + RoomID int64 + JoinedAt time.Time } func (q *Queries) JoinRoom(ctx context.Context, arg JoinRoomParams) (JoinRoomRow, error) { @@ -170,8 +168,7 @@ func (q *Queries) JoinRoom(ctx context.Context, arg JoinRoomParams) (JoinRoomRow &i.ID, &i.UserID, &i.RoomID, - &i.CreatedAt, - &i.UpdatedAt, + &i.JoinedAt, ) return i, err } @@ -179,7 +176,7 @@ func (q *Queries) JoinRoom(ctx context.Context, arg JoinRoomParams) (JoinRoomRow const leaveRoom = `-- name: LeaveRoom :exec UPDATE user_chat_rooms -SET deleted_at = NOW() +SET left_at = NOW() WHERE user_id = $1 AND room_id = $2 ` @@ -214,7 +211,7 @@ type WriteMessageParams struct { } type WriteMessageRow struct { - ID int64 + ID int32 UserID int64 RoomID int64 MessageType string diff --git a/internal/infra/database/gen/models.go b/internal/infra/database/gen/models.go index 94074e3f..0bb2fe3c 100644 --- a/internal/infra/database/gen/models.go +++ b/internal/infra/database/gen/models.go @@ -30,7 +30,7 @@ type Breed struct { } type ChatMessage struct { - ID int64 + ID int32 UserID int64 RoomID int64 MessageType string @@ -41,7 +41,7 @@ type ChatMessage struct { } type ChatRoom struct { - ID int64 + ID int32 Name string RoomType string CreatedAt time.Time @@ -159,23 +159,12 @@ type User struct { ProfileImageID sql.NullInt64 } -type UserChatMessage struct { - ID int64 - UserID int64 - MessageID int64 - RoomID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt sql.NullTime -} - type UserChatRoom struct { - ID int64 - UserID int64 - RoomID int64 - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt sql.NullTime + ID int32 + UserID int64 + RoomID int64 + JoinedAt time.Time + LeftAt time.Time } type VCondition struct { diff --git a/internal/service/chat_service.go b/internal/service/chat_service.go index 0ceee6f7..f3f78131 100644 --- a/internal/service/chat_service.go +++ b/internal/service/chat_service.go @@ -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 } diff --git a/queries/chats.sql b/queries/chats.sql index 1a1e403d..8a0d1493 100644 --- a/queries/chats.sql +++ b/queries/chats.sql @@ -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;