-
Notifications
You must be signed in to change notification settings - Fork 1
/
account-api.go
134 lines (109 loc) · 2.74 KB
/
account-api.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
package main
import (
"log"
"net/http"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
func accountAPI(r chi.Router) {
r.Group(func(r chi.Router) {
r.Use(RequireAccount())
r.Get("/app", getApp)
r.Route("/emailupdate/{EMAIL}", func(r chi.Router) {
r.Post("/", updateEmail)
})
r.Post("/nameupdate", updateName)
r.Post("/resend", resend)
r.Post("/delete", deleteAccount)
r.Post("/workspaces", createWorkspace)
})
}
func getApp(w http.ResponseWriter, r *http.Request) {
type response struct {
Mode string `json:"mode"`
Account *Account `json:"account"`
Workspaces []*Workspace `json:"workspaces"`
Memberships []*Member `json:"memberships"`
Subscriptions []*Subscription `json:"subscriptions"`
}
s := GetEnv(r).Service
s.UpdateLatestActivityNow()
ss := s.GetSubscriptionsByAccount()
log.Println(len(ss))
render.JSON(w, r, response{
Mode: s.GetConfig().Mode,
Account: s.GetAccountObject(),
Workspaces: s.GetWorkspaces(),
Memberships: s.GetMembersByAccount(),
Subscriptions: s.GetSubscriptionsByAccount(),
})
}
func updateEmail(w http.ResponseWriter, r *http.Request) {
email := chi.URLParam(r, "EMAIL")
s := GetEnv(r).Service
err := s.UpdateEmail(email)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
return
}
type updateNameRequest struct {
Name string `json:"name"`
}
func (p *updateNameRequest) Bind(r *http.Request) error {
return nil
}
func updateName(w http.ResponseWriter, r *http.Request) {
data := &updateNameRequest{}
if err := render.Bind(r, data); err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
s := GetEnv(r).Service
err := s.UpdateName(data.Name)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
return
}
func deleteAccount(w http.ResponseWriter, r *http.Request) {
s := GetEnv(r).Service
err := s.DeleteAccount()
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
return
}
func resend(w http.ResponseWriter, r *http.Request) {
s := GetEnv(r).Service
err := s.ResendEmail()
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
return
}
// Workspace
type createWorkspaceRequest struct {
Name string `json:"name"`
}
func (p *createWorkspaceRequest) Bind(r *http.Request) error {
return nil
}
func createWorkspace(w http.ResponseWriter, r *http.Request) {
data := &createWorkspaceRequest{}
if err := render.Bind(r, data); err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
s := GetEnv(r).Service
workspace, _, _, err := s.CreateWorkspace(data.Name)
if err != nil {
_ = render.Render(w, r, ErrInvalidRequest(err))
return
}
render.JSON(w, r, workspace)
}