From 9b5710f23d6cbda9f2b0e650cf8916e7707d5588 Mon Sep 17 00:00:00 2001
From: liuxiaolong <liuxiaolong@aiotlink.com>
Date: 星期一, 29 六月 2020 19:27:14 +0800
Subject: [PATCH] addPlateNo and findMyPlateNos

---
 controllers/user.go    |   29 +++++++++++++++++++++++++++++
 routers/router.go      |    2 ++
 models/userCar.go      |    4 ++--
 service/userService.go |   13 +++++++++++++
 models/user.go         |    2 +-
 5 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/controllers/user.go b/controllers/user.go
index 9cf24da..1947e7c 100644
--- a/controllers/user.go
+++ b/controllers/user.go
@@ -113,7 +113,36 @@
 		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()
diff --git a/models/user.go b/models/user.go
index 3d15d2d..46ee619 100644
--- a/models/user.go
+++ b/models/user.go
@@ -36,7 +36,7 @@
 
 func (u *User) SelectById(uid string) error {
 	o := orm.NewOrm()
-	err := o.Raw("select * from ? where id=?", u.TableName(), uid).QueryRow(u)
+	err := o.QueryTable(u.TableName()).Filter("id", uid).One(u)
 	return err
 }
 
diff --git a/models/userCar.go b/models/userCar.go
index 24eb587..881e48c 100644
--- a/models/userCar.go
+++ b/models/userCar.go
@@ -29,8 +29,8 @@
 func (uc *UserCar) Exist(userId string, plateNo string) bool {
 	var list []UserCar
 	o := orm.NewOrm()
-	i,_ := o.Raw("select * from ? where userId=? and plateNo=?", uc.TableName(), userId, plateNo).QueryRows(&list)
-	if i > 0 && len(list) >0 {
+	o.Raw("select * from ? where userId=? and plateNo=?", uc.TableName(), userId, plateNo).QueryRows(&list)
+	if len(list) >0 {
 		return true
 	}
 	return false
diff --git a/routers/router.go b/routers/router.go
index e2e6f18..b25ea35 100644
--- a/routers/router.go
+++ b/routers/router.go
@@ -47,4 +47,6 @@
 	beego.Router(preApi+"/code/new", &controllers.CodeController{}, "*:New")
 	beego.Router(preApi+"/user/login", &controllers.UserController{}, "*:Login")
 	beego.Router(preApi+"/user/logout", &controllers.UserController{}, "*:Logout")
+	beego.Router(preApi+"/user/addPlateNo", &controllers.UserController{}, "*:AddPlateNo")
+	beego.Router(preApi+"/user/myPlateNos", &controllers.UserController{}, "*:MyPlateNos")
 }
diff --git a/service/userService.go b/service/userService.go
index 1fe7435..15ed042 100644
--- a/service/userService.go
+++ b/service/userService.go
@@ -58,6 +58,19 @@
 }
 
 func (sv *UserService) AddPlateNo(userId, plateNo string) bool {
+	var uc models.UserCar
+	if uc.Exist(userId, plateNo) {
+		return true
+	}
+	tmp := models.UserCar{
+		Id: uuid.NewV4().String(),
+		UserId: userId,
+		PlateNo: plateNo,
+	}
+	_, err := tmp.Insert()
+	if err == nil {
+		return true
+	}
 	return false
 }
 

--
Gitblit v1.8.0