7
"xelbot.com/reprogl/api/backend"
8
"xelbot.com/reprogl/container"
9
"xelbot.com/reprogl/models"
10
"xelbot.com/reprogl/models/repositories"
11
"xelbot.com/reprogl/session"
12
"xelbot.com/reprogl/views"
15
type updateProfileResponse struct {
16
Valid bool `json:"valid"`
17
Errors []backend.FormError `json:"errors,omitempty"`
20
func ProfileAction(app *container.Application) http.HandlerFunc {
21
return func(w http.ResponseWriter, r *http.Request) {
23
if !session.HasIdentity(r.Context()) {
24
http.Redirect(w, r, container.GenerateURL("login"), http.StatusFound)
29
if identity, ok := session.GetIdentity(r.Context()); ok {
30
repo := repositories.UserRepository{DB: app.DB}
31
user, _ = repo.Find(identity.ID)
35
app.ServerError(w, errors.New("profile logic error: user is null"))
39
subscriptionSettingsRepo := repositories.EmailSubscriptionRepository{DB: app.DB}
40
subscrSettings, err := subscriptionSettingsRepo.FindOrCreate(user.Email, models.SubscriptionReplyComment)
42
app.ServerError(w, err)
46
templateData := views.NewProfilePageData(user, subscrSettings)
47
err = views.WriteTemplate(w, "profile.gohtml", templateData)
49
app.ServerError(w, err)
56
func UpdateProfile(app *container.Application) http.HandlerFunc {
57
return func(w http.ResponseWriter, r *http.Request) {
59
if !session.HasIdentity(r.Context()) {
60
http.Redirect(w, r, container.GenerateURL("login"), http.StatusFound)
65
if identity, ok := session.GetIdentity(r.Context()); ok {
66
repo := repositories.UserRepository{DB: app.DB}
67
user, _ = repo.Find(identity.ID)
71
app.ServerError(w, errors.New("profile logic error: user is null"))
75
profileData := backend.ProfileDTO{
78
Username: r.PostFormValue("username"),
79
Email: r.PostFormValue("email"),
80
DisplayName: r.PostFormValue("displayName"),
83
if r.PostFormValue("gender") == "male" {
84
profileData.IsMale = true
86
profileData.IsMale = false
90
statusCode := http.StatusOK
92
apiResponse, err := backend.SendProfileData(profileData)
94
app.ServerError(w, err)
99
if apiResponse != nil {
100
result := updateProfileResponse{
102
Errors: apiResponse.Violations,
105
if apiResponse.Violations != nil && len(apiResponse.Violations) > 0 {
109
responseData = result
111
if apiResponse.User != nil {
112
subscriptionSettingsRepo := repositories.EmailSubscriptionRepository{DB: app.DB}
113
subscrSettings, err := subscriptionSettingsRepo.FindOrCreate(
114
apiResponse.User.Email,
115
models.SubscriptionReplyComment)
117
app.ServerError(w, err)
121
formValue := r.PostFormValue("reply_subscribe")
123
if subscrSettings.BlockSending {
124
err = subscriptionSettingsRepo.Subscribe(subscrSettings.ID)
127
if !subscrSettings.BlockSending {
128
err = subscriptionSettingsRepo.Unsubscribe(subscrSettings.ID)
132
app.ServerError(w, err)
138
jsonResponse(w, statusCode, responseData)