Skip to content

Commit

Permalink
refactor: move the backends to their own pakcages
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Dec 30, 2022
1 parent 08679d3 commit 5b517e7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
4 changes: 2 additions & 2 deletions cmd/goread/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"os"

"github.com/TypicalAM/goread/internal/backend"
"github.com/TypicalAM/goread/internal/backend/web"
"github.com/TypicalAM/goread/internal/model"
"github.com/TypicalAM/goread/internal/style"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -18,7 +18,7 @@ func main() {
}

// Create the main model
model := model.New(backend.NewWebBackend())
model := model.New(web.New())

// Start the program
p := tea.NewProgram(model)
Expand Down
13 changes: 7 additions & 6 deletions internal/backend/fake.go → internal/backend/fake/fake.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package backend
package fake

import (
"os"
"strings"
"time"

"github.com/PuerkitoBio/goquery"
"github.com/TypicalAM/goread/internal/backend"
simpleList "github.com/TypicalAM/goread/internal/list"
"github.com/TypicalAM/goread/internal/style"
"github.com/charmbracelet/bubbles/list"
Expand All @@ -25,7 +26,7 @@ func (b FakeBackend) Name() string {
// Return some fake categories
func (b FakeBackend) FetchCategories() tea.Cmd {
return func() tea.Msg {
return FetchSuccessMessage{
return backend.FetchSuccessMessage{
Items: []list.Item{
simpleList.NewListItem("All", "All the categories", ""),
simpleList.NewListItem("Books", "Books", "books"),
Expand All @@ -42,7 +43,7 @@ func (b FakeBackend) FetchCategories() tea.Cmd {
func (b FakeBackend) FetchFeeds(catName string) tea.Cmd {
return func() tea.Msg {
time.Sleep(1 * time.Second)
return FetchSuccessMessage{
return backend.FetchSuccessMessage{
Items: []list.Item{
simpleList.NewListItem("feed 1", "cat1", "more content"),
simpleList.NewListItem("feed 2", "cat2", "more content"),
Expand All @@ -58,7 +59,7 @@ func (b FakeBackend) FetchArticles(feedName string) tea.Cmd {
time.Sleep(1 * time.Second)
file, err := os.Open("rss.rss")
if err != nil {
return FetchErrorMessage{
return backend.FetchErrorMessage{
Description: "Could not open file",
Err: err,
}
Expand All @@ -68,7 +69,7 @@ func (b FakeBackend) FetchArticles(feedName string) tea.Cmd {
fp := gofeed.NewParser()
feed, err := fp.Parse(file)
if err != nil {
return FetchErrorMessage{
return backend.FetchErrorMessage{
Description: "Could not parse file",
Err: err,
}
Expand All @@ -85,7 +86,7 @@ func (b FakeBackend) FetchArticles(feedName string) tea.Cmd {
}

// Return the message
return FetchSuccessMessage{Items: result}
return backend.FetchSuccessMessage{Items: result}
}
}

Expand Down
20 changes: 11 additions & 9 deletions internal/backend/web.go → internal/backend/web/web.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package backend
package web

import (
"github.com/TypicalAM/goread/internal/backend"
"github.com/TypicalAM/goread/internal/backend/fake"
simpleList "github.com/TypicalAM/goread/internal/list"
"github.com/TypicalAM/goread/internal/rss"
"github.com/charmbracelet/bubbles/list"
Expand All @@ -14,7 +16,7 @@ type WebBackend struct {
}

// New returns a new WebBackend
func NewWebBackend() WebBackend {
func New() WebBackend {
return WebBackend{rss: rss.New()}
}

Expand All @@ -37,7 +39,7 @@ func (b WebBackend) FetchCategories() tea.Cmd {
}

// Return the message
return FetchSuccessMessage{Items: items}
return backend.FetchSuccessMessage{Items: items}
}
}

Expand All @@ -48,7 +50,7 @@ func (b WebBackend) FetchFeeds(catName string) tea.Cmd {
// Create a list of feeds
category, err := b.rss.GetCategory(catName)
if err != nil {
return FetchErrorMessage{
return backend.FetchErrorMessage{
Description: "Failed to get feeds",
Err: err,
}
Expand All @@ -67,7 +69,7 @@ func (b WebBackend) FetchFeeds(catName string) tea.Cmd {
}

// Return the message
return FetchSuccessMessage{Items: items}
return backend.FetchSuccessMessage{Items: items}
}
}

Expand All @@ -78,7 +80,7 @@ func (b WebBackend) FetchArticles(feedName string) tea.Cmd {
// Create a list of articles
url, err := b.rss.GetFeedUrl(feedName)
if err != nil {
return FetchErrorMessage{
return backend.FetchErrorMessage{
Description: "Failed to get articles",
Err: err,
}
Expand All @@ -88,7 +90,7 @@ func (b WebBackend) FetchArticles(feedName string) tea.Cmd {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(url)
if err != nil {
return FetchErrorMessage{
return backend.FetchErrorMessage{
Description: "Failed to parse the articles",
Err: err,
}
Expand All @@ -97,7 +99,7 @@ func (b WebBackend) FetchArticles(feedName string) tea.Cmd {
// Create the list of list items
var result []list.Item
for i := range feed.Items {
content := CreateFakeContent(i, feed)
content := fake.CreateFakeContent(i, feed)
result = append(result, simpleList.NewListItem(
content.Title,
content.Description,
Expand All @@ -106,6 +108,6 @@ func (b WebBackend) FetchArticles(feedName string) tea.Cmd {
}

// Return the message
return FetchSuccessMessage{Items: result}
return backend.FetchSuccessMessage{Items: result}
}
}

0 comments on commit 5b517e7

Please sign in to comment.