104 lines
3.1 KiB
Go
104 lines
3.1 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"time"
|
|
)
|
|
|
|
// ConvocationPanel is a single convocation panel embed+button entry.
|
|
type ConvocationPanel struct {
|
|
ID int64
|
|
GuildID string
|
|
EmbedTitle string
|
|
EmbedDesc string
|
|
EmbedColor string
|
|
ChannelID string
|
|
MessageID string
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
type ConvocationPanelRepo struct{ db *sql.DB }
|
|
|
|
func NewConvocationPanelRepo(db *sql.DB) *ConvocationPanelRepo {
|
|
return &ConvocationPanelRepo{db: db}
|
|
}
|
|
|
|
func (r *ConvocationPanelRepo) List(ctx context.Context, guildID string) ([]*ConvocationPanel, error) {
|
|
rows, err := r.db.QueryContext(ctx,
|
|
`SELECT id,guild_id,embed_title,embed_description,embed_color,channel_id,message_id,created_at,updated_at
|
|
FROM convocation_panels WHERE guild_id=? ORDER BY id`, guildID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var out []*ConvocationPanel
|
|
for rows.Next() {
|
|
p, err := scanConvocPanel(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, p)
|
|
}
|
|
return out, rows.Err()
|
|
}
|
|
|
|
func (r *ConvocationPanelRepo) GetByID(ctx context.Context, id int64) (*ConvocationPanel, error) {
|
|
row := r.db.QueryRowContext(ctx,
|
|
`SELECT id,guild_id,embed_title,embed_description,embed_color,channel_id,message_id,created_at,updated_at
|
|
FROM convocation_panels WHERE id=?`, id)
|
|
var p ConvocationPanel
|
|
err := row.Scan(&p.ID, &p.GuildID, &p.EmbedTitle, &p.EmbedDesc, &p.EmbedColor,
|
|
&p.ChannelID, &p.MessageID, &p.CreatedAt, &p.UpdatedAt)
|
|
if err == sql.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
return &p, err
|
|
}
|
|
|
|
func (r *ConvocationPanelRepo) Create(ctx context.Context, p *ConvocationPanel) error {
|
|
now := time.Now().UTC()
|
|
res, err := r.db.ExecContext(ctx,
|
|
`INSERT INTO convocation_panels(guild_id,embed_title,embed_description,embed_color,channel_id,message_id,created_at,updated_at)
|
|
VALUES(?,?,?,?,?,?,?,?)`,
|
|
p.GuildID, p.EmbedTitle, p.EmbedDesc, p.EmbedColor, p.ChannelID, p.MessageID, now, now)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
p.ID, _ = res.LastInsertId()
|
|
p.CreatedAt = now
|
|
p.UpdatedAt = now
|
|
return nil
|
|
}
|
|
|
|
func (r *ConvocationPanelRepo) Update(ctx context.Context, p *ConvocationPanel) error {
|
|
now := time.Now().UTC()
|
|
_, err := r.db.ExecContext(ctx,
|
|
`UPDATE convocation_panels SET embed_title=?,embed_description=?,embed_color=?,channel_id=?,updated_at=? WHERE id=?`,
|
|
p.EmbedTitle, p.EmbedDesc, p.EmbedColor, p.ChannelID, now, p.ID)
|
|
if err == nil {
|
|
p.UpdatedAt = now
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (r *ConvocationPanelRepo) SetMessage(ctx context.Context, id int64, messageID string) error {
|
|
_, err := r.db.ExecContext(ctx,
|
|
`UPDATE convocation_panels SET message_id=?,updated_at=? WHERE id=?`,
|
|
messageID, time.Now().UTC(), id)
|
|
return err
|
|
}
|
|
|
|
func (r *ConvocationPanelRepo) Delete(ctx context.Context, id int64) error {
|
|
_, err := r.db.ExecContext(ctx, `DELETE FROM convocation_panels WHERE id=?`, id)
|
|
return err
|
|
}
|
|
|
|
func scanConvocPanel(rows *sql.Rows) (*ConvocationPanel, error) {
|
|
var p ConvocationPanel
|
|
err := rows.Scan(&p.ID, &p.GuildID, &p.EmbedTitle, &p.EmbedDesc, &p.EmbedColor,
|
|
&p.ChannelID, &p.MessageID, &p.CreatedAt, &p.UpdatedAt)
|
|
return &p, err
|
|
}
|