diff options
| author | David Schlachter <t480-debian-git@schlachter.ca> | 2026-01-08 01:02:29 -0500 |
|---|---|---|
| committer | David Schlachter <t480-debian-git@schlachter.ca> | 2026-01-08 01:02:29 -0500 |
| commit | b84cad66f701e274d8e8abd65fb46f95b340bcda (patch) | |
| tree | fd8c64ba172f7c77f2d32cb08cd2cf9c6b74b44f /add.go | |
| parent | e2b5d4e8bf68dc5b790a5424fbc30ebda46c2f04 (diff) | |
Application is now interactiv
Diffstat (limited to 'add.go')
| -rw-r--r-- | add.go | 106 |
1 files changed, 69 insertions, 37 deletions
@@ -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) + } } |
