-
Notifications
You must be signed in to change notification settings - Fork 2
/
enter_organisation_name.go
78 lines (62 loc) · 1.94 KB
/
enter_organisation_name.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package supporter
import (
"net/http"
"github.com/ministryofjustice/opg-go-common/template"
"github.com/ministryofjustice/opg-modernising-lpa/internal/page"
"github.com/ministryofjustice/opg-modernising-lpa/internal/validation"
)
type enterOrganisationNameData struct {
App page.AppData
Errors validation.List
Form *organisationNameForm
}
func EnterOrganisationName(tmpl template.Template, organisationStore OrganisationStore, memberStore MemberStore, sessionStore SessionStore) page.Handler {
return func(appData page.AppData, w http.ResponseWriter, r *http.Request) error {
data := &enterOrganisationNameData{
App: appData,
Form: &organisationNameForm{},
}
if r.Method == http.MethodPost {
data.Form = readOrganisationNameForm(r, "fullOrganisationOrCompanyName")
data.Errors = data.Form.Validate()
if data.Errors.None() {
member, err := memberStore.GetAny(r.Context())
if err != nil {
return err
}
organisation, err := organisationStore.Create(r.Context(), member, data.Form.Name)
if err != nil {
return err
}
loginSession, err := sessionStore.Login(r)
if err != nil {
return page.Paths.Supporter.Start.Redirect(w, r, appData)
}
loginSession.OrganisationID = organisation.ID
loginSession.OrganisationName = organisation.Name
if err := sessionStore.SetLogin(r, w, loginSession); err != nil {
return err
}
return page.Paths.Supporter.OrganisationCreated.Redirect(w, r, appData)
}
}
return tmpl(w, data)
}
}
type organisationNameForm struct {
Name string
Label string
}
func readOrganisationNameForm(r *http.Request, label string) *organisationNameForm {
return &organisationNameForm{
Name: page.PostFormString(r, "name"),
Label: label,
}
}
func (f *organisationNameForm) Validate() validation.List {
var errors validation.List
errors.String("name", f.Label, f.Name,
validation.Empty(),
validation.StringTooLong(100))
return errors
}