James Tombleson
2b959e140c
* updated db calls to new endpoints. Not all are finished yet * updated models to use the new dto objects to hide orm values * updated reddit to build articles based on what type of post it is * getting reddit ready to post the articles to the db * Now able to post to the db under the new DTO object * moved the reddit model to articles model to the struct and out of main
38 lines
764 B
Go
38 lines
764 B
Go
package database
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
|
|
"github.com/jtom38/newsbot/collector/domain/model"
|
|
)
|
|
|
|
type SourcesClient struct {
|
|
rootUri string
|
|
}
|
|
|
|
func (sb *SourcesClient) List() ([]model.Sources, error) {
|
|
var items []model.Sources
|
|
url := fmt.Sprintf("%v/api/v1/sources", sb.rootUri)
|
|
resp, err := getContent(url)
|
|
if err != nil { return items, err }
|
|
|
|
err = json.Unmarshal(resp, &items)
|
|
if err != nil { return []model.Sources{}, err }
|
|
|
|
return items, nil
|
|
}
|
|
|
|
func (sb *SourcesClient) FindBySource(SourceType string) ([]model.Sources, error) {
|
|
items, err := sb.List()
|
|
if err != nil { log.Panicln(err) }
|
|
|
|
var res []model.Sources
|
|
for _, item := range(items) {
|
|
if item.Source == SourceType {
|
|
res = append(res, item)
|
|
}
|
|
}
|
|
return res, nil
|
|
} |