package controllers
|
|
import (
|
"car-service/extend/code"
|
"car-service/models"
|
"car-service/service"
|
"car-service/vo"
|
"encoding/json"
|
"fmt"
|
"github.com/astaxie/beego"
|
"net/http"
|
)
|
|
// Operations about Users
|
type UserController struct {
|
beego.Controller
|
}
|
|
|
// @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
|
u.Data["json"] = user
|
|
}
|
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")
|
var user = models.User{
|
Id: uid,
|
}
|
|
i, err := user.DeleteById()
|
if err ==nil && i >0 {
|
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() {
|
phoneNum := u.GetString("phoneNum")
|
cod := u.GetString("code")
|
resp := code.Code{}
|
fmt.Println("phoneNum:", phoneNum, "code:", cod)
|
if phoneNum == "" || cod == "" {
|
resp.Success= false
|
resp.Status= http.StatusBadRequest
|
resp.Data= "参数有误"
|
} else {
|
var sv service.UserService
|
b, info, e := sv.Login(phoneNum, cod)
|
|
if b {
|
resp.Success= true
|
resp.Status= http.StatusOK
|
resp.Data= *info
|
} else {
|
resp.Success= false
|
resp.Status= http.StatusBadRequest
|
resp.Data= e.Error()
|
}
|
}
|
|
u.Data["json"] = resp
|
u.ServeJSON()
|
}
|
|
// @Title logout
|
// @Description Logs out current logged in user session
|
// @Success 200 {string} logout success
|
// @router /logout [get]
|
func (u *UserController) Logout() {
|
resp := code.Code{}
|
resp.Success = true
|
resp.Status = http.StatusOK
|
resp.Data = "退出成功"
|
u.Data["json"] = resp
|
u.ServeJSON()
|
}
|
|
// @Param reqBody body vo.AddPlateNoVo true "绑定车牌号参数"
|
// @router /addPlateNo [post]
|
func (u *UserController) AddPlateNo() {
|
var reqBody vo.AddPlateNoVo
|
body := u.Ctx.Input.RequestBody
|
err := json.Unmarshal(body, &reqBody)
|
resp := code.Code{}
|
if err != nil {
|
resp.Success = false
|
resp.Status = http.StatusBadRequest
|
resp.Data = "参数有误"
|
} else {
|
var sv service.UserService
|
if sv.AddPlateNo(reqBody.UserId, reqBody.PlateNo) {
|
resp.Success = true
|
resp.Status = http.StatusOK
|
resp.Data = "添加成功"
|
} else {
|
resp.Success = false
|
resp.Status = http.StatusBadRequest
|
resp.Data = "添加失败"
|
}
|
}
|
u.Data["json"] = resp
|
u.ServeJSON()
|
}
|
|
// @router /myPlateNos [get]
|
func (u *UserController) MyPlateNos() {
|
userId := u.GetString("userId")
|
var uc models.UserCar
|
all, err := uc.GetByUserId(userId)
|
var nos = make([]string, 0)
|
if err == nil && all != nil {
|
for _,p := range all {
|
nos = append(nos, p.PlateNo)
|
}
|
}
|
resp := code.Code{
|
Success: true,
|
Status: http.StatusOK,
|
Data: nos,
|
}
|
u.Data["json"] = resp
|
u.ServeJSON()
|
}
|