2023-01-22 10:12:55 -08:00
|
|
|
package dto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/jtom38/newsbot/collector/database"
|
|
|
|
"github.com/jtom38/newsbot/collector/domain/models"
|
|
|
|
)
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
func (c *DtoClient) ListSources(ctx context.Context, limit int32) ([]models.SourceDto, error) {
|
2023-01-22 10:12:55 -08:00
|
|
|
var res []models.SourceDto
|
|
|
|
|
|
|
|
items, err := c.db.ListSources(ctx, limit)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
res = append(res, c.ConvertToSource(item))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
func (c *DtoClient) ListSourcesBySource(ctx context.Context, sourceName string) ([]models.SourceDto, error) {
|
2023-01-22 10:12:55 -08:00
|
|
|
var res []models.SourceDto
|
|
|
|
|
|
|
|
items, err := c.db.ListSourcesBySource(ctx, strings.ToLower(sourceName))
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
res = append(res, c.ConvertToSource(item))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
func (c *DtoClient) GetSourceById(ctx context.Context, id uuid.UUID) (models.SourceDto, error) {
|
2023-01-22 10:12:55 -08:00
|
|
|
var res models.SourceDto
|
|
|
|
|
|
|
|
item, err := c.db.GetSourceByID(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.ConvertToSource(item), nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
func (c *DtoClient) GetSourceByNameAndSource(ctx context.Context, name, source string) (models.SourceDto, error) {
|
2023-01-22 10:12:55 -08:00
|
|
|
var res models.SourceDto
|
|
|
|
|
|
|
|
item, err := c.db.GetSourceByNameAndSource(ctx, database.GetSourceByNameAndSourceParams{
|
|
|
|
Name: name,
|
|
|
|
Source: source,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return res, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.ConvertToSource(item), nil
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:39:54 -08:00
|
|
|
func (c *DtoClient) ConvertToSource(i database.Source) models.SourceDto {
|
2023-01-22 10:12:55 -08:00
|
|
|
var deleted bool
|
|
|
|
if !i.Deleted.Valid {
|
|
|
|
deleted = true
|
|
|
|
}
|
|
|
|
|
|
|
|
return models.SourceDto{
|
|
|
|
ID: i.ID,
|
|
|
|
Site: i.Site,
|
|
|
|
Name: i.Name,
|
|
|
|
Source: i.Source,
|
|
|
|
Type: i.Type,
|
|
|
|
Value: i.Value.String,
|
|
|
|
Enabled: i.Enabled,
|
|
|
|
Url: i.Url,
|
|
|
|
Tags: c.SplitTags(i.Tags),
|
|
|
|
Deleted: deleted,
|
|
|
|
}
|
|
|
|
}
|