newsbot-api/internal/repository/discordWebHooks.go

229 lines
5.8 KiB
Go

package repository
import (
"context"
"database/sql"
"time"
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain"
"github.com/huandu/go-sqlbuilder"
)
type DiscordWebHookRepo interface {
Create(ctx context.Context, userId int64, url, server, channel string, enabled bool) (int64, error)
Enable(ctx context.Context, id int64) (int64, error)
Disable(ctx context.Context, id int64) (int64, error)
SoftDelete(ctx context.Context, id int64) (int64, error)
Restore(ctx context.Context, id int64) (int64, error)
Delete(ctx context.Context, id int64) (int64, error)
GetById(ctx context.Context, id int64) (domain.DiscordWebHookEntity, error)
GetByUrl(ctx context.Context, url string) (domain.DiscordWebHookEntity, error)
ListByServerName(ctx context.Context, name string) ([]domain.DiscordWebHookEntity, error)
ListByServerAndChannel(ctx context.Context, server, channel string) ([]domain.DiscordWebHookEntity, error)
}
type discordWebHookRepository struct {
conn *sql.DB
}
func NewDiscordWebHookRepository(conn *sql.DB) discordWebHookRepository {
return discordWebHookRepository{
conn: conn,
}
}
func (r discordWebHookRepository) Create(ctx context.Context, userId int64, url, server, channel string, enabled bool) (int64, error) {
dt := time.Now()
queryBuilder := sqlbuilder.NewInsertBuilder()
queryBuilder.InsertInto("DiscordWebHooks")
queryBuilder.Cols("UpdatedAt", "CreatedAt", "DeletedAt", "UserID", "Url", "Server", "Channel", "Enabled")
queryBuilder.Values(dt, dt, timeZero, userId, url, server, channel, enabled)
query, args := queryBuilder.Build()
_, err := r.conn.ExecContext(ctx, query, args...)
if err != nil {
return 0, err
}
return 1, nil
}
func (r discordWebHookRepository) Enable(ctx context.Context, id int64) (int64, error) {
b := sqlbuilder.NewUpdateBuilder()
b.Update("DiscordWebHooks")
b.Set(
b.Assign("Enabled", true),
b.Assign("UpdatedAt", time.Now()),
)
b.Where(
b.Equal("Id", id),
)
query, args := b.Build()
_, err := r.conn.ExecContext(ctx, query, args...)
if err != nil {
return 0, err
}
return 1, nil
}
func (r discordWebHookRepository) Disable(ctx context.Context, id int64) (int64, error) {
b := sqlbuilder.NewUpdateBuilder()
b.Update("DiscordWebHooks")
b.Set(
b.Assign("Enabled", false),
b.Assign("UpdatedAt", time.Now()),
)
b.Where(
b.Equal("Id", id),
)
query, args := b.Build()
_, err := r.conn.ExecContext(ctx, query, args...)
if err != nil {
return 0, err
}
return 1, nil
}
func (r discordWebHookRepository) SoftDelete(ctx context.Context, id int64) (int64, error) {
return softDeleteRow(ctx, r.conn, "DiscordWebHooks", id)
}
func (r discordWebHookRepository) Restore(ctx context.Context, id int64) (int64, error) {
return restoreRow(ctx, r.conn, "DiscordWebHooks", id)
}
func (r discordWebHookRepository) Delete(ctx context.Context, id int64) (int64, error) {
return deleteFromTable(ctx, r.conn, "DiscordWebHooks", id)
}
func (r discordWebHookRepository) GetById(ctx context.Context, id int64) (domain.DiscordWebHookEntity, error) {
builder := sqlbuilder.NewSelectBuilder()
builder.Select("*")
builder.From("DiscordWebHooks").Where(
builder.E("id", id),
)
builder.Limit(1)
query, args := builder.Build()
rows, err := r.conn.QueryContext(ctx, query, args...)
if err != nil {
return domain.DiscordWebHookEntity{}, err
}
data, err := r.processRows(rows)
if len(data) == 0 {
return domain.DiscordWebHookEntity{}, err
}
return data[0], nil
}
func (r discordWebHookRepository) GetByUrl(ctx context.Context, url string) (domain.DiscordWebHookEntity, error) {
builder := sqlbuilder.NewSelectBuilder()
builder.Select("*")
builder.From("DiscordWebHooks").Where(
builder.E("Url", url),
)
builder.Limit(1)
query, args := builder.Build()
rows, err := r.conn.QueryContext(ctx, query, args...)
if err != nil {
return domain.DiscordWebHookEntity{}, err
}
data, err := r.processRows(rows)
if len(data) == 0 {
return domain.DiscordWebHookEntity{}, err
}
return data[0], nil
}
func (r discordWebHookRepository) ListByServerName(ctx context.Context, name string) ([]domain.DiscordWebHookEntity, error) {
builder := sqlbuilder.NewSelectBuilder()
builder.Select("*")
builder.From("DiscordWebHooks").Where(
builder.E("Server", name),
)
query, args := builder.Build()
rows, err := r.conn.QueryContext(ctx, query, args...)
if err != nil {
return []domain.DiscordWebHookEntity{}, err
}
data, err := r.processRows(rows)
if len(data) == 0 {
return []domain.DiscordWebHookEntity{}, err
}
return data, nil
}
func (r discordWebHookRepository) ListByServerAndChannel(ctx context.Context, server, channel string) ([]domain.DiscordWebHookEntity, error) {
builder := sqlbuilder.NewSelectBuilder()
builder.Select("*")
builder.From("DiscordWebHooks").Where(
builder.Equal("Server", server),
builder.Equal("Channel", channel),
)
query, args := builder.Build()
rows, err := r.conn.QueryContext(ctx, query, args...)
if err != nil {
return []domain.DiscordWebHookEntity{}, err
}
data, err := r.processRows(rows)
if len(data) == 0 {
return []domain.DiscordWebHookEntity{}, err
}
return data, nil
}
func (r discordWebHookRepository) processRows(rows *sql.Rows) ([]domain.DiscordWebHookEntity, error) {
items := []domain.DiscordWebHookEntity{}
for rows.Next() {
var id int64
var createdAt time.Time
var updatedAt time.Time
var deletedAt time.Time
var userId int64
var url string
var server string
var channel string
var enabled bool
err := rows.Scan(
&id, &createdAt, &updatedAt,
&deletedAt, &userId, &url, &server,
&channel, &enabled,
)
if err != nil {
return items, err
}
item := domain.DiscordWebHookEntity{
ID: id,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
DeletedAt: deletedAt,
UserID: userId,
Url: url,
Server: server,
Channel: channel,
Enabled: enabled,
}
items = append(items, item)
}
return items, nil
}