editing cakes by id

This commit is contained in:
Bronku 2025-01-31 22:26:31 +01:00
parent 353dffdd82
commit a09cee09c8
3 changed files with 19 additions and 2 deletions

View file

@ -19,6 +19,7 @@ func (c *controller) LoadRouter(pub fs.FS) {
serveMux.HandleFunc("GET /available_cakes", c.cakeOptions) serveMux.HandleFunc("GET /available_cakes", c.cakeOptions)
serveMux.HandleFunc("GET /edit_cakes", c.cakeOptions) serveMux.HandleFunc("GET /edit_cakes", c.cakeOptions)
serveMux.HandleFunc("GET /cake_editor", c.cakeEditor) serveMux.HandleFunc("GET /cake_editor", c.cakeEditor)
serveMux.HandleFunc("GET /cake_editor/{ID}", c.cakeEditor)
serveMux.HandleFunc("POST /new_cake", c.newCake) serveMux.HandleFunc("POST /new_cake", c.newCake)
c.Handler = serveMux c.Handler = serveMux

View file

@ -18,14 +18,29 @@ func (c *controller) cakeOptions(w http.ResponseWriter, r *http.Request) {
} }
func (c *controller) cakeEditor(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 { type cake struct {
Name string Name string
ID int ID int
Price int Price int
} }
w.Header().Set("Content-Type", "text/html") url := strings.Split(r.URL.String(), "/")
c.tmpl["cake_editor"].Execute(w, cake{"Hello", 1, 120})
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) { func (c *controller) newCake(w http.ResponseWriter, r *http.Request) {

View file

@ -9,4 +9,5 @@
</div> </div>
<input value="{{.ID}}" name="id" hidden></input> <input value="{{.ID}}" name="id" hidden></input>
<button type="submit">Submit</button> <button type="submit">Submit</button>
<button type="button" onclick="document.getElementById('cake_editor').innerText=''">Cancel</button>
</form> </form>