Skip to content

Commit

Permalink
update example to demo path param parser
Browse files Browse the repository at this point in the history
  • Loading branch information
crhntr committed Aug 23, 2024
1 parent f01e813 commit 5bfacf0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 19 deletions.
27 changes: 12 additions & 15 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,24 @@ type EditRowPage struct {
Error error
}

func (b *Backend) SubmitFormEditRow(request *http.Request, fruit string) EditRowPage {
func (b *Backend) SubmitFormEditRow(request *http.Request, fruitID int) EditRowPage {
if fruitID < 0 || fruitID >= len(b.data) {
return EditRowPage{Error: fmt.Errorf("fruit not found")}
}
count, err := strconv.Atoi(request.FormValue("count"))
if err != nil {
return EditRowPage{Error: err, Row: Row{Name: fruit}}
}
for i := range b.data {
if b.data[i].Name == fruit {
b.data[i].Value = count
return EditRowPage{Error: nil, Row: b.data[i]}
}
return EditRowPage{Error: err, Row: b.data[fruitID]}
}
return EditRowPage{Error: fmt.Errorf("fruit not found")}
row := b.data[fruitID]
row.Value = count
return EditRowPage{Error: nil, Row: row}
}

func (b *Backend) GetFormEditRow(fruit string) EditRowPage {
for i := range b.data {
if b.data[i].Name == fruit {
return EditRowPage{Error: nil, Row: b.data[i]}
}
func (b *Backend) GetFormEditRow(fruitID int) EditRowPage {
if fruitID < 0 || fruitID >= len(b.data) {
return EditRowPage{Error: fmt.Errorf("fruit not found")}
}
return EditRowPage{Error: fmt.Errorf("fruit not found")}
return EditRowPage{Error: nil, Row: b.data[fruitID]}
}

type Row struct {
Expand Down
19 changes: 15 additions & 4 deletions example/template_routes.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5bfacf0

Please sign in to comment.