feat: port lab4 implementation
This commit is contained in:
@@ -0,0 +1,232 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: chats.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const addChatMembers = `-- name: AddChatMembers :exec
|
||||
INSERT INTO chat_members (
|
||||
chat_id,
|
||||
user_id
|
||||
)
|
||||
SELECT
|
||||
$1::uuid,
|
||||
unnest($2::uuid[])
|
||||
`
|
||||
|
||||
type AddChatMembersParams struct {
|
||||
ChatID uuid.UUID
|
||||
UserIds []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) AddChatMembers(ctx context.Context, arg AddChatMembersParams) error {
|
||||
_, err := q.db.Exec(ctx, addChatMembers, arg.ChatID, arg.UserIds)
|
||||
return err
|
||||
}
|
||||
|
||||
const createChat = `-- name: CreateChat :one
|
||||
INSERT INTO chats (
|
||||
id,
|
||||
title
|
||||
) VALUES (
|
||||
$1,
|
||||
$2
|
||||
)
|
||||
RETURNING id, title, created_at
|
||||
`
|
||||
|
||||
type CreateChatParams struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateChat(ctx context.Context, arg CreateChatParams) (Chat, error) {
|
||||
row := q.db.QueryRow(ctx, createChat, arg.ID, arg.Title)
|
||||
var i Chat
|
||||
err := row.Scan(&i.ID, &i.Title, &i.CreatedAt)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getChat = `-- name: GetChat :one
|
||||
SELECT
|
||||
c.id, c.title, c.created_at,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
WHERE c.id = $1
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM chat_members m
|
||||
WHERE m.chat_id = c.id
|
||||
AND m.user_id = $2
|
||||
)
|
||||
`
|
||||
|
||||
type GetChatParams struct {
|
||||
ID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
}
|
||||
|
||||
type GetChatRow struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
CreatedAt time.Time
|
||||
MemberIds []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) GetChat(ctx context.Context, arg GetChatParams) (GetChatRow, error) {
|
||||
row := q.db.QueryRow(ctx, getChat, arg.ID, arg.UserID)
|
||||
var i GetChatRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.CreatedAt,
|
||||
&i.MemberIds,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const isChatMember = `-- name: IsChatMember :one
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = $1
|
||||
AND user_id = $2
|
||||
)
|
||||
`
|
||||
|
||||
type IsChatMemberParams struct {
|
||||
ChatID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) IsChatMember(ctx context.Context, arg IsChatMemberParams) (bool, error) {
|
||||
row := q.db.QueryRow(ctx, isChatMember, arg.ChatID, arg.UserID)
|
||||
var exists bool
|
||||
err := row.Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
const listChatsAfter = `-- name: ListChatsAfter :many
|
||||
SELECT
|
||||
c.id, c.title, c.created_at,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1
|
||||
AND (c.created_at, c.id) > (
|
||||
$2::timestamptz,
|
||||
$3::uuid
|
||||
)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type ListChatsAfterParams struct {
|
||||
UserID uuid.UUID
|
||||
AfterCreatedAt time.Time
|
||||
AfterID uuid.UUID
|
||||
Lim int
|
||||
}
|
||||
|
||||
type ListChatsAfterRow struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
CreatedAt time.Time
|
||||
MemberIds []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) ListChatsAfter(ctx context.Context, arg ListChatsAfterParams) ([]ListChatsAfterRow, error) {
|
||||
rows, err := q.db.Query(ctx, listChatsAfter,
|
||||
arg.UserID,
|
||||
arg.AfterCreatedAt,
|
||||
arg.AfterID,
|
||||
arg.Lim,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListChatsAfterRow{}
|
||||
for rows.Next() {
|
||||
var i ListChatsAfterRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.CreatedAt,
|
||||
&i.MemberIds,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listChatsFirstPage = `-- name: ListChatsFirstPage :many
|
||||
SELECT
|
||||
c.id, c.title, c.created_at,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListChatsFirstPageParams struct {
|
||||
UserID uuid.UUID
|
||||
Lim int
|
||||
}
|
||||
|
||||
type ListChatsFirstPageRow struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
CreatedAt time.Time
|
||||
MemberIds []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) ListChatsFirstPage(ctx context.Context, arg ListChatsFirstPageParams) ([]ListChatsFirstPageRow, error) {
|
||||
rows, err := q.db.Query(ctx, listChatsFirstPage, arg.UserID, arg.Lim)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListChatsFirstPageRow{}
|
||||
for rows.Next() {
|
||||
var i ListChatsFirstPageRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.CreatedAt,
|
||||
&i.MemberIds,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
Reference in New Issue
Block a user