get updating posts working
This commit is contained in:
parent
972707e5fd
commit
6b8ba76018
6 changed files with 148 additions and 53 deletions
|
|
@ -9,11 +9,11 @@ import (
|
|||
|
||||
// GemlogEntry represents a single gemlog post entry
|
||||
type GemlogEntry struct {
|
||||
Title string `json:"title"`
|
||||
Slug string `json:"slug"`
|
||||
Date time.Time `json:"date"`
|
||||
Tags []string `json:"tags"`
|
||||
Gemtxt string `json:"gemtxt"`
|
||||
Title string `json:"title"`
|
||||
Slug string `json:"slug"`
|
||||
Date time.Time `json:"date"`
|
||||
// Tags []string `json:"tags"`
|
||||
Gemtxt string `json:"gemtxt"`
|
||||
}
|
||||
|
||||
type GemlogListEntry struct {
|
||||
|
|
|
|||
38
gemlog/db.go
38
gemlog/db.go
|
|
@ -119,7 +119,7 @@ func ReadGemlogEntry(config *Config, id string) (GemlogEntry, error) {
|
|||
Slug: rawData.Slug,
|
||||
Date: rawData.Date,
|
||||
Gemtxt: rawData.GemText,
|
||||
Tags: make([]string, 0),
|
||||
// Tags: make([]string, 0),
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -151,6 +151,42 @@ func DeleteGemlogEntry(config *Config, id string, rev string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func UpdateGemlogEntry(config *Config, entry *GemlogEntry, id string, rev string) error {
|
||||
url := fmt.Sprintf("%s:%d/gemlog/%s?rev=%s", config.CouchDB.Host, config.CouchDB.Port, id, rev)
|
||||
|
||||
// Marshal the entry struct to JSON
|
||||
jsonData, err := json.Marshal(entry)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to marshal entry: %w", err)
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(jsonData))
|
||||
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")
|
||||
slog.Info("Sending request to update gemlog entry", "url", url, "data", string(jsonData))
|
||||
|
||||
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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue