editing cakes by id
This commit is contained in:
parent
353dffdd82
commit
a09cee09c8
3 changed files with 19 additions and 2 deletions
|
|
@ -19,6 +19,7 @@ func (c *controller) LoadRouter(pub fs.FS) {
|
|||
serveMux.HandleFunc("GET /available_cakes", c.cakeOptions)
|
||||
serveMux.HandleFunc("GET /edit_cakes", c.cakeOptions)
|
||||
serveMux.HandleFunc("GET /cake_editor", c.cakeEditor)
|
||||
serveMux.HandleFunc("GET /cake_editor/{ID}", c.cakeEditor)
|
||||
serveMux.HandleFunc("POST /new_cake", c.newCake)
|
||||
|
||||
c.Handler = serveMux
|
||||
|
|
|
|||
19
handlers.go
19
handlers.go
|
|
@ -18,14 +18,29 @@ func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
func (c *controller) cakeEditor(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println("received get request at ", r.URL.String())
|
||||
type cake struct {
|
||||
Name string
|
||||
ID int
|
||||
Price int
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
c.tmpl["cake_editor"].Execute(w, cake{"Hello", 1, 120})
|
||||
url := strings.Split(r.URL.String(), "/")
|
||||
|
||||
if len(url) < 3 {
|
||||
fmt.Println("no arg")
|
||||
c.tmpl["cake_editor"].Execute(w, cake{"", 0, 0})
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.Atoi(url[2])
|
||||
if err != nil {
|
||||
fmt.Println("can't get info")
|
||||
c.tmpl["cake_editor"].Execute(w, cake{"", 0, 0})
|
||||
return
|
||||
}
|
||||
|
||||
c.tmpl["cake_editor"].Execute(w, cake{"Hello", id, 120})
|
||||
}
|
||||
|
||||
func (c *controller) newCake(w http.ResponseWriter, r *http.Request) {
|
||||
|
|
|
|||
|
|
@ -9,4 +9,5 @@
|
|||
</div>
|
||||
<input value="{{.ID}}" name="id" hidden></input>
|
||||
<button type="submit">Submit</button>
|
||||
<button type="button" onclick="document.getElementById('cake_editor').innerText=''">Cancel</button>
|
||||
</form>
|
||||
Loading…
Add table
Add a link
Reference in a new issue