get select edit, read, and delete working
This commit is contained in:
parent
b680a2f5d7
commit
99548e67b7
5 changed files with 62 additions and 13 deletions
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
11
entryList.go
11
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 {
|
||||
|
|
|
|||
28
gemlog/db.go
28
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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue