diff --git a/README.md b/README.md index 608525d..060133d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,13 @@ Quick tool to write gemtext posts for my gemlog. => gemini://travisshears.com/gemlog +## Dev + +To run command locally: + +```shell +$ go run . +``` ## Entites diff --git a/actionsList.go b/actionsList.go index 430021c..7134156 100644 --- a/actionsList.go +++ b/actionsList.go @@ -49,22 +49,15 @@ func (m ActionListPageModel) Update(msg tea.Msg, ctx *context) (ActionListPageMo switch action { case Write: return m, gemlog.WritePostCMD(ctx.config) - case Read: + case Read, Delete, Edit: switchPageCmd := func() tea.Msg { return SwitchPages{Page: EntryList} } + actionToTakeCmd := func() tea.Msg { + return SelectActionToTake{ActionToTake: action} + } loadGemLogsCmd := gemlog.LoadGemlogCMD(ctx.config) - return m, tea.Batch(switchPageCmd, loadGemLogsCmd) - // case Edit: - // m.ui.page = EntryList - // m.ui.entryListPage.cursor = 0 - // m.ui.entryListPage.actionToTake = Edit - // return m, gemlog.LoadGemlogCMD(ctx.config) - // case Delete: - // m.ui.page = EntryList - // m.ui.entryListPage.cursor = 0 - // m.ui.entryListPage.actionToTake = Delete - // return m, gemlog.LoadGemlogCMD(m.context.config) + return m, tea.Batch(switchPageCmd, loadGemLogsCmd, actionToTakeCmd) } } } diff --git a/entryList.go b/entryList.go index af9e8c3..a579e65 100644 --- a/entryList.go +++ b/entryList.go @@ -13,6 +13,8 @@ type EntryListPageModel struct { cursor int } +type SelectActionToTake struct{ ActionToTake Action } + func InitialEntryListPageModel() EntryListPageModel { return EntryListPageModel{ cursor: 0, @@ -26,6 +28,8 @@ func (m EntryListPageModel) InitEntryListPage() tea.Cmd { func (m EntryListPageModel) Update(msg tea.Msg, ctx *context) (EntryListPageModel, tea.Cmd) { switch msg := msg.(type) { + case SelectActionToTake: + m.actionToTake = msg.ActionToTake case gemlog.GemLogsLoaded: m.entries = msg.Logs return m, nil @@ -39,7 +43,12 @@ func (m EntryListPageModel) Update(msg tea.Msg, ctx *context) (EntryListPageMode if m.cursor < len(actions)-1 { m.cursor++ } - // case "enter", " ": + case "enter", " ": + id := m.entries[m.cursor].ID + switch m.actionToTake { + case Delete: + return m, gemlog.DeleteGemlogCMD(ctx.config, id) + } // action := actions[m.cursor] // switch action { diff --git a/gemlog/db.go b/gemlog/db.go index e5f123f..2c9a18c 100644 --- a/gemlog/db.go +++ b/gemlog/db.go @@ -76,6 +76,34 @@ func listGemLogs(config *Config) ([]GemlogListEntry, error) { return entries, nil } +func DeleteGemlogEntry(config *Config, id string) error { + url := fmt.Sprintf("%s:%d/gemlog/%s", config.CouchDB.Host, config.CouchDB.Port, id) + req, err := http.NewRequest("DELETE", url, nil) + if err != nil { + return 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 fmt.Errorf("failed to send request: %w", err) + } + defer res.Body.Close() + + body, err := io.ReadAll(res.Body) + if err != nil { + return fmt.Errorf("failed to read response body: %w", err) + } + + if res.StatusCode < 200 || res.StatusCode >= 300 { + return fmt.Errorf("unexpected status code %d: %s", res.StatusCode, string(body)) + } + + return nil +} + func SaveGemlogEntry(config *Config, entry *GemlogEntry) error { url := fmt.Sprintf("%s:%d/gemlog/", config.CouchDB.Host, config.CouchDB.Port) diff --git a/gemlog/list.go b/gemlog/list.go index ce51dfc..e28d213 100644 --- a/gemlog/list.go +++ b/gemlog/list.go @@ -1,9 +1,21 @@ package gemlog import ( + "fmt" + tea "github.com/charmbracelet/bubbletea" ) +func DeleteGemlogCMD(config *Config, id string) tea.Cmd { + return func() tea.Msg { + err := DeleteGemlogEntry(config, id) + if err != nil { + return ErrorMsg{err} + } + return Notification(fmt.Sprintf("Gemlog with id: %s deleted", id)) + } +} + func LoadGemlogCMD(config *Config) tea.Cmd { return func() tea.Msg { logs, err := listGemLogs(config)