-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage_deputy_contact_details.go
60 lines (49 loc) · 1.91 KB
/
manage_deputy_contact_details.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package server
import (
"fmt"
"github.com/ministryofjustice/opg-sirius-supervision-deputy-hub/internal/sirius"
"github.com/ministryofjustice/opg-sirius-supervision-deputy-hub/internal/util"
"net/http"
)
type DeputyContactDetailsInformation interface {
UpdateDeputyContactDetails(sirius.Context, int, sirius.DeputyContactDetails) error
}
type manageDeputyContactDetailsVars struct {
AppVars
}
func renderTemplateForManageDeputyContactDetails(client DeputyContactDetailsInformation, tmpl Template) Handler {
return func(app AppVars, w http.ResponseWriter, r *http.Request) error {
ctx := getContext(r)
app.PageName = "Manage deputy contact details"
vars := manageDeputyContactDetailsVars{AppVars: app}
switch r.Method {
case http.MethodGet:
return tmpl.ExecuteTemplate(w, "page", vars)
case http.MethodPost:
form := sirius.DeputyContactDetails{
DeputySubType: app.DeputyDetails.DeputySubType.SubType,
DeputyFirstName: r.PostFormValue("deputy-first-name"),
DeputySurname: r.PostFormValue("deputy-last-name"),
OrganisationName: r.PostFormValue("organisation-name"),
AddressLine1: r.PostFormValue("address-line-1"),
AddressLine2: r.PostFormValue("address-line-2"),
AddressLine3: r.PostFormValue("address-line-3"),
Town: r.PostFormValue("town"),
County: r.PostFormValue("county"),
Postcode: r.PostFormValue("postcode"),
PhoneNumber: r.PostFormValue("telephone"),
Email: r.PostFormValue("email"),
}
err := client.UpdateDeputyContactDetails(ctx, app.DeputyId(), form)
if verr, ok := err.(sirius.ValidationError); ok {
vars.Errors = util.RenameErrors(verr.Errors)
return tmpl.ExecuteTemplate(w, "page", vars)
} else if err != nil {
return err
}
return Redirect(fmt.Sprintf("/%d?success=deputyDetails", app.DeputyId()))
default:
return StatusError(http.StatusMethodNotAllowed)
}
}
}