summaryrefslogtreecommitdiff
path: root/add.go
diff options
context:
space:
mode:
authorDavid Schlachter <t480-debian-git@schlachter.ca>2026-01-08 01:02:29 -0500
committerDavid Schlachter <t480-debian-git@schlachter.ca>2026-01-08 01:02:29 -0500
commitb84cad66f701e274d8e8abd65fb46f95b340bcda (patch)
treefd8c64ba172f7c77f2d32cb08cd2cf9c6b74b44f /add.go
parente2b5d4e8bf68dc5b790a5424fbc30ebda46c2f04 (diff)
Application is now interactiv
Diffstat (limited to 'add.go')
-rw-r--r--add.go106
1 files changed, 69 insertions, 37 deletions
diff --git a/add.go b/add.go
index 40c45ee..2b358fe 100644
--- a/add.go
+++ b/add.go
@@ -6,54 +6,86 @@ import (
"fmt"
"io"
"net/http"
+
+ tea "github.com/charmbracelet/bubbletea"
)
const apiVersion = 6
-func addCard(c *http.Client, front, back string) error {
- noteRequest := addNote{
- Action: "addNote",
- Version: apiVersion,
- Params: addNoteParams{
- Note: note{
- DeckName: deckName,
- ModelName: modelName,
- Fields: fields{
- Front: front,
- Back: back,
- },
- Options: options{
- AllowDuplicate: false,
- DuplicateScope: "deck",
+type addNote struct {
+ Action string `json:"action"`
+ Version int `json:"version"`
+ Params addNoteParams `json:"params"`
+}
+
+type addNoteParams struct {
+ Note note `json:"note"`
+}
+
+type note struct {
+ DeckName string `json:"deckName"`
+ ModelName string `json:"modelName"`
+ // Fields will not be trivial to generalize
+ Fields fields `json:"fields"`
+ Options options `json:"options"`
+}
+
+type fields struct {
+ Front string `json:"Front"`
+ Back string `json:"Back"`
+}
+
+type options struct {
+ AllowDuplicate bool `json:"allowDuplicate"`
+ DuplicateScope string `json:"duplicateScope"`
+}
+
+func addCard(c *http.Client, front, back string) tea.Cmd {
+ return func() tea.Msg {
+ noteRequest := addNote{
+ Action: "addNote",
+ Version: apiVersion,
+ Params: addNoteParams{
+ Note: note{
+ DeckName: deckName,
+ ModelName: modelName,
+ Fields: fields{
+ Front: front,
+ Back: back,
+ },
+ Options: options{
+ AllowDuplicate: false,
+ DuplicateScope: "deck",
+ },
},
},
- },
- }
+ }
- jsonBytes, err := json.Marshal(noteRequest)
- if err != nil {
- return fmt.Errorf("marshaling JSON: %s", err)
- }
+ jsonBytes, err := json.Marshal(noteRequest)
+ if err != nil {
+ return errMsg(fmt.Errorf("marshaling JSON: %s", err))
+ }
- req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonBytes))
- req.Header.Set("Content-Type", "application/json")
+ req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(jsonBytes))
+ req.Header.Set("Content-Type", "application/json")
- resp, err := c.Do(req)
- if err != nil {
- return fmt.Errorf("making request: %s", err)
- }
- defer resp.Body.Close()
+ resp, err := c.Do(req)
+ if err != nil {
+ return errMsg(fmt.Errorf("making request: %s", err))
+ }
+ defer resp.Body.Close()
- body, _ := io.ReadAll(resp.Body)
+ body, _ := io.ReadAll(resp.Body)
- var jsonResp struct {
- Error string `json:"error"`
- }
+ var jsonResp struct {
+ Error string `json:"error"`
+ }
- json.Unmarshal(body, &jsonResp)
- if jsonResp.Error != "" {
- return fmt.Errorf("creating card: %s", jsonResp.Error)
- }
+ json.Unmarshal(body, &jsonResp)
+ if jsonResp.Error != "" {
+ return errMsg(fmt.Errorf("creating card: %s", jsonResp.Error))
+ }
- return nil
+ return wordAddedMsg(front)
+ }
}