refine navigation and loading enteries
This commit is contained in:
parent
99548e67b7
commit
8414414f98
10 changed files with 148 additions and 41 deletions
50
gemlog/db.go
50
gemlog/db.go
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue