2024-04-27 13:11:03 -07:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/entity"
|
2024-04-27 13:11:03 -07:00
|
|
|
"github.com/huandu/go-sqlbuilder"
|
|
|
|
)
|
|
|
|
|
2024-05-04 11:58:35 -07:00
|
|
|
type DiscordWebHookRepo interface {
|
|
|
|
Create(ctx context.Context, userId int64, url, server, channel string, enabled bool) (int64, error)
|
2024-04-28 11:39:25 -07:00
|
|
|
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)
|
2024-05-09 19:08:57 -07:00
|
|
|
GetById(ctx context.Context, id int64) (entity.DiscordWebHookEntity, error)
|
|
|
|
GetByUrl(ctx context.Context, url string) (entity.DiscordWebHookEntity, error)
|
|
|
|
ListByServerName(ctx context.Context, name string) ([]entity.DiscordWebHookEntity, error)
|
|
|
|
ListByServerAndChannel(ctx context.Context, server, channel string) ([]entity.DiscordWebHookEntity, error)
|
2024-04-28 11:39:25 -07:00
|
|
|
}
|
|
|
|
|
2024-04-27 13:11:03 -07:00
|
|
|
type discordWebHookRepository struct {
|
|
|
|
conn *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDiscordWebHookRepository(conn *sql.DB) discordWebHookRepository {
|
|
|
|
return discordWebHookRepository{
|
|
|
|
conn: conn,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-04 11:58:35 -07:00
|
|
|
func (r discordWebHookRepository) Create(ctx context.Context, userId int64, url, server, channel string, enabled bool) (int64, error) {
|
2024-04-27 13:11:03 -07:00
|
|
|
dt := time.Now()
|
|
|
|
queryBuilder := sqlbuilder.NewInsertBuilder()
|
|
|
|
queryBuilder.InsertInto("DiscordWebHooks")
|
2024-05-04 11:58:35 -07:00
|
|
|
queryBuilder.Cols("UpdatedAt", "CreatedAt", "DeletedAt", "UserID", "Url", "Server", "Channel", "Enabled")
|
|
|
|
queryBuilder.Values(dt, dt, timeZero, userId, url, server, channel, enabled)
|
2024-04-27 13:11:03 -07:00
|
|
|
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()),
|
|
|
|
)
|
2024-04-28 10:02:57 -07:00
|
|
|
b.Where(
|
|
|
|
b.Equal("Id", id),
|
|
|
|
)
|
2024-04-27 13:11:03 -07:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-04-28 10:02:57 -07:00
|
|
|
func (r discordWebHookRepository) SoftDelete(ctx context.Context, id int64) (int64, error) {
|
|
|
|
return softDeleteRow(ctx, r.conn, "DiscordWebHooks", id)
|
|
|
|
}
|
2024-04-27 13:11:03 -07:00
|
|
|
|
2024-04-28 10:02:57 -07:00
|
|
|
func (r discordWebHookRepository) Restore(ctx context.Context, id int64) (int64, error) {
|
|
|
|
return restoreRow(ctx, r.conn, "DiscordWebHooks", id)
|
2024-04-27 13:11:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r discordWebHookRepository) Delete(ctx context.Context, id int64) (int64, error) {
|
2024-04-28 10:02:57 -07:00
|
|
|
return deleteFromTable(ctx, r.conn, "DiscordWebHooks", id)
|
2024-04-27 13:11:03 -07:00
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
func (r discordWebHookRepository) GetById(ctx context.Context, id int64) (entity.DiscordWebHookEntity, error) {
|
2024-04-27 13:11:03 -07:00
|
|
|
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 {
|
2024-05-09 19:08:57 -07:00
|
|
|
return entity.DiscordWebHookEntity{}, err
|
2024-04-27 13:11:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := r.processRows(rows)
|
|
|
|
if len(data) == 0 {
|
2024-05-09 19:08:57 -07:00
|
|
|
return entity.DiscordWebHookEntity{}, err
|
2024-04-27 13:11:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return data[0], nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
func (r discordWebHookRepository) GetByUrl(ctx context.Context, url string) (entity.DiscordWebHookEntity, error) {
|
2024-04-27 13:11:03 -07:00
|
|
|
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 {
|
2024-05-09 19:08:57 -07:00
|
|
|
return entity.DiscordWebHookEntity{}, err
|
2024-04-27 13:11:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := r.processRows(rows)
|
|
|
|
if len(data) == 0 {
|
2024-05-09 19:08:57 -07:00
|
|
|
return entity.DiscordWebHookEntity{}, err
|
2024-04-27 13:11:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return data[0], nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
func (r discordWebHookRepository) ListByServerName(ctx context.Context, name string) ([]entity.DiscordWebHookEntity, error) {
|
2024-04-27 13:11:03 -07:00
|
|
|
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 {
|
2024-05-09 19:08:57 -07:00
|
|
|
return []entity.DiscordWebHookEntity{}, err
|
2024-04-27 13:11:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := r.processRows(rows)
|
|
|
|
if len(data) == 0 {
|
2024-05-09 19:08:57 -07:00
|
|
|
return []entity.DiscordWebHookEntity{}, err
|
2024-04-27 13:11:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
func (r discordWebHookRepository) ListByServerAndChannel(ctx context.Context, server, channel string) ([]entity.DiscordWebHookEntity, error) {
|
2024-04-27 13:11:03 -07:00
|
|
|
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 {
|
2024-05-09 19:08:57 -07:00
|
|
|
return []entity.DiscordWebHookEntity{}, err
|
2024-04-27 13:11:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
data, err := r.processRows(rows)
|
|
|
|
if len(data) == 0 {
|
2024-05-09 19:08:57 -07:00
|
|
|
return []entity.DiscordWebHookEntity{}, err
|
2024-04-27 13:11:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
func (r discordWebHookRepository) processRows(rows *sql.Rows) ([]entity.DiscordWebHookEntity, error) {
|
|
|
|
items := []entity.DiscordWebHookEntity{}
|
2024-04-27 13:11:03 -07:00
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var id int64
|
|
|
|
var createdAt time.Time
|
|
|
|
var updatedAt time.Time
|
2024-04-28 10:02:57 -07:00
|
|
|
var deletedAt time.Time
|
2024-05-04 11:58:35 -07:00
|
|
|
var userId int64
|
2024-04-27 13:11:03 -07:00
|
|
|
var url string
|
|
|
|
var server string
|
|
|
|
var channel string
|
|
|
|
var enabled bool
|
|
|
|
err := rows.Scan(
|
|
|
|
&id, &createdAt, &updatedAt,
|
2024-05-04 11:58:35 -07:00
|
|
|
&deletedAt, &userId, &url, &server,
|
2024-04-27 13:11:03 -07:00
|
|
|
&channel, &enabled,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return items, err
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:08:57 -07:00
|
|
|
item := entity.DiscordWebHookEntity{
|
2024-04-27 13:11:03 -07:00
|
|
|
ID: id,
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
UpdatedAt: updatedAt,
|
2024-04-28 10:02:57 -07:00
|
|
|
DeletedAt: deletedAt,
|
2024-05-04 11:58:35 -07:00
|
|
|
UserID: userId,
|
2024-04-27 13:11:03 -07:00
|
|
|
Url: url,
|
|
|
|
Server: server,
|
|
|
|
Channel: channel,
|
|
|
|
Enabled: enabled,
|
|
|
|
}
|
|
|
|
|
|
|
|
items = append(items, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
return items, nil
|
|
|
|
}
|