-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
149 lines (127 loc) · 3.73 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
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
package v1
import (
"net/http"
"time"
"github.com/freeusd/solebtc/Godeps/_workspace/src/github.com/gin-gonic/gin"
"github.com/freeusd/solebtc/errors"
"github.com/freeusd/solebtc/models"
)
type signupPayload struct {
Email string `json:"email" binding:"required,email"`
Address string `json:"address" binding:"required"`
RefererID int64 `json:"referer_id,omitempty" binding:"-"`
}
func userWithSignupPayload(p signupPayload) models.User {
return models.User{
Email: p.Email,
Address: p.Address,
}
}
// Signup creates a new user with unique email, address
func Signup(createUser dependencyCreateUser, getUserByID dependencyGetUserByID) gin.HandlerFunc {
return func(c *gin.Context) {
payload := signupPayload{}
if err := c.BindJSON(&payload); err != nil {
return
}
user := userWithSignupPayload(payload)
// assign referer_id to user
referer, _ := getUserByID(payload.RefererID)
user.RefererID = referer.ID
if err := createUser(user); err != nil {
switch err.ErrCode {
case errors.ErrCodeDuplicateEmail:
c.AbortWithError(http.StatusConflict, err)
case errors.ErrCodeDuplicateAddress:
c.AbortWithError(http.StatusConflict, err)
default:
c.AbortWithError(http.StatusInternalServerError, err)
}
return
}
c.JSON(http.StatusOK, user)
}
}
// VerifyEmail updates user's status to verified if current status is unverified
func VerifyEmail(
getSessionByToken dependencyGetSessionByToken,
getUserByID dependencyGetUserByID,
updateUserStatus dependencyUpdateUserStatus,
) gin.HandlerFunc {
return func(c *gin.Context) {
token := c.Query("token")
// check session lifetime
session, err := getSessionByToken(token)
if session.UpdatedAt.Add(3 * time.Hour).Before(time.Now()) {
if err != nil {
c.AbortWithError(http.StatusUnauthorized, err)
} else {
c.AbortWithStatus(http.StatusUnauthorized)
}
return
}
// get user
user, err := getUserByID(session.UserID)
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
// check user status
if user.Status == models.UserStatusBanned {
c.AbortWithStatus(http.StatusForbidden)
return
}
// update user
if err := updateUserStatus(user.ID, models.UserStatusVerified); err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.Status(http.StatusOK)
}
}
// UserInfo returns user's info as response
func UserInfo(getUserByID dependencyGetUserByID) gin.HandlerFunc {
return func(c *gin.Context) {
authToken := c.MustGet("auth_token").(models.AuthToken)
user, err := getUserByID(authToken.UserID)
if err != nil {
// user is already authorized
// if get user error
// it must be internal server error
// do not need to check existence of user
// although error code can be ErrCodeNotFound
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, user)
}
}
// RefereeList returns user's referee list as response
func RefereeList(
getRefereesSinceID dependencyGetRefereesSince,
getRefereesUntilID dependencyGetRefereesUntil,
) gin.HandlerFunc {
return func(c *gin.Context) {
authToken := c.MustGet("auth_token").(models.AuthToken)
// parse pagination args
isSince, separator, limit, err := parsePagination(c)
if err != nil {
c.AbortWithStatus(http.StatusBadRequest)
return
}
// get result according to args
result := []models.User{}
var syserr *errors.Error
if isSince {
result, syserr = getRefereesSinceID(authToken.UserID, separator, limit)
} else {
result, syserr = getRefereesUntilID(authToken.UserID, separator, limit)
}
// response with result or error
if syserr != nil {
c.AbortWithError(http.StatusInternalServerError, syserr)
return
}
c.JSON(http.StatusOK, result)
}
}