diff --git a/cmd/server/handler/chat_handler.go b/cmd/server/handler/chat_handler.go index 65fb3421..e9ee2f1e 100644 --- a/cmd/server/handler/chat_handler.go +++ b/cmd/server/handler/chat_handler.go @@ -30,7 +30,7 @@ func NewChatHandler( // @Tags chat // @Accept json // @Produce json -// @Param roomID path int true "채팅방 ID" +// @Param roomID path string true "채팅방 ID" // @Security FirebaseAuth // @Success 200 {object} domain.RoomSimpleInfo // @Router /chat/rooms/{roomID} [get] @@ -45,7 +45,7 @@ func (h ChatHandler) FindRoomByID(c echo.Context) error { return c.JSON(err.StatusCode, err) } - res, err := h.chatService.FindChatRoomByUIDAndRoomID(c.Request().Context(), foundUser.FirebaseUID, int64(*roomID)) + res, err := h.chatService.FindChatRoomByUIDAndRoomID(c.Request().Context(), foundUser.FirebaseUID, roomID) if err != nil { return c.JSON(err.StatusCode, err) } @@ -93,7 +93,7 @@ func (h ChatHandler) CreateRoom(c echo.Context) error { // @Tags chat // @Accept json // @Produce json -// @Param roomID path int true "채팅방 ID" +// @Param roomID path string true "채팅방 ID" // @Security FirebaseAuth // @Success 200 {object} domain.JoinRoomsView // @Router /chat/rooms/{roomID}/join [post] @@ -108,7 +108,7 @@ func (h ChatHandler) JoinChatRoom(c echo.Context) error { return c.JSON(err.StatusCode, err) } - res, err := h.chatService.JoinRoom(c.Request().Context(), int64(*roomID), foundUser.FirebaseUID) + res, err := h.chatService.JoinRoom(c.Request().Context(), roomID, foundUser.FirebaseUID) if err != nil { return c.JSON(err.StatusCode, err) } @@ -122,7 +122,7 @@ func (h ChatHandler) JoinChatRoom(c echo.Context) error { // @Tags chat // @Accept json // @Produce json -// @Param roomID path int true "채팅방 ID" +// @Param roomID path string true "채팅방 ID" // @Security FirebaseAuth // @Success 200 // @Router /chat/rooms/{roomID}/leave [post] @@ -137,7 +137,7 @@ func (h ChatHandler) LeaveChatRoom(c echo.Context) error { return c.JSON(err.StatusCode, err) } - res := h.chatService.LeaveRoom(c.Request().Context(), int64(*roomID), foundUser.FirebaseUID) + res := h.chatService.LeaveRoom(c.Request().Context(), roomID, foundUser.FirebaseUID) return c.JSON(http.StatusOK, res) } @@ -169,7 +169,7 @@ func (h ChatHandler) FindAllRooms(c echo.Context) error { // @Tags chat // @Accept json // @Produce json -// @Param roomID path int true "채팅방 ID" +// @Param roomID path string true "채팅방 ID" // @Param prev query int false "이전 페이지" // @Param next query int false "다음 페이지" // @Param size query int false "페이지 사이즈" default(30) @@ -189,7 +189,7 @@ func (h ChatHandler) FindMessagesByRoomID(c echo.Context) error { res, err := h.chatService.FindChatRoomMessagesByRoomID( c.Request().Context(), - int64(*roomID), + roomID, int64(prev), int64(next), int64(limit), diff --git a/internal/infra/database/gen/chats.sql.go b/internal/infra/database/gen/chats.sql.go index 04a85c70..4a587389 100644 --- a/internal/infra/database/gen/chats.sql.go +++ b/internal/infra/database/gen/chats.sql.go @@ -16,9 +16,9 @@ import ( const createRoom = `-- name: CreateRoom :one INSERT INTO chat_rooms (name, -room_type, -created_at, -updated_at) + room_type, + created_at, + updated_at) VALUES ($1, $2, NOW(), NOW()) RETURNING id, name, room_type, created_at, updated_at ` @@ -62,11 +62,10 @@ func (q *Queries) DeleteRoom(ctx context.Context, id uuid.UUID) error { } const existsUserInRoom = `-- name: ExistsUserInRoom :one -SELECT EXISTS ( - SELECT 1 - FROM user_chat_rooms - WHERE room_id = $1 AND user_id = $2 -) +SELECT EXISTS (SELECT 1 + FROM user_chat_rooms + WHERE room_id = $1 + AND user_id = $2) ` type ExistsUserInRoomParams struct { @@ -81,25 +80,124 @@ func (q *Queries) ExistsUserInRoom(ctx context.Context, arg ExistsUserInRoomPara return exists, err } +const findAllUserChatRoomsByUserUID = `-- name: FindAllUserChatRoomsByUserUID :many +SELECT user_chat_rooms.id, + user_chat_rooms.user_id, + user_chat_rooms.room_id, + user_chat_rooms.joined_at, + users.email, + users.nickname, + users.fullname, + media.url AS profile_image_url, + users.fb_provider_type, + users.fb_uid, + users.created_at, + users.updated_at, + chat_rooms.id AS chat_room_id, + chat_rooms.name AS chat_room_name, + chat_rooms.room_type AS chat_room_type, + chat_rooms.created_at AS chat_room_created_at, + chat_rooms.updated_at AS chat_room_updated_at +FROM user_chat_rooms + JOIN users + ON users.id = user_chat_rooms.user_id + JOIN chat_rooms + ON chat_rooms.id = user_chat_rooms.room_id + LEFT OUTER JOIN media + ON users.profile_image_id = media.id +WHERE user_chat_rooms.left_at IS NULL + AND chat_rooms.deleted_at IS NULL + AND user_chat_rooms.user_id = $1 +` + +type FindAllUserChatRoomsByUserUIDRow struct { + ID uuid.UUID + UserID uuid.UUID + RoomID uuid.UUID + JoinedAt time.Time + Email string + Nickname string + Fullname string + ProfileImageUrl sql.NullString + FbProviderType sql.NullString + FbUid sql.NullString + CreatedAt time.Time + UpdatedAt time.Time + ChatRoomID uuid.UUID + ChatRoomName string + ChatRoomType string + ChatRoomCreatedAt time.Time + ChatRoomUpdatedAt time.Time +} + +func (q *Queries) FindAllUserChatRoomsByUserUID(ctx context.Context, userID uuid.UUID) ([]FindAllUserChatRoomsByUserUIDRow, error) { + rows, err := q.db.QueryContext(ctx, findAllUserChatRoomsByUserUID, userID) + if err != nil { + return nil, err + } + defer rows.Close() + var items []FindAllUserChatRoomsByUserUIDRow + for rows.Next() { + var i FindAllUserChatRoomsByUserUIDRow + if err := rows.Scan( + &i.ID, + &i.UserID, + &i.RoomID, + &i.JoinedAt, + &i.Email, + &i.Nickname, + &i.Fullname, + &i.ProfileImageUrl, + &i.FbProviderType, + &i.FbUid, + &i.CreatedAt, + &i.UpdatedAt, + &i.ChatRoomID, + &i.ChatRoomName, + &i.ChatRoomType, + &i.ChatRoomCreatedAt, + &i.ChatRoomUpdatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + const findMessageByRoomID = `-- name: FindMessageByRoomID :many -SELECT - id, - user_id, - room_id, - message_type, - content -FROM - chat_messages -WHERE - room_id = $3 -ORDER BY created_at DESC -LIMIT $1 OFFSET $2 +SELECT id, + user_id, + room_id, + message_type, + content, + created_at +FROM chat_messages +WHERE chat_messages.deleted_at IS NULL + AND room_id = $2 + AND (($3::uuid IS NOT NULL AND $4::uuid IS NOT NULL AND + id > $3::uuid AND id < $4::uuid) + OR + ($3::uuid IS NOT NULL AND $4::uuid IS NULL AND + id < $3::uuid) + OR + ($3::uuid IS NULL AND $4::uuid IS NOT NULL AND + id > $4::uuid)) +ORDER BY chat_messages.created_at ASC +LIMIT $1 ` type FindMessageByRoomIDParams struct { Limit int32 - Offset int32 - RoomID uuid.NullUUID + RoomID uuid.UUID + Prev uuid.NullUUID + Next uuid.NullUUID } type FindMessageByRoomIDRow struct { @@ -108,10 +206,16 @@ type FindMessageByRoomIDRow struct { RoomID uuid.UUID MessageType string Content string + CreatedAt time.Time } func (q *Queries) FindMessageByRoomID(ctx context.Context, arg FindMessageByRoomIDParams) ([]FindMessageByRoomIDRow, error) { - rows, err := q.db.QueryContext(ctx, findMessageByRoomID, arg.Limit, arg.Offset, arg.RoomID) + rows, err := q.db.QueryContext(ctx, findMessageByRoomID, + arg.Limit, + arg.RoomID, + arg.Prev, + arg.Next, + ) if err != nil { return nil, err } @@ -125,6 +229,7 @@ func (q *Queries) FindMessageByRoomID(ctx context.Context, arg FindMessageByRoom &i.RoomID, &i.MessageType, &i.Content, + &i.CreatedAt, ); err != nil { return nil, err } @@ -139,21 +244,28 @@ func (q *Queries) FindMessageByRoomID(ctx context.Context, arg FindMessageByRoom return items, nil } -const findRoomByID = `-- name: FindRoomByID :one -SELECT - id, - name, - room_type, - created_at, - updated_at -FROM - chat_rooms -WHERE - (chat_rooms.id = $1) - AND (chat_rooms.deleted_at IS NULL) +const findRoomByIDAndUserID = `-- name: FindRoomByIDAndUserID :one +SELECT id, + name, + room_type, + created_at, + updated_at +FROM chat_rooms +WHERE chat_rooms.deleted_at IS NULL + AND (chat_rooms.id = $1) + AND EXISTS (SELECT 1 + FROM user_chat_rooms + WHERE user_id = $2 + AND room_id = chat_rooms.id + AND left_at IS NULL) ` -type FindRoomByIDRow struct { +type FindRoomByIDAndUserIDParams struct { + ID uuid.UUID + UserID uuid.UUID +} + +type FindRoomByIDAndUserIDRow struct { ID uuid.UUID Name string RoomType string @@ -161,9 +273,9 @@ type FindRoomByIDRow struct { UpdatedAt time.Time } -func (q *Queries) FindRoomByID(ctx context.Context, id uuid.NullUUID) (FindRoomByIDRow, error) { - row := q.db.QueryRowContext(ctx, findRoomByID, id) - var i FindRoomByIDRow +func (q *Queries) FindRoomByIDAndUserID(ctx context.Context, arg FindRoomByIDAndUserIDParams) (FindRoomByIDAndUserIDRow, error) { + row := q.db.QueryRowContext(ctx, findRoomByIDAndUserID, arg.ID, arg.UserID) + var i FindRoomByIDAndUserIDRow err := row.Scan( &i.ID, &i.Name, @@ -174,103 +286,11 @@ func (q *Queries) FindRoomByID(ctx context.Context, id uuid.NullUUID) (FindRoomB return i, err } -const findUserChatRooms = `-- name: FindUserChatRooms :many -SELECT - user_chat_rooms.id, - user_chat_rooms.user_id, - user_chat_rooms.room_id, - user_chat_rooms.joined_at, - users.email, - users.nickname, - users.fullname, - media.url AS profile_image_url, - users.fb_provider_type, - users.fb_uid, - users.created_at, - users.updated_at, - chat_rooms.id AS chat_room_id, - chat_rooms.name AS chat_room_name, - chat_rooms.room_type AS chat_room_type, - chat_rooms.created_at AS chat_room_created_at, - chat_rooms.updated_at AS chat_room_updated_at -FROM - user_chat_rooms - JOIN users - ON users.id = user_chat_rooms.user_id - JOIN chat_rooms - ON chat_rooms.id = user_chat_rooms.room_id - LEFT OUTER JOIN media - ON users.profile_image_id = media.id -WHERE - user_chat_rooms.left_at IS NULL -` - -type FindUserChatRoomsRow struct { - ID uuid.UUID - UserID uuid.UUID - RoomID uuid.UUID - JoinedAt time.Time - Email string - Nickname string - Fullname string - ProfileImageUrl sql.NullString - FbProviderType sql.NullString - FbUid sql.NullString - CreatedAt time.Time - UpdatedAt time.Time - ChatRoomID uuid.UUID - ChatRoomName string - ChatRoomType string - ChatRoomCreatedAt time.Time - ChatRoomUpdatedAt time.Time -} - -func (q *Queries) FindUserChatRooms(ctx context.Context) ([]FindUserChatRoomsRow, error) { - rows, err := q.db.QueryContext(ctx, findUserChatRooms) - if err != nil { - return nil, err - } - defer rows.Close() - var items []FindUserChatRoomsRow - for rows.Next() { - var i FindUserChatRoomsRow - if err := rows.Scan( - &i.ID, - &i.UserID, - &i.RoomID, - &i.JoinedAt, - &i.Email, - &i.Nickname, - &i.Fullname, - &i.ProfileImageUrl, - &i.FbProviderType, - &i.FbUid, - &i.CreatedAt, - &i.UpdatedAt, - &i.ChatRoomID, - &i.ChatRoomName, - &i.ChatRoomType, - &i.ChatRoomCreatedAt, - &i.ChatRoomUpdatedAt, - ); err != nil { - return nil, err - } - items = append(items, i) - } - if err := rows.Close(); err != nil { - return nil, err - } - if err := rows.Err(); err != nil { - return nil, err - } - return items, nil -} - const joinRoom = `-- name: JoinRoom :one INSERT INTO user_chat_rooms -(user_id, -room_id, -joined_at) +(user_id, + room_id, + joined_at) VALUES ($1, $2, NOW()) RETURNING id, user_id, room_id, joined_at ` @@ -299,12 +319,31 @@ func (q *Queries) JoinRoom(ctx context.Context, arg JoinRoomParams) (JoinRoomRow return i, err } +const joinRooms = `-- name: JoinRooms :exec +INSERT INTO user_chat_rooms +(user_id, + room_id, + joined_at) +VALUES ($1, $2, NOW()) +RETURNING id, user_id, room_id, joined_at +` + +type JoinRoomsParams struct { + UserID uuid.UUID + RoomID uuid.UUID +} + +func (q *Queries) JoinRooms(ctx context.Context, arg JoinRoomsParams) error { + _, err := q.db.ExecContext(ctx, joinRooms, arg.UserID, arg.RoomID) + return err +} + const leaveRoom = `-- name: LeaveRoom :exec -UPDATE +UPDATE user_chat_rooms SET left_at = NOW() WHERE user_id = $1 -AND room_id = $2 + AND room_id = $2 ` type LeaveRoomParams struct { @@ -318,11 +357,10 @@ func (q *Queries) LeaveRoom(ctx context.Context, arg LeaveRoomParams) error { } const userExistsInRoom = `-- name: UserExistsInRoom :one -SELECT EXISTS ( - SELECT 1 - FROM user_chat_rooms - WHERE room_id = $1 AND left_at IS NULL -) +SELECT EXISTS (SELECT 1 + FROM user_chat_rooms + WHERE room_id = $1 + AND left_at IS NULL) ` func (q *Queries) UserExistsInRoom(ctx context.Context, roomID uuid.UUID) (bool, error) { @@ -331,52 +369,3 @@ func (q *Queries) UserExistsInRoom(ctx context.Context, roomID uuid.UUID) (bool, err := row.Scan(&exists) return exists, err } - -const writeMessage = `-- name: WriteMessage :one -INSERT INTO chat_messages -(user_id, -room_id, -message_type, -content, -created_at, -updated_at) -VALUES ($1, $2, $3, $4, NOW(), NOW()) -RETURNING id, user_id, room_id, message_type, content, created_at, updated_at -` - -type WriteMessageParams struct { - UserID uuid.UUID - RoomID uuid.UUID - MessageType string - Content string -} - -type WriteMessageRow struct { - ID uuid.UUID - UserID uuid.UUID - RoomID uuid.UUID - MessageType string - Content string - CreatedAt time.Time - UpdatedAt time.Time -} - -func (q *Queries) WriteMessage(ctx context.Context, arg WriteMessageParams) (WriteMessageRow, error) { - row := q.db.QueryRowContext(ctx, writeMessage, - arg.UserID, - arg.RoomID, - arg.MessageType, - arg.Content, - ) - var i WriteMessageRow - err := row.Scan( - &i.ID, - &i.UserID, - &i.RoomID, - &i.MessageType, - &i.Content, - &i.CreatedAt, - &i.UpdatedAt, - ) - return i, err -} diff --git a/internal/service/chat_service.go b/internal/service/chat_service.go index b7f7c2f1..a6eabb9b 100644 --- a/internal/service/chat_service.go +++ b/internal/service/chat_service.go @@ -4,6 +4,8 @@ import ( "context" "errors" + "github.com/google/uuid" + pnd "github.com/pet-sitter/pets-next-door-api/api" utils "github.com/pet-sitter/pets-next-door-api/internal/common" "github.com/pet-sitter/pets-next-door-api/internal/domain/chat" @@ -52,8 +54,8 @@ func (s *ChatService) CreateRoom( } _, err3 := q.JoinRoom(ctx, databasegen.JoinRoomParams{ - UserID: int64(userData.ID), - RoomID: int64(row.ID), + UserID: userData.ID, + RoomID: row.ID, }) if err3 != nil { @@ -65,7 +67,7 @@ func (s *ChatService) CreateRoom( return chat.ToCreateRoom(row, chat.ToJoinUsers(userData)), nil } -func (s *ChatService) JoinRoom(ctx context.Context, roomID int64, fbUID string) (*chat.JoinRoom, *pnd.AppError) { +func (s *ChatService) JoinRoom(ctx context.Context, roomID uuid.UUID, fbUID string) (*chat.JoinRoom, *pnd.AppError) { userData, err := databasegen.New(s.conn).FindUser(ctx, databasegen.FindUserParams{ FbUid: utils.StrToNullStr(fbUID), }) @@ -81,7 +83,7 @@ func (s *ChatService) JoinRoom(ctx context.Context, roomID int64, fbUID string) if !exists { row, err := databasegen.New(s.conn).JoinRoom(ctx, databasegen.JoinRoomParams{ RoomID: roomID, - UserID: int64(userData.ID), + UserID: userData.ID, }) if err != nil { return nil, pnd.FromPostgresError(err) @@ -93,7 +95,7 @@ func (s *ChatService) JoinRoom(ctx context.Context, roomID int64, fbUID string) return nil, pnd.ErrBadRequest(errors.New("이미 참여중인 채팅방입니다")) } -func (s *ChatService) LeaveRoom(ctx context.Context, roomID int64, fbUID string) *pnd.AppError { +func (s *ChatService) LeaveRoom(ctx context.Context, roomID uuid.UUID, fbUID string) *pnd.AppError { userData, err := databasegen.New(s.conn).FindUser(ctx, databasegen.FindUserParams{ FbUid: utils.StrToNullStr(fbUID), }) @@ -103,7 +105,7 @@ func (s *ChatService) LeaveRoom(ctx context.Context, roomID int64, fbUID string) err = databasegen.New(s.conn).LeaveRoom(ctx, databasegen.LeaveRoomParams{ RoomID: roomID, - UserID: int64(userData.ID), + UserID: userData.ID, }) if err != nil { return pnd.FromPostgresError(err) @@ -114,7 +116,7 @@ func (s *ChatService) LeaveRoom(ctx context.Context, roomID int64, fbUID string) } if !exists { - err = databasegen.New(s.conn).DeleteRoom(ctx, int32(roomID)) + err = databasegen.New(s.conn).DeleteRoom(ctx, roomID) if err != nil { return pnd.FromPostgresError(err) } @@ -129,7 +131,7 @@ func (s *ChatService) FindAllByUserUID(ctx context.Context, fbUID string) (*chat if err != nil { return nil, pnd.FromPostgresError(err) } - rows, err := databasegen.New(s.conn).FindAllUserChatRoomsByUserUID(ctx, int64(userData.ID)) + rows, err := databasegen.New(s.conn).FindAllUserChatRoomsByUserUID(ctx, userData.ID) if err != nil { return nil, pnd.FromPostgresError(err) } @@ -138,7 +140,7 @@ func (s *ChatService) FindAllByUserUID(ctx context.Context, fbUID string) (*chat return chat.ToUserChatRoomsView(rows), nil } -func (s *ChatService) FindChatRoomByUIDAndRoomID(ctx context.Context, fbUID string, roomID int64) ( +func (s *ChatService) FindChatRoomByUIDAndRoomID(ctx context.Context, fbUID string, roomID uuid.NullUUID) ( *chat.RoomSimpleInfo, *pnd.AppError, ) { userData, err := databasegen.New(s.conn).FindUser(ctx, databasegen.FindUserParams{ @@ -148,7 +150,7 @@ func (s *ChatService) FindChatRoomByUIDAndRoomID(ctx context.Context, fbUID stri return nil, pnd.FromPostgresError(err) } - row, err := databasegen.New(s.conn).FindRoomByIDAndUserID(ctx, roomID, int64(userData.ID)) + row, err := databasegen.New(s.conn).FindRoomByIDAndUserID(ctx, roomID, userData.ID) if err != nil { return nil, pnd.FromPostgresError(err) } @@ -156,13 +158,13 @@ func (s *ChatService) FindChatRoomByUIDAndRoomID(ctx context.Context, fbUID stri return chat.ToUserChatRoomView(row), nil } -func (s *ChatService) FindChatRoomMessagesByRoomID(ctx context.Context, roomID, prev, next, limit int64) ( +func (s *ChatService) FindChatRoomMessagesByRoomID(ctx context.Context, roomID uuid.UUID, prev, next uuid.NullUUID, limit int64) ( *chat.MessageCursorView, *pnd.AppError, ) { - hasNext, hasPrev, rows, err := databasegen.New(s.conn).FindMessageByRoomID(ctx, databasegen.FindMessageByRoomIDParams{ + rows, err := databasegen.New(s.conn).FindMessageByRoomID(ctx, databasegen.FindMessageByRoomIDParams{ Prev: prev, Next: next, - Limit: limit, + Limit: int32(limit), RoomID: roomID, }) if err != nil { diff --git a/queries/chats.sql b/queries/chats.sql index 2bee764b..3692246f 100644 --- a/queries/chats.sql +++ b/queries/chats.sql @@ -1,111 +1,118 @@ -- name: CreateRoom :one INSERT INTO chat_rooms (name, -room_type, -created_at, -updated_at) + room_type, + created_at, + updated_at) VALUES ($1, $2, NOW(), NOW()) RETURNING id, name, room_type, created_at, updated_at; --- name: FindRoomByID :one -SELECT - id, - name, - room_type, - created_at, - updated_at -FROM +-- name: DeleteRoom :exec +UPDATE chat_rooms -WHERE - (chat_rooms.id = sqlc.narg('id')) - AND (chat_rooms.deleted_at IS NULL); +SET deleted_at = NOW() +WHERE id = $1; + +-- name: ExistsUserInRoom :one +SELECT EXISTS (SELECT 1 + FROM user_chat_rooms + WHERE room_id = $1 + AND user_id = $2); + + +-- name: FindMessageByRoomID :many +SELECT id, + user_id, + room_id, + message_type, + content, + created_at +FROM chat_messages +WHERE chat_messages.deleted_at IS NULL + AND room_id = $2 + AND ((sqlc.narg('prev')::uuid IS NOT NULL AND sqlc.narg('next')::uuid IS NOT NULL AND + id > sqlc.narg('prev')::uuid AND id < sqlc.narg('next')::uuid) + OR + (sqlc.narg('prev')::uuid IS NOT NULL AND sqlc.narg('next')::uuid IS NULL AND + id < sqlc.narg('prev')::uuid) + OR + (sqlc.narg('prev')::uuid IS NULL AND sqlc.narg('next')::uuid IS NOT NULL AND + id > sqlc.narg('next')::uuid)) +ORDER BY chat_messages.created_at ASC +LIMIT $1; + + +-- name: FindRoomByIDAndUserID :one +SELECT id, + name, + room_type, + created_at, + updated_at +FROM chat_rooms +WHERE chat_rooms.deleted_at IS NULL + AND (chat_rooms.id = $1) + AND EXISTS (SELECT 1 + FROM user_chat_rooms + WHERE user_id = $2 + AND room_id = chat_rooms.id + AND left_at IS NULL); + +-- name: FindAllUserChatRoomsByUserUID :many +SELECT user_chat_rooms.id, + user_chat_rooms.user_id, + user_chat_rooms.room_id, + user_chat_rooms.joined_at, + users.email, + users.nickname, + users.fullname, + media.url AS profile_image_url, + users.fb_provider_type, + users.fb_uid, + users.created_at, + users.updated_at, + chat_rooms.id AS chat_room_id, + chat_rooms.name AS chat_room_name, + chat_rooms.room_type AS chat_room_type, + chat_rooms.created_at AS chat_room_created_at, + chat_rooms.updated_at AS chat_room_updated_at +FROM user_chat_rooms + JOIN users + ON users.id = user_chat_rooms.user_id + JOIN chat_rooms + ON chat_rooms.id = user_chat_rooms.room_id + LEFT OUTER JOIN media + ON users.profile_image_id = media.id +WHERE user_chat_rooms.left_at IS NULL + AND chat_rooms.deleted_at IS NULL + AND user_chat_rooms.user_id = $1; -- name: JoinRoom :one INSERT INTO user_chat_rooms -(user_id, -room_id, -joined_at) +(user_id, + room_id, + joined_at) VALUES ($1, $2, NOW()) RETURNING id, user_id, room_id, joined_at; --- name: DeleteRoom :exec -UPDATE - chat_rooms -SET deleted_at = NOW() -WHERE id = $1; - --- name: LeaveRoom :exec -UPDATE - user_chat_rooms -SET left_at = NOW() -WHERE user_id = $1 -AND room_id = $2; --- name: WriteMessage :one -INSERT INTO chat_messages +-- name: JoinRooms :exec +INSERT INTO user_chat_rooms (user_id, -room_id, -message_type, -content, -created_at, -updated_at) -VALUES ($1, $2, $3, $4, NOW(), NOW()) -RETURNING id, user_id, room_id, message_type, content, created_at, updated_at; + room_id, + joined_at) +VALUES ($1, $2, NOW()) +RETURNING id, user_id, room_id, joined_at; --- name: FindMessageByRoomID :many -SELECT - id, - user_id, - room_id, - message_type, - content -FROM - chat_messages -WHERE - room_id = sqlc.narg('room_id') -ORDER BY created_at DESC -LIMIT $1 OFFSET $2; --- name: FindUserChatRooms :many -SELECT - user_chat_rooms.id, - user_chat_rooms.user_id, - user_chat_rooms.room_id, - user_chat_rooms.joined_at, - users.email, - users.nickname, - users.fullname, - media.url AS profile_image_url, - users.fb_provider_type, - users.fb_uid, - users.created_at, - users.updated_at, - chat_rooms.id AS chat_room_id, - chat_rooms.name AS chat_room_name, - chat_rooms.room_type AS chat_room_type, - chat_rooms.created_at AS chat_room_created_at, - chat_rooms.updated_at AS chat_room_updated_at -FROM +-- name: LeaveRoom :exec +UPDATE user_chat_rooms - JOIN users - ON users.id = user_chat_rooms.user_id - JOIN chat_rooms - ON chat_rooms.id = user_chat_rooms.room_id - LEFT OUTER JOIN media - ON users.profile_image_id = media.id -WHERE - user_chat_rooms.left_at IS NULL; - --- name: ExistsUserInRoom :one -SELECT EXISTS ( - SELECT 1 - FROM user_chat_rooms - WHERE room_id = $1 AND user_id = $2 -); +SET left_at = NOW() +WHERE user_id = $1 + AND room_id = $2; -- name: UserExistsInRoom :one -SELECT EXISTS ( - SELECT 1 - FROM user_chat_rooms - WHERE room_id = $1 AND left_at IS NULL -); +SELECT EXISTS (SELECT 1 + FROM user_chat_rooms + WHERE room_id = $1 + AND left_at IS NULL);