refine navigation and loading enteries

This commit is contained in:
Travis Shears 2025-10-02 20:09:57 +02:00
parent 99548e67b7
commit 8414414f98
10 changed files with 148 additions and 41 deletions

View file

@ -3,3 +3,4 @@ package gemlog
type Notification string
type ErrorMsg struct{ err error }
type GemLogsLoaded struct{ Logs []GemlogListEntry }
type GemLogLoaded struct{ Log GemlogEntry }

View file

@ -21,6 +21,7 @@ type GemlogListEntry struct {
Slug string `json:"slug"`
Date time.Time `json:"date"`
ID string `json:"id"`
Rev string `json:"rev"`
}
// NewUUID generates a new UUID v4 using crypto/rand

View file

@ -51,6 +51,7 @@ func listGemLogs(config *Config) ([]GemlogListEntry, error) {
Title string `json:"title"`
Slug string `json:"slug"`
Date time.Time `json:"date"`
Rev string `json:"rev"`
ID string `json:"id"`
} `json:"value"`
} `json:"rows"`
@ -68,6 +69,7 @@ func listGemLogs(config *Config) ([]GemlogListEntry, error) {
Title: row.Value.Title,
Slug: row.Value.Slug,
Date: row.Value.Date,
Rev: row.Value.Rev,
}
entries = append(entries, entry)
}
@ -76,8 +78,54 @@ func listGemLogs(config *Config) ([]GemlogListEntry, error) {
return entries, nil
}
func DeleteGemlogEntry(config *Config, id string) error {
func ReadGemlogEntry(config *Config, id string) (GemlogEntry, error) {
url := fmt.Sprintf("%s:%d/gemlog/%s", config.CouchDB.Host, config.CouchDB.Port, id)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return GemlogEntry{}, fmt.Errorf("failed to create request: %w", err)
}
req.Header.Add("authorization", genBasicAuthHeader(config.CouchDB.User, config.CouchDB.Password))
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return GemlogEntry{}, fmt.Errorf("failed to send request: %w", err)
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
return GemlogEntry{}, fmt.Errorf("failed to read response body: %w", err)
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
return GemlogEntry{}, fmt.Errorf("unexpected status code %d: %s", res.StatusCode, string(body))
}
var rawData struct {
ID int `json:"_id"`
Rev int `json:"_rev"`
Title string `json:"title"`
GemText string `json:"gemtext"`
Slug string `json:"slug"`
Date time.Time `json:"date"`
}
if err := json.Unmarshal(body, &rawData); err != nil {
return GemlogEntry{}, fmt.Errorf("failed to parse response: %w", err)
}
return GemlogEntry{
Title: rawData.Title,
Slug: rawData.Slug,
Date: rawData.Date,
Tags: make([]string, 0),
}, nil
}
func DeleteGemlogEntry(config *Config, id string, rev string) error {
url := fmt.Sprintf("%s:%d/gemlog/%s?rev=%s", config.CouchDB.Host, config.CouchDB.Port, id, rev)
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)

View file

@ -6,17 +6,28 @@ import (
tea "github.com/charmbracelet/bubbletea"
)
func DeleteGemlogCMD(config *Config, id string) tea.Cmd {
func DeleteGemlogCMD(config *Config, id string, rev string) tea.Cmd {
return func() tea.Msg {
err := DeleteGemlogEntry(config, id)
err := DeleteGemlogEntry(config, id, rev)
if err != nil {
return ErrorMsg{err}
}
return Notification(fmt.Sprintf("Gemlog with id: %s deleted", id))
}
}
func LoadGemlogCMD(config *Config) tea.Cmd {
func LoadGemlogCMD(config *Config, id string) tea.Cmd {
return func() tea.Msg {
log, err := ReadGemlogEntry(config, id)
if err != nil {
return ErrorMsg{err}
}
return GemLogLoaded{Log: log}
}
}
func LoadGemlogsCMD(config *Config) tea.Cmd {
return func() tea.Msg {
logs, err := listGemLogs(config)
if err != nil {