Skip to content

Commit

Permalink
feat: added deleting and fixed a bug in writing the urls
Browse files Browse the repository at this point in the history
  • Loading branch information
TypicalAM committed Jan 3, 2023
1 parent ead032c commit a8d8836
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 43 deletions.
20 changes: 20 additions & 0 deletions internal/backend/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Backend interface {
FetchArticles(feedName string) tea.Cmd
// AddItem adds an item to the rss
AddItem(itemType ItemType, fields ...string)
// DeleteItem deletes an item from the rss
DeleteItem(itemType ItemType, key string)
// Close closes the backend
Close() error
}
Expand Down Expand Up @@ -63,3 +65,21 @@ func NewItem(itemType ItemType, fields ...string) tea.Cmd {
}
}
}

// DeleteItemMessage is a message to tell the main model that a new item
// needs to be removed from the list
type DeleteItemMessage struct {
Type ItemType
Key string
}

// DeleteItem is a function to tell the main model that a new item
// needs to be removed from the list
func DeleteItem(itemType ItemType, key string) tea.Cmd {
return func() tea.Msg {
return DeleteItemMessage{
Type: itemType,
Key: key,
}
}
}
23 changes: 22 additions & 1 deletion internal/backend/cache/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Backend struct {
}

// New creates a new Cache Backend
func New(urlFilePath string) backend.Backend {
func New(urlFilePath string) Backend {
// TODO: Make the path configurable
cache := newCache()

Expand Down Expand Up @@ -137,6 +137,27 @@ func (b Backend) AddItem(itemType backend.ItemType, fields ...string) {
}
}

// DeleteItem deletes an item from the rss
func (b Backend) DeleteItem(itemType backend.ItemType, key string) {
// Delete the item from the rss
switch itemType {
case backend.Category:
for i, cat := range b.rss.Categories {
if cat.Name == key {
b.rss.Categories = append(b.rss.Categories[:i], b.rss.Categories[i+1:]...)
return
}
}
case backend.Feed:
// FIXME: Get the category
for i, cat := range b.rss.Categories {
if cat.Name == key {
b.rss.Categories = append(b.rss.Categories[:i], b.rss.Categories[i+1:]...)
}
}
}
}

// Close closes the backend
func (b Backend) Close() error {
// Try to save the rss
Expand Down
37 changes: 15 additions & 22 deletions internal/backend/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

// Cache is a basic cache to read and write gofeed.Items based on the URL
type Cache struct {
path string
Content map[string]Item
filePath string
Content map[string]Item
}

// Item is an item in the cache
Expand All @@ -31,15 +31,15 @@ func newCache() Cache {
}

return Cache{
path: path,
Content: make(map[string]Item),
filePath: path,
Content: make(map[string]Item),
}
}

// Load reads the cache from disk
func (c *Cache) Load() error {
// Load the cache from the file
file, err := os.ReadFile(c.path)
file, err := os.ReadFile(c.filePath)
if err != nil {
return err
}
Expand All @@ -62,33 +62,26 @@ func (c *Cache) Load() error {

// Save writes the cache to disk
func (c *Cache) Save() error {
file, err := os.Create(c.path)
// Try to encode the cache
cacheData, err := json.Marshal(c.Content)
if err != nil {
return err
}

// Try to write the data to the file
if err = os.WriteFile(c.filePath, cacheData, 0600); err != nil {
// Try to create the directory
err = os.MkdirAll(filepath.Dir(c.path), 0755)
err = os.MkdirAll(filepath.Dir(c.filePath), 0755)
if err != nil {
return err
}

// Try to create the file again
file, err = os.Create(c.path)
// Try to write to the file again
err = os.WriteFile(c.filePath, cacheData, 0600)
if err != nil {
return err
}
}
defer file.Close()

// Try to encode the cache
content, err := json.Marshal(c.Content)
if err != nil {
return err
}

// Try to write the cache to disk
_, err = file.Write(content)
if err != nil {
return err
}

// Writing was successful
return nil
Expand Down
3 changes: 3 additions & 0 deletions internal/backend/fake/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ func (b Backend) FetchArticles(feedName string) tea.Cmd {
// AddItem adds an item to the rss
func (b Backend) AddItem(itemType backend.ItemType, fields ...string) {}

// DeleteItem deletes an item from the rss
func (b Backend) DeleteItem(itemType backend.ItemType, key string) {}

// Close the backend
func (b Backend) Close() error {
return nil
Expand Down
20 changes: 20 additions & 0 deletions internal/backend/web/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,26 @@ func (b Backend) AddItem(itemType backend.ItemType, fields ...string) {
}
}

// DeleteItem deletes an item from the rss
func (b Backend) DeleteItem(itemType backend.ItemType, key string) {
// Delete the item from the rss
switch itemType {
case backend.Category:
for i, cat := range b.rss.Categories {
if cat.Name == key {
b.rss.Categories = append(b.rss.Categories[:i], b.rss.Categories[i+1:]...)
}
}
case backend.Feed:
// FIXME: Get the category
for i, cat := range b.rss.Categories {
if cat.Name == key {
b.rss.Categories = append(b.rss.Categories[:i], b.rss.Categories[i+1:]...)
}
}
}
}

// Close closes the backend
func (b Backend) Close() error {
// Try to save the rss
Expand Down
20 changes: 19 additions & 1 deletion internal/model/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.message = "Added an item - " + strings.Join(m.createItem.GetValues(), " ")

// Fetch the categories again to update the list
return m, m.backend.FetchCategories()
if m.createItem.Type == backend.Category {
return m, m.backend.FetchCategories()
}

// Fetch the feeds again to update the list
return m, m.backend.FetchFeeds(m.tabs[m.activeTab].Title())
}

return m, tea.Batch(cmds...)
Expand Down Expand Up @@ -100,6 +105,19 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.createItem = newItemCreation(msg.Fields, msg.Type)
m.creatingItem = true

case backend.DeleteItemMessage:
// Delete the item
m.message = fmt.Sprintf("Deleted item %s", msg.Key)
m.backend.DeleteItem(msg.Type, msg.Key)

// Fetch the categories again to update the list
if m.createItem.Type == backend.Category {
return m, m.backend.FetchCategories()
}

// Fetch the feeds again to update the list
return m, m.backend.FetchFeeds(m.tabs[m.activeTab].Title())

case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc", "q":
Expand Down
22 changes: 4 additions & 18 deletions internal/rss/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rss

import (
"errors"
"fmt"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -82,39 +81,26 @@ func (rss *Rss) loadFromFile() error {

// Save will write the Rss structure to a file
func (rss *Rss) Save() error {
fmt.Println("Saving rss file to", rss.filePath)
for _, cat := range rss.Categories {
fmt.Println("Category:", cat.Name)
}

// Try to marshall the data
yamlData, err := yaml.Marshal(rss)
if err != nil {
return err
}

// Try to open the file, if it doesn't exist, create it
file, err := os.OpenFile(rss.filePath, os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
// Try to write the data to the file
if err = os.WriteFile(rss.filePath, yamlData, 0600); err != nil {
// Try to create the directory
err = os.MkdirAll(filepath.Dir(rss.filePath), 0755)
if err != nil {
return err
}

// Try to create the file again
file, err = os.Create(rss.filePath)
// Try to write to the file again
err = os.WriteFile(rss.filePath, yamlData, 0600)
if err != nil {
return err
}
}
defer file.Close()

// Write the data to the file
_, err = file.Write(yamlData)
if err != nil {
return err
}

// Successfully wrote the file
return nil
Expand Down
14 changes: 13 additions & 1 deletion internal/tab/welcome/welcome.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,19 @@ func (w Welcome) Update(msg tea.Msg) (tab.Tab, tea.Cmd) {

case "n":
// Add a new category
cmds = append(cmds, backend.NewItem(backend.Category, "name", "desc"))
cmds = append(cmds, backend.NewItem(
backend.Category,
"name", "desc",
))

case "d":
// Delete the selected category
if !w.list.IsEmpty() {
cmds = append(cmds, backend.DeleteItem(
backend.Category,
w.list.SelectedItem().FilterValue(),
))
}
}
}

Expand Down

0 comments on commit a8d8836

Please sign in to comment.