Commit 0c4b0773 by chenxianqi

add api

parent 3210f86b
package controllers package controllers
import ( import (
"encoding/json"
"kefu_server/configs" "kefu_server/configs"
"kefu_server/models"
"kefu_server/services" "kefu_server/services"
) )
...@@ -56,7 +58,34 @@ func (c *WorkOrderController) Comment() { ...@@ -56,7 +58,34 @@ func (c *WorkOrderController) Comment() {
// PostType add work order type // PostType add work order type
func (c *WorkOrderController) PostType() { func (c *WorkOrderController) PostType() {
c.JSON(configs.ResponseSucess, "success", nil)
// GetAuthInfo
auth := c.GetAuthInfo()
admin := services.GetAdminRepositoryInstance().GetAdmin(auth.UID)
if admin != nil && admin.Root != 1 {
c.JSON(configs.ResponseFail, "没有权限!", nil)
}
// request body
var workOrderType models.WorkOrderType
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &workOrderType); err != nil {
c.JSON(configs.ResponseFail, "参数有误,请检查!", nil)
}
// validation
if workOrderType.Title == "" {
c.JSON(configs.ResponseFail, "类型标题不能为空!!", nil)
}
isNew, _, err := c.WorkOrderTypeRepository.Add(workOrderType)
if err != nil {
c.JSON(configs.ResponseFail, "添加失败!", err)
}
if !isNew {
c.JSON(configs.ResponseFail, "类型名称已存在!", err)
}
c.JSON(configs.ResponseSucess, "添加成功!", nil)
} }
// GetType get work order type // GetType get work order type
......
...@@ -2,6 +2,7 @@ package services ...@@ -2,6 +2,7 @@ package services
import ( import (
"kefu_server/models" "kefu_server/models"
"time"
"github.com/astaxie/beego/logs" "github.com/astaxie/beego/logs"
"github.com/astaxie/beego/orm" "github.com/astaxie/beego/orm"
...@@ -9,8 +10,9 @@ import ( ...@@ -9,8 +10,9 @@ import (
// WorkOrderTypeRepositoryInterface interface // WorkOrderTypeRepositoryInterface interface
type WorkOrderTypeRepositoryInterface interface { type WorkOrderTypeRepositoryInterface interface {
GetWorkOrder() *models.WorkOrderType GetWorkOrderType() *models.WorkOrderType
Update(id int64,params *orm.Params) (int64, error) Update(id int64, params *orm.Params) (int64, error)
Add(data models.WorkOrderType) (bool, int64, error)
} }
// WorkOrderTypeRepository struct // WorkOrderTypeRepository struct
...@@ -25,10 +27,18 @@ func GetWorkOrderTypeRepositoryInstance() *WorkOrderTypeRepository { ...@@ -25,10 +27,18 @@ func GetWorkOrderTypeRepositoryInstance() *WorkOrderTypeRepository {
return instance return instance
} }
// Add add a WorkOrderType
func (r *WorkOrderTypeRepository) Add(data models.WorkOrderType) (bool, int64, error) {
data.CreateAt = time.Now().Unix()
isNew, row, err := r.o.ReadOrCreate(&data, "title")
if err != nil {
logs.Warn("Add add a WorkOrderType------------", err)
}
return isNew, row, err
}
// Update WorkOrderType Info // Update WorkOrderType Info
func (r *WorkOrderTypeRepository) Update(id int64,params orm.Params) (int64, error) { func (r *WorkOrderTypeRepository) Update(id int64, params orm.Params) (int64, error) {
index, err := r.q.Filter("id", id).Update(params) index, err := r.q.Filter("id", id).Update(params)
if err != nil { if err != nil {
logs.Warn("Update WorkOrderType Info------------", err) logs.Warn("Update WorkOrderType Info------------", err)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment