liuxiaolong
2020-06-17 49f07b0ce7a6a9e0893a9a01199130f2fa0e619c
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
package controllers
 
import (
    "car-service/models"
    "encoding/json"
 
    "github.com/astaxie/beego"
)
 
// Operations about Users
type UserController struct {
    beego.Controller
}
 
// @Title CreateUser
// @Description create users
// @Param    body        body     models.User    true        "body for user content"
// @Success 200 {int} models.User.Id
// @Failure 403 {string} json "body is empty"
// @router / [post]
func (u *UserController) Post() {
    var user models.User
    json.Unmarshal(u.Ctx.Input.RequestBody, &user)
    uid := models.AddUser(user)
    u.Data["json"] = map[string]string{"uid": uid}
    u.ServeJSON()
}
 
// @Title GetAll
// @Description get all Users
// @Success 200 {object} models.User
// @router / [get]
func (u *UserController) GetAll() {
    users := models.GetAllUsers()
    u.Data["json"] = users
    u.ServeJSON()
}
 
// @Title Get
// @Description get user by uid
// @Param    uid        path     string    true        "The key for staticblock"
// @Success 200 {object} models.User
// @Failure 403 {string} json ":uid is empty"
// @router /:uid [get]
func (u *UserController) Get() {
    uid := u.GetString(":uid")
    if uid != "" {
        user, err := models.GetUser(uid)
        if err != nil {
            u.Data["json"] = err.Error()
        } else {
            u.Data["json"] = user
        }
    }
    u.ServeJSON()
}
 
// @Title Update
// @Description update the user
// @Param    uid        path     string    true        "The uid you want to update"
// @Param    body        body     models.User    true        "body for user content"
// @Success 200 {object} models.User
// @Failure 403 {string} json ":uid is not int"
// @router /:uid [put]
func (u *UserController) Put() {
    uid := u.GetString(":uid")
    if uid != "" {
        var user models.User
        json.Unmarshal(u.Ctx.Input.RequestBody, &user)
        uu, err := models.UpdateUser(uid, &user)
        if err != nil {
            u.Data["json"] = err.Error()
        } else {
            u.Data["json"] = uu
        }
    }
    u.ServeJSON()
}
 
// @Title Delete
// @Description delete the user
// @Param    uid        path     string    true        "The uid you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 {string} json  "uid is empty"
// @router /:uid [delete]
func (u *UserController) Delete() {
    uid := u.GetString(":uid")
    models.DeleteUser(uid)
    u.Data["json"] = "delete success!"
    u.ServeJSON()
}
 
// @Title Login
// @Description Logs user into the system
// @Param    username        query     string    true        "The username for login"
// @Param    password        query     string    true        "The password for login"
// @Success 200 {string} login success
// @Failure 403 {string} json  "user not exist"
// @router /login [get]
func (u *UserController) Login() {
    username := u.GetString("username")
    password := u.GetString("password")
    if models.Login(username, password) {
        u.Data["json"] = "login success"
    } else {
        u.Data["json"] = "user not exist"
    }
    u.ServeJSON()
}
 
// @Title logout
// @Description Logs out current logged in user session
// @Success 200 {string} logout success
// @router /logout [get]
func (u *UserController) Logout() {
    u.Data["json"] = "logout success"
    u.ServeJSON()
}