allow to mark episodes as completed

This commit is contained in:
2026-03-12 14:53:44 +03:00
parent 618c034b05
commit 5409167a96
6 changed files with 152 additions and 28 deletions

43
main.go
View File

@@ -57,6 +57,7 @@ type Episode struct {
Url string `json:"url" db:"url"`
PodcastId int64 `json:"podcastId" db:"podcast_id"`
Number int `json:"number" db:"number"`
Listened bool `json:"listened" db:"listened"`
CreatedAt time.Time `json:"createdAt" db:"created_at"`
}
@@ -308,6 +309,8 @@ func main() {
);
`)
db.Exec(`ALTER TABLE episodes ADD COLUMN listened boolean default false;`)
go func(db *sqlx.DB) {
for {
podcasts, err := getPodcasts(db)
@@ -496,6 +499,46 @@ func main() {
}{true}, 200)
})
mux.HandleFunc("PATCH /api/episodes/{id}", func(w http.ResponseWriter, r *http.Request) {
var body struct {
Listened *bool `json:"listened"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, err.Error(), 400)
return
}
fields := map[string]any{}
if body.Listened != nil {
fields["listened"] = &body.Listened
}
if len(fields) == 0 {
sendJSON(w, struct {
Ok bool `json:"ok"`
}{true}, 200)
return
}
fields["id"] = r.PathValue("id")
f := []string{}
for field := range fields {
f = append(f, fmt.Sprintf("%s = :%s", field, field))
}
q := fmt.Sprintf("UPDATE episodes SET %s WHERE id = :id", strings.Join(f, ", "))
_, err := db.NamedExec(q, fields)
if err != nil {
http.Error(w, err.Error(), 400)
return
}
sendJSON(w, struct {
Ok bool `json:"ok"`
}{true}, 200)
})
fmt.Println("starting http server on http://localhost:5000")
if err := http.ListenAndServe(":5000", mux); err != nil {
log.Fatalf("failed to start http server: %v\n", err)