diff --git a/examples/gno.land/p/demo/forms/forms_json.gno b/examples/gno.land/p/demo/forms/forms_json.gno new file mode 100644 index 00000000000..464e10d5c9a --- /dev/null +++ b/examples/gno.land/p/demo/forms/forms_json.gno @@ -0,0 +1,52 @@ +package forms + +import ( + "std" + "strings" + + "gno.land/p/demo/json" + "gno.land/p/demo/ufmt" +) + +func (b *json.NodeBuilder) WriteFormFields(key string, fields []Field) *json.NodeBuilder { + b.WriteArray(key, func(builder *json.ArrayBuilder) { + for _, field := range fields { + builder.WriteObject(func(builder *json.NodeBuilder) { + builder.WriteString("label", field.Label). + WriteString("fieldType", field.FieldType). + WriteBool("required", field.Required) + }) + } + }) + return b +} + +func (b *json.NodeBuilder) WriteFormSubmission(key string, submission *Submission) *json.NodeBuilder { + b.WriteObject(key, func(builder *json.NodeBuilder) { + builder.WriteString("submittedAt", submission.SubmittedAt.Format("2006-01-02 15:04:05")). + WriteString("answers", strings.ReplaceAll(submission.Answers, "\"", "'")) + }) + return b +} + +func (b *json.NodeBuilder) WriteForm(key string, value *Form) *json.NodeBuilder { + b.WriteString("id", value.ID). + WriteString("owner", value.Owner.String()). + WriteString("title", value.Title). + WriteString("description", value.Description). + WriteString("createdAt", value.CreatedAt.Format("2006-01-02 15:04:05")). + WriteFormFields("fields", value.Fields) + return b +} + +func (b *json.ArrayBuilder) WriteForm(key string, value *Form) *json.ArrayBuilder { + b.WriteObject(func(builder *json.NodeBuilder) { + builder.WriteString("id", value.ID). + WriteString("owner", value.Owner.String()). + WriteString("title", value.Title). + WriteString("description", value.Description). + WriteString("createdAt", value.CreatedAt.Format("2006-01-02 15:04:05")). + WriteFormFields("fields", value.Fields) + }) + return b +} diff --git a/examples/gno.land/r/demo/forms/forms.gno b/examples/gno.land/r/demo/forms/forms.gno index 2bc86528a1b..7c2e35bfe7b 100644 --- a/examples/gno.land/r/demo/forms/forms.gno +++ b/examples/gno.land/r/demo/forms/forms.gno @@ -29,20 +29,7 @@ func GetForms() string { builder.WriteArray("forms", func(builder *json.ArrayBuilder) { for _, form := range db.Forms { builder.WriteObject(func(builder *json.NodeBuilder) { - builder.WriteString("id", form.ID). - WriteString("owner", form.Owner.String()). - WriteString("title", form.Title). - WriteString("description", form.Description). - WriteString("createdAt", form.CreatedAt.Format("2006-01-02 15:04:05")). - WriteArray("fields", func(builder *json.ArrayBuilder) { - for _, field := range form.Fields { - builder.WriteObject(func(builder *json.NodeBuilder) { - builder.WriteString("label", field.Label). - WriteString("fieldType", field.FieldType). - WriteBool("required", field.Required) - }) - } - }) + builder.WriteForm("form", form) }) } }) @@ -63,24 +50,11 @@ func GetFormByID(id string) string { builder := json.Builder() - builder.WriteString("id", form.ID). - WriteString("owner", form.Owner.String()). - WriteString("title", form.Title). - WriteString("description", form.Description). - WriteString("createdAt", form.CreatedAt.Format("2006-01-02 15:04:05")). - WriteArray("fields", func(builder *json.ArrayBuilder) { - for _, field := range form.Fields { - builder.WriteObject(func(builder *json.NodeBuilder) { - builder.WriteString("label", field.Label).WriteString("fieldType", field.FieldType).WriteBool("required", field.Required) - }) - } - }). + builder.WriteForm("form", form). WriteObject("submissions", func(builder *json.NodeBuilder) { formSubmissions := db.GetSubmissionsByFormID(form.ID) for _, submission := range formSubmissions { - builder.WriteObject(submission.Author.String(), func(builder *json.NodeBuilder) { - builder.WriteString("submittedAt", submission.SubmittedAt.Format("2006-01-02 15:04:05")).WriteString("answers", strings.ReplaceAll(submission.Answers, "\"", "'")) - }) + builder.WriteFormSubmission(submission.Author.String(), submission) } })