-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathuser.go
101 lines (88 loc) · 3.07 KB
/
user.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
package controllers
import (
"fmt"
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/twreporter/go-api/globals"
"github.com/twreporter/go-api/models"
)
// GetUser given userID, this func will try to get the user record, joined with users_mailgroup table, from DB
func (mc *MembershipController) GetUser(c *gin.Context) (int, gin.H, error) {
userID := c.Param("userID")
user, err := mc.Storage.GetUserByID(userID)
if err != nil {
return toResponse(err)
}
roles := make([]gin.H, len(user.Roles))
for i, role := range user.Roles {
roles[i] = gin.H{
"id": role.ID, // does frontend need ID?
"name": role.Name,
"name_en": role.NameEn,
"key": role.Key,
}
}
var activated *time.Time
if user.Activated.Valid && !user.Activated.Time.IsZero() {
activated = &user.Activated.Time
}
mailGroups := make([]string, 0)
for _, group := range user.MailGroups {
for key, value := range globals.Conf.Mailchimp.InterestIDs {
if value == group.MailgroupID {
mailGroups = append(mailGroups, key)
break
}
}
}
readPreferenceArr := make([]string, 0)
if user.ReadPreference.Valid {
readPreferenceArr = strings.Split(user.ReadPreference.String, ",")
}
return http.StatusOK, gin.H{"status": "success", "data": gin.H{
"user_id": userID,
"first_name": user.FirstName.String,
"last_name": user.LastName.String,
"email": user.Email.String,
"registration_date": user.RegistrationDate.Time,
"activated": activated,
"roles": roles,
"read_preference": readPreferenceArr,
"maillist": mailGroups,
"agree_data_collection": user.AgreeDataCollection,
"read_posts_count": user.ReadPostsCount,
"read_posts_sec": user.ReadPostsSec,
},
}, nil
}
// SetUser given userID and POST body, this func will try to create record in the related table,
// and build the relationship between records and user
func (mc *MembershipController) SetUser(c *gin.Context) (int, gin.H, error) {
userID := c.Param("userID")
var preferences models.UserPreference
err := c.BindJSON(&preferences)
if err != nil {
fmt.Println("Error decoding JSON:", err)
}
// Convert maillist values using the mapping array
maillists := make([]string, 0)
for _, maillist := range preferences.Maillist {
convertedMaillist, exists := globals.Conf.Mailchimp.InterestIDs[maillist]
if !exists {
return http.StatusBadRequest, gin.H{"status": "error", "message": "invalid maillist value"}, errors.New("Invalid maillist value")
}
maillists = append(maillists, convertedMaillist)
}
// Call UpdateReadPreferenceOfUser to save the preferences.ReadPreference to DB
if err = mc.Storage.UpdateReadPreferenceOfUser(userID, preferences.ReadPreference); err != nil {
return toResponse(err)
}
// Call CreateMaillistOfUser to save the preferences.Maillist to DB
if err = mc.Storage.CreateMaillistOfUser(userID, maillists); err != nil {
return toResponse(err)
}
return http.StatusCreated, gin.H{"status": "ok", "record": preferences}, nil
}