-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_controller.go
125 lines (106 loc) · 3.79 KB
/
user_controller.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
package controller
import (
"github.com/hacktiv8-fp-golang/final-project-04/internal/helper"
"github.com/hacktiv8-fp-golang/final-project-04/internal/model"
"github.com/hacktiv8-fp-golang/final-project-04/internal/service"
"fmt"
"net/http"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
// UserRegister godoc
// @Summary Register a new user
// @Description Endpoint to register a new user
// @Tags User
// @Accept json
// @Produce json
// @Param model.UserRegister body model.UserRegister true "create user"
// @Success 201 {object} model.UserRegisterResponse
// @Failure 400 {object} helper.ErrorResponse "Bad Request"
// @Failure 404 {object} helper.ErrorResponse "Not Found"
// @Failure 422 {object} helper.ErrorResponse "Invalid Request"
// @Failure 500 {object} helper.ErrorResponse "Server Error"
// @Router /users/register [post]
func Register(context *gin.Context) {
var user model.User
if err := context.ShouldBindJSON(&user); err != nil {
errorHandler := helper.UnprocessibleEntity("Invalid JSON body")
context.AbortWithStatusJSON(errorHandler.Status(), errorHandler)
return
}
user.Role = "customer"
userResponse, err := service.UserService.Register(&user)
if err != nil {
context.AbortWithStatusJSON(err.Status(), err)
return
}
context.JSON(http.StatusCreated, gin.H{
"id": userResponse.ID,
"full_name": userResponse.FullName,
"email": userResponse.Email,
"password": userResponse.Password,
"balance": userResponse.Balance,
"created_at": userResponse.CreatedAt,
})
}
// UserLogin godoc
// @Summary Authenticate a user
// @Description Endpoint to register a new user
// @Tags User
// @Accept json
// @Produce json
// @Param model.LoginCredential body model.LoginCredential true "authenticate user"
// @Success 200 {string} string "OK"
// @Failure 400 {object} helper.ErrorResponse "Bad Request"
// @Failure 404 {object} helper.ErrorResponse "Not Found"
// @Failure 422 {object} helper.ErrorResponse "Invalid Request"
// @Failure 500 {object} helper.ErrorResponse "Server Error"
// @Router /users/login [post]
func Login(context *gin.Context) {
var userLogin model.LoginCredential
if err := context.ShouldBindJSON(&userLogin); err != nil {
errorHandler := helper.UnprocessibleEntity("Invalid JSON body")
context.AbortWithStatusJSON(errorHandler.Status(), errorHandler)
return
}
token, err := service.UserService.Login(&userLogin)
if err != nil {
context.AbortWithStatusJSON(err.Status(), err)
return
}
context.JSON(http.StatusOK, gin.H{"token": token})
}
// UserTopup godoc
// @Summary Topup user balance
// @Description Endpoint to top-up user balance
// @Tags User
// @Accept json
// @Produce json
// @Param Authorization header string true "Bearer Token"
// @Param model.User body model.BalanceUpdate true "Amount to top-up"
// @Success 200 {string} string "OK"
// @Failure 400 {object} helper.ErrorResponse "Bad Request"
// @Failure 401 {object} helper.ErrorResponse "Unauthorized"
// @Failure 404 {object} helper.ErrorResponse "Not Found"
// @Failure 422 {object} helper.ErrorResponse "Invalid Request"
// @Failure 500 {object} helper.ErrorResponse "Server Error"
// @Security Bearer
// @Router /users/topup [patch]
func UpdateBalance(context *gin.Context) {
var balance model.BalanceUpdate
if err := context.ShouldBindJSON(&balance); err != nil {
errorHandler := helper.UnprocessibleEntity("Invalid JSON body")
context.AbortWithStatusJSON(errorHandler.Status(), errorHandler)
return
}
userData := context.MustGet("userData").(jwt.MapClaims)
userId := int(userData["id"].(float64))
updatedBalance, err := service.UserService.UpdateBalance(&balance, userId)
if err != nil {
context.AbortWithStatusJSON(err.Status(), err)
return
}
context.JSON(http.StatusOK, gin.H{
"message": fmt.Sprintf("Your balance has been successfully updated to Rp %d", updatedBalance),
})
}