/
organizations_handler.go
228 lines (198 loc) · 5.64 KB
/
organizations_handler.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package app
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
)
func OrganizationsIndexHandler(c echo.Context) error {
filters := &OrganizationFilters{}
if err := c.Bind(filters); err != nil {
return err
}
filters.AccountId = r.User(c).AccountId
p, orgs, err := PaginateOrganizationsFilters(c.Request().Context(), r.DB(c), r.Pagination(c), filters)
if err != nil {
return err
}
return r.Inertia(c).Render(http.StatusOK, "Organizations/Index", map[string]interface{}{
"filters": filters,
"organizations": map[string]interface{}{
"current_page": p.CurrentPage,
"data": orgs.ToMap(
"id",
"name",
"phone",
"city",
"deleted_at",
),
"links": p.Links(),
"path": p.Path,
"per_page": p.PerPage,
"prev_page_url": p.PreviousPageUrl(),
"next_page_url": p.NextPageUrl(),
"first_page_url": p.FirstPageUrl(),
"last_page_url": p.LastPageUrl(),
"last_page": p.PagesCount,
"from": p.From(),
"to": p.To(),
"total": p.ItemsCount,
},
})
}
func OrganizationsCreateHandler(c echo.Context) error {
return r.Inertia(c).Render(http.StatusOK, "Organizations/Create", nil)
}
func OrganizationsStoreHandler(c echo.Context) error {
input := &struct {
Name string `json:"name"`
Email *string `json:"email"`
Phone *string `json:"phone"`
Address *string `json:"address"`
City *string `json:"city"`
Region *string `json:"region"`
Country *string `json:"country"`
PostalCode *string `json:"postal_code"`
}{}
if err := c.Bind(input); err != nil {
return err
}
va := NewValidator()
va.Required("name", input.Name, "The name field is required.")
if va.HasErrors() {
if err := r.Session(c).SetErrors(va.ErrorMessageMap()); err != nil {
return err
}
return c.Redirect(http.StatusFound, "/organizations/create")
}
user := r.User(c)
org := NewOrganization()
org.AccountId = user.AccountId
org.Name = input.Name
org.Email = input.Email
org.Phone = input.Phone
org.Address = input.Address
org.City = input.City
org.Region = input.Region
org.Country = input.Country
org.PostalCode = input.PostalCode
if err := CreateOrganization(c.Request().Context(), r.DB(c), org); err != nil {
return err
}
if err := r.Session(c).SetFlashSuccess("Organization created."); err != nil {
return err
}
return c.Redirect(http.StatusFound, "/organizations")
}
func OrganizationsEditHandler(c echo.Context) error {
id := ParamInt64(c, "id")
if id == 0 {
return echo.NewHTTPError(http.StatusNotFound)
}
org, err := GetOrganizationById(c.Request().Context(), r.DB(c), id)
if err != nil {
if IsErrNoRows(err) {
return echo.NewHTTPError(http.StatusNotFound)
} else {
return err
}
}
if err := LoadOrganizationWithContacts(c.Request().Context(), r.DB(c), org); err != nil {
return err
}
return r.Inertia(c).Render(http.StatusOK, "Organizations/Edit", map[string]interface{}{
"organization": map[string]interface{}{
"id": org.Id,
"name": org.Name,
"email": org.Email,
"phone": org.Phone,
"address": org.Address,
"city": org.City,
"region": org.Region,
"country": org.Country,
"postal_code": org.PostalCode,
"deleted_at": org.DeletedAt,
"contacts": org.Contacts.ToMap(
"id",
"name",
"city",
"phone",
),
},
})
}
func OrganizationsUpdateHandler(c echo.Context) error {
id := ParamInt64(c, "id")
if id == 0 {
return echo.NewHTTPError(http.StatusNotFound)
}
org, err := GetOrganizationById(c.Request().Context(), r.DB(c), id)
if err != nil {
if IsErrNoRows(err) {
return echo.NewHTTPError(http.StatusNotFound)
} else {
return err
}
}
input := &struct {
Name string `json:"name"`
Email *string `json:"email"`
Phone *string `json:"phone"`
Address *string `json:"address"`
City *string `json:"city"`
Region *string `json:"region"`
Country *string `json:"country"`
PostalCode *string `json:"postal_code"`
}{}
if err := c.Bind(input); err != nil {
return err
}
va := NewValidator()
va.Required("name", input.Name, "The name field is required.")
if va.HasErrors() {
if err := r.Session(c).SetErrors(va.ErrorMessageMap()); err != nil {
return err
}
return c.Redirect(http.StatusFound, fmt.Sprintf("/organizations/%v/edit", id))
}
org.Name = input.Name
org.Email = input.Email
org.Phone = input.Phone
org.Address = input.Address
org.City = input.City
org.Region = input.Region
org.Country = input.Country
org.PostalCode = input.PostalCode
if err := UpdateOrganization(c.Request().Context(), r.DB(c), org); err != nil {
return err
}
if err := r.Session(c).SetFlashSuccess("Organization updated."); err != nil {
return err
}
return c.Redirect(http.StatusFound, fmt.Sprintf("/organizations/%v/edit", id))
}
func OrganizationsDestroyHandler(c echo.Context) error {
id := ParamInt64(c, "id")
if id == 0 {
return echo.NewHTTPError(http.StatusNotFound)
}
if err := SoftDeleteOrganization(c.Request().Context(), r.DB(c), id); err != nil {
return err
}
if err := r.Session(c).SetFlashSuccess("Organization deleted."); err != nil {
return err
}
return c.Redirect(http.StatusFound, fmt.Sprintf("/organizations/%v/edit", id))
}
func OrganizationsRestoreHandler(c echo.Context) error {
id := ParamInt64(c, "id")
if id == 0 {
return echo.NewHTTPError(http.StatusNotFound)
}
if err := RestoreOrganization(c.Request().Context(), r.DB(c), id); err != nil {
return err
}
if err := r.Session(c).SetFlashSuccess("Organization restored."); err != nil {
return err
}
return c.Redirect(http.StatusFound, fmt.Sprintf("/organizations/%v/edit", id))
}