/
contactform.go
40 lines (32 loc) · 1.1 KB
/
contactform.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package endpoints
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/EngineerKamesh/igb/igweb/common"
"github.com/EngineerKamesh/igb/igweb/shared/forms"
"github.com/EngineerKamesh/igb/igweb/submissions"
"go.isomorphicgo.org/go/isokit"
)
func ContactFormEndpoint(env *common.Env) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var fields map[string]string
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Print("Encountered error when attempting to read the request body: ", err)
}
err = json.Unmarshal(reqBody, &fields)
if err != nil {
log.Print("Encountered error when attempting to unmarshal json data: ", err)
}
formParams := isokit.FormParams{ResponseWriter: w, Request: r, UseFormFieldsForValidation: true, FormFields: fields}
contactForm := forms.NewContactForm(&formParams)
validationResult := contactForm.Validate()
if validationResult == true {
submissions.ProcessContactForm(env, contactForm)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(contactForm.Errors())
})
}