Added description and a way to save and restore the RSS feed.

This commit is contained in:
Jason Streifling 2024-02-18 14:01:06 +01:00
parent ee04a2a351
commit c5623fe4fd
4 changed files with 46 additions and 10 deletions

2
.gitignore vendored
View File

@ -21,3 +21,5 @@
# Go workspace file
go.work
# Custom stuff
tmp/

View File

@ -1,19 +1,21 @@
package feed
import (
"encoding/gob"
"fmt"
"os"
"time"
"github.com/gorilla/feeds"
)
type Feed struct {
*feeds.Feed
feeds.Feed
}
func NewFeed(title, link, desc string) Feed {
return Feed{
Feed: &feeds.Feed{
func NewFeed(title, link, desc string) *Feed {
return &Feed{
Feed: feeds.Feed{
Title: title,
Link: &feeds.Link{Href: link},
Description: desc,
@ -21,11 +23,40 @@ func NewFeed(title, link, desc string) Feed {
}
}
func AddToFeed(feed Feed, title, content string) error {
func SaveFeed(feed *Feed, filename string) error {
file, err := os.Create(filename)
if err != nil {
return fmt.Errorf("error cmd/feed/rss.go WriteFeed os.Create(): %v", err)
}
defer file.Close()
encoder := gob.NewEncoder(file)
encoder.Encode(feed)
return nil
}
func OpenFeed(filename string) (*Feed, error) {
var feed *Feed
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error cmd/feed/rss.go ReadFeed os.Open(): %v", err)
}
defer file.Close()
decoder := gob.NewDecoder(file)
decoder.Decode(feed)
return feed, nil
}
func AddToFeed(feed *Feed, title, desc, content string) error {
item := feeds.Item{
Title: title,
Created: time.Now(),
Content: content,
Title: title,
Created: time.Now(),
Description: desc,
Content: content,
}
feed.Add(&item)

View File

@ -8,9 +8,10 @@ import (
"streifling.com/jason/cpolis/cmd/feed"
)
func HandleFinishedEdit(f feed.Feed) http.HandlerFunc {
func HandleFinishedEdit(f *feed.Feed) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
title := r.PostFormValue("editor-title")
desc := r.PostFormValue("editor-desc")
mdContent := r.PostFormValue("editor-text")
content, err := articles.ConvertToHTML(mdContent)
@ -19,7 +20,8 @@ func HandleFinishedEdit(f feed.Feed) http.HandlerFunc {
log.Panicln(err)
}
feed.AddToFeed(f, title, content)
feed.AddToFeed(f, title, desc, content)
feed.SaveFeed(f, "tmp/rss.gob")
// template.Must(template.ParseFiles("web/templates/editor.html")).ExecuteTemplate(w, "html-result", rssItem)
}
}

View File

@ -1,6 +1,7 @@
{{define "page-content"}}
<form>
<input type="text" name="editor-title" value="Titel">
<textarea name="editor-desc"></textarea>
<textarea name="editor-text"></textarea>
<input type="submit" value="Senden" hx-post="/finished-edit/" hx-target="#page-content">
</form>