2022-04-02 12:05:32 -07:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2022-04-07 14:53:40 -07:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2022-04-02 12:05:32 -07:00
|
|
|
"errors"
|
2022-04-07 14:53:40 -07:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-04-02 12:05:32 -07:00
|
|
|
|
|
|
|
"github.com/jtom38/newsbot/collector/domain/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Generate this struct fr
|
|
|
|
type ArticlesClient struct {
|
|
|
|
rootUri string
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:53:40 -07:00
|
|
|
func (ac *ArticlesClient) List() ([]model.Articles, error) {
|
2022-04-02 12:05:32 -07:00
|
|
|
var items []model.Articles
|
2022-04-07 14:53:40 -07:00
|
|
|
url := fmt.Sprintf("%v/api/v1/articles", ac.rootUri)
|
|
|
|
resp, err := getContent(url)
|
|
|
|
if err != nil {
|
|
|
|
return items, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(resp, &items)
|
|
|
|
if err != nil {
|
|
|
|
return []model.Articles{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return items, nil
|
2022-04-02 12:05:32 -07:00
|
|
|
}
|
|
|
|
|
2022-04-07 14:53:40 -07:00
|
|
|
func (ac *ArticlesClient) FindByID(ID uint) (model.Articles, error) {
|
|
|
|
var items model.Articles
|
|
|
|
url := fmt.Sprintf("%v/api/v1/articles/%v", ac.rootUri, ID)
|
|
|
|
resp, err := getContent(url)
|
|
|
|
if err != nil {
|
|
|
|
return items, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(resp, &items)
|
|
|
|
if err != nil {
|
|
|
|
return items, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return items, nil
|
2022-04-02 12:05:32 -07:00
|
|
|
}
|
|
|
|
|
2022-04-07 14:53:40 -07:00
|
|
|
func (ac *ArticlesClient) FindByUrl(url string) (model.Articles, error) {
|
|
|
|
var item model.Articles
|
|
|
|
get := fmt.Sprintf("%v/api/v1/articles/url/%v", ac.rootUri, url)
|
|
|
|
resp, err := getContent(get)
|
|
|
|
if err != nil {
|
|
|
|
return item, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(resp, &item)
|
|
|
|
if err != nil {
|
|
|
|
return item, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return item, nil
|
2022-04-02 12:05:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *ArticlesClient) Delete(id int32) error {
|
|
|
|
return errors.New("not implemented")
|
|
|
|
}
|
|
|
|
|
2022-04-07 14:53:40 -07:00
|
|
|
func (ac *ArticlesClient) Add(item model.Articles) error {
|
|
|
|
//return errors.New("not implemented")
|
|
|
|
url := fmt.Sprintf("%v/api/v1/articles/", ac.rootUri)
|
|
|
|
|
|
|
|
bItem, err := json.Marshal(item)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("POST", url, bytes.NewBuffer(bItem))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return errors.New("failed to post to the DB")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
//body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
//if err != nil { return err }
|
|
|
|
|
2022-04-02 12:05:32 -07:00
|
|
|
}
|