forked from mtusi2026/mtusi2026lab6
161 lines
3.3 KiB
Go
161 lines
3.3 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: messages.sql
|
|
|
|
package sqlcgen
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const createMessage = `-- name: CreateMessage :one
|
|
INSERT INTO messages (chat_id, sender_id, text)
|
|
SELECT $1, $2, $3
|
|
WHERE EXISTS (
|
|
SELECT 1
|
|
FROM chat_members
|
|
WHERE chat_id = $1
|
|
AND user_id = $2
|
|
)
|
|
RETURNING id, chat_id, sender_id, text, created_at
|
|
`
|
|
|
|
type CreateMessageParams struct {
|
|
ChatID uuid.UUID
|
|
SenderID uuid.UUID
|
|
Text string
|
|
}
|
|
|
|
func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error) {
|
|
row := q.db.QueryRow(ctx, createMessage, arg.ChatID, arg.SenderID, arg.Text)
|
|
var i Message
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ChatID,
|
|
&i.SenderID,
|
|
&i.Text,
|
|
&i.CreatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const isChatMember = `-- name: IsChatMember :one
|
|
SELECT EXISTS (
|
|
SELECT 1
|
|
FROM chat_members
|
|
WHERE chat_id = $1
|
|
AND user_id = $2
|
|
) AS is_member
|
|
`
|
|
|
|
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 is_member bool
|
|
err := row.Scan(&is_member)
|
|
return is_member, err
|
|
}
|
|
|
|
const listMessagesForChat = `-- name: ListMessagesForChat :many
|
|
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
|
FROM messages m
|
|
JOIN chat_members cm
|
|
ON cm.chat_id = m.chat_id
|
|
AND cm.user_id = $1
|
|
WHERE m.chat_id = $2
|
|
ORDER BY m.created_at, m.id
|
|
LIMIT $3
|
|
`
|
|
|
|
type ListMessagesForChatParams struct {
|
|
CallerID uuid.UUID
|
|
ChatID uuid.UUID
|
|
RowLimit int32
|
|
}
|
|
|
|
func (q *Queries) ListMessagesForChat(ctx context.Context, arg ListMessagesForChatParams) ([]Message, error) {
|
|
rows, err := q.db.Query(ctx, listMessagesForChat, arg.CallerID, arg.ChatID, arg.RowLimit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Message{}
|
|
for rows.Next() {
|
|
var i Message
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ChatID,
|
|
&i.SenderID,
|
|
&i.Text,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listMessagesForChatAfter = `-- name: ListMessagesForChatAfter :many
|
|
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at
|
|
FROM messages m
|
|
JOIN chat_members cm
|
|
ON cm.chat_id = m.chat_id
|
|
AND cm.user_id = $1
|
|
WHERE m.chat_id = $2
|
|
AND (m.created_at, m.id) > ($3::timestamptz, $4::uuid)
|
|
ORDER BY m.created_at, m.id
|
|
LIMIT $5
|
|
`
|
|
|
|
type ListMessagesForChatAfterParams struct {
|
|
CallerID uuid.UUID
|
|
ChatID uuid.UUID
|
|
AfterCreatedAt time.Time
|
|
AfterID uuid.UUID
|
|
RowLimit int32
|
|
}
|
|
|
|
func (q *Queries) ListMessagesForChatAfter(ctx context.Context, arg ListMessagesForChatAfterParams) ([]Message, error) {
|
|
rows, err := q.db.Query(ctx, listMessagesForChatAfter,
|
|
arg.CallerID,
|
|
arg.ChatID,
|
|
arg.AfterCreatedAt,
|
|
arg.AfterID,
|
|
arg.RowLimit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []Message{}
|
|
for rows.Next() {
|
|
var i Message
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ChatID,
|
|
&i.SenderID,
|
|
&i.Text,
|
|
&i.CreatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|