2023-01-22 10:12:55 -08:00
|
|
|
package dto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2024-04-23 07:15:38 -07:00
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/database"
|
|
|
|
"git.jamestombleson.com/jtom38/newsbot-api/internal/domain/models"
|
2023-01-22 10:12:55 -08:00
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
func (c *DtoClient) ListSubscriptions(ctx context.Context, limit int32) ([]models.SubscriptionDto, error) {
|
2023-01-22 10:12:55 -08:00
|
|
|
var res []models.SubscriptionDto
|
|
|
|
|
|
|
|
items, err := c.db.ListSubscriptions(ctx, limit)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
res = append(res, c.ConvertSubscription(item))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
func (c *DtoClient) ListSubscriptionDetails(ctx context.Context, limit int32) ([]models.SubscriptionDetailsDto, error) {
|
2023-01-22 10:12:55 -08:00
|
|
|
var res []models.SubscriptionDetailsDto
|
|
|
|
|
|
|
|
items, err := c.ListSubscriptions(ctx, limit)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
dwh, err := c.GetDiscordWebhook(ctx, item.DiscordWebhookId)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
source, err := c.GetSourceById(ctx, item.SourceId)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, models.SubscriptionDetailsDto{
|
|
|
|
ID: item.ID,
|
|
|
|
Source: source,
|
|
|
|
DiscordWebHook: dwh,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
func (c *DtoClient) ListSubscriptionsByDiscordWebhookId(ctx context.Context, id uuid.UUID) ([]models.SubscriptionDto, error) {
|
2023-01-22 10:12:55 -08:00
|
|
|
var res []models.SubscriptionDto
|
|
|
|
|
|
|
|
items, err := c.db.GetSubscriptionsByDiscordWebHookId(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
res = append(res, c.ConvertSubscription(item))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
func (c *DtoClient) ListSubscriptionsBySourceId(ctx context.Context, id uuid.UUID) ([]models.SubscriptionDto, error) {
|
2023-01-22 10:12:55 -08:00
|
|
|
var res []models.SubscriptionDto
|
|
|
|
|
|
|
|
items, err := c.db.GetSubscriptionsBySourceID(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
res = append(res, c.ConvertSubscription(item))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
func (c *DtoClient) ConvertSubscription(i database.Subscription) models.SubscriptionDto {
|
2023-01-22 10:12:55 -08:00
|
|
|
return models.SubscriptionDto{
|
|
|
|
ID: i.ID,
|
|
|
|
DiscordWebhookId: i.Discordwebhookid,
|
|
|
|
SourceId: i.Sourceid,
|
|
|
|
}
|
|
|
|
}
|