livebud / bud

The Full-Stack Web Framework for Go
MIT License
5.53k stars 181 forks source link

Is it possible to send structs from svelte file? #324

Closed Fuerback closed 1 year ago

Fuerback commented 1 year ago

I'm trying the framework and the idea is to present information from a database, but also allow the users to update some of the fields, for example:

type PersonalIfno struct {
    Name    string  `json:"name"`
    Address Address `json:"address"`
    Age     int     `json:"age"`
}

I would present this information using a form and the user could edit the address and the age (just for example purposes), in this case I'd like to have a route that receives a PersonalIfno struct, like the example below:

// Edit personal info
// POST edit
func (c *Controller) Edit(personalInfo PersonalIfno) {
     // save the new info in the data base
}

But I don't know if sending it through the frontend/svelte layer is possible, could you help me?

Fuerback commented 1 year ago

Yep, I just figure out how to do it.

type Form struct {
    Notes  string `json:"notes"`
    Status string `json:"status"`
}

// Create a build
// POST
func (c *Controller) Create(form Form) (builds []*Build, err error) {
    log.Println("NOTES ", form.Notes)
    log.Println("STATUS ", form.Status)
    return c.Index(context.Background())
}

Svelte file:

<form method="post" action="/" class="button" id={build.clnumber}>  
          <select class="status" name="status" id="status">
            <option key="Good">Good</option>
            <option key="Bad">Bad</option>
            <option key="Undefined">Undefined</option>
          </select>
          <textarea form={build.clnumber} class="textarea" rows="4" cols="50" name="notes" placeholder="notes"></textarea>
          <input type="submit" value="Update" />
</form>

It should work. Would be nice to have a similar example in Bud's documentation.