完善硬件管理接口
This commit is contained in:
parent
02aa809976
commit
800ce06907
@ -1,6 +1,7 @@
|
|||||||
package controller
|
package controller
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"background/logs"
|
||||||
"background/model"
|
"background/model"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"qiniupkg.com/x/log.v7"
|
"qiniupkg.com/x/log.v7"
|
||||||
@ -20,6 +21,8 @@ func AddHardware(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
e = hardware.CreateHardware()
|
e = hardware.CreateHardware()
|
||||||
if nil != e{
|
if nil != e{
|
||||||
|
resp.Status = -100
|
||||||
|
resp.Msg = e.Error()
|
||||||
log.Print(e)
|
log.Print(e)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -29,7 +32,21 @@ func AddHardware(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func DeleteHardWare(c *gin.Context) {
|
func DeleteHardWare(c *gin.Context) {
|
||||||
|
resp := RespBase{"unkown error",-231,nil}
|
||||||
|
defer func() {
|
||||||
|
c.JSON(200,resp)
|
||||||
|
}()
|
||||||
|
name := c.Query("name")
|
||||||
|
if name == ""{
|
||||||
|
return
|
||||||
|
}
|
||||||
|
e := model.DeleteHardware(name)
|
||||||
|
if nil != e{
|
||||||
|
logs.Error(e.Error())
|
||||||
|
}
|
||||||
|
resp.Msg = "OK"
|
||||||
|
resp.Status = 0
|
||||||
|
resp.Data = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateHardWare(c *gin.Context) {
|
func UpdateHardWare(c *gin.Context) {
|
||||||
@ -43,6 +60,7 @@ func ReadHardWare(c *gin.Context) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
limit,offset := GetPageParaFromQuery(c)
|
limit,offset := GetPageParaFromQuery(c)
|
||||||
|
log.Print(limit,offset)
|
||||||
hardware,e := model.GetHardwares(limit,offset)
|
hardware,e := model.GetHardwares(limit,offset)
|
||||||
if nil != e{
|
if nil != e{
|
||||||
return
|
return
|
||||||
|
@ -26,7 +26,6 @@ func (p *ElkEngine)Create(index string,types string,id string,data interface{})
|
|||||||
if (reflect.TypeOf(data).Kind() == reflect.String) || (reflect.TypeOf(data).Kind() == reflect.Struct){
|
if (reflect.TypeOf(data).Kind() == reflect.String) || (reflect.TypeOf(data).Kind() == reflect.Struct){
|
||||||
resp, err := p.cli.Index().
|
resp, err := p.cli.Index().
|
||||||
Index(index).
|
Index(index).
|
||||||
Type(types).
|
|
||||||
BodyJson(data).
|
BodyJson(data).
|
||||||
Do(context.Background())
|
Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -47,7 +46,6 @@ func (p *ElkEngine)Create(index string,types string,id string,data interface{})
|
|||||||
func (p *ElkEngine)Delete(index string,types string,id string) error{
|
func (p *ElkEngine)Delete(index string,types string,id string) error{
|
||||||
if nil != p{
|
if nil != p{
|
||||||
res, err := p.cli.Delete().Index(index).
|
res, err := p.cli.Delete().Index(index).
|
||||||
Type(types).
|
|
||||||
Id(id).
|
Id(id).
|
||||||
Do(context.Background())
|
Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -66,35 +64,40 @@ func (p *ElkEngine)Delete(index string,types string,id string) error{
|
|||||||
|
|
||||||
*/
|
*/
|
||||||
func (p *ElkEngine)Query(index string,query elastic.Query,data interface{},
|
func (p *ElkEngine)Query(index string,query elastic.Query,data interface{},
|
||||||
limit int,offset int) ([]interface{},error) {
|
limit int,offset int) ([]interface{},[]string,error) {
|
||||||
if nil != p{
|
if nil != p{
|
||||||
if(limit == 0){
|
if(limit == 0){
|
||||||
res, err := p.cli.
|
res, err := p.cli.Search(index).Query(query).Do(context.Background())
|
||||||
Search(index).
|
|
||||||
Query(query).Do(context.Background())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
print(err)
|
print(err)
|
||||||
return nil,err
|
return nil,nil,err
|
||||||
}
|
}
|
||||||
//var typ Employee
|
//var typ Employee
|
||||||
typ := reflect.TypeOf(data)
|
typ := reflect.TypeOf(data)
|
||||||
return res.Each(typ),nil
|
rets := res.Each(typ)
|
||||||
|
id := []string{}
|
||||||
|
for _,vs := range res.Hits.Hits{
|
||||||
|
id = append(id,vs.Id)
|
||||||
|
}
|
||||||
|
return rets,id,nil
|
||||||
}else{
|
}else{
|
||||||
res, err := p.cli.
|
res, err := p.cli.Search(index).Query(query).Size(limit).From(limit*offset).Do(context.Background())
|
||||||
Search(index).
|
|
||||||
Query(query).Size(limit).From(limit*offset).Do(context.Background())
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
print(err)
|
print(err)
|
||||||
return nil,err
|
return nil,nil,err
|
||||||
}
|
}
|
||||||
log.Print(res)
|
|
||||||
//var typ Employee
|
//var typ Employee
|
||||||
typ := reflect.TypeOf(data)
|
typ := reflect.TypeOf(data)
|
||||||
return res.Each(typ),nil
|
rets := res.Each(typ)
|
||||||
|
id := []string{}
|
||||||
|
for _,vs := range res.Hits.Hits{
|
||||||
|
id = append(id,vs.Id)
|
||||||
|
}
|
||||||
|
return rets,id,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
}else{
|
}else{
|
||||||
return nil,errors.New(ERROR_PTR)
|
return nil,nil,errors.New(ERROR_PTR)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -102,7 +105,6 @@ func (p *ElkEngine)Update(index string,types string,id string,data map[string]in
|
|||||||
if nil != p {
|
if nil != p {
|
||||||
_, err := p.cli.Update().
|
_, err := p.cli.Update().
|
||||||
Index(index).
|
Index(index).
|
||||||
Type(types).
|
|
||||||
Id(id).
|
Id(id).
|
||||||
Doc(data).
|
Doc(data).
|
||||||
Do(context.Background())
|
Do(context.Background())
|
||||||
|
4
main.go
4
main.go
@ -129,7 +129,9 @@ func main() {
|
|||||||
|
|
||||||
api.POST("/hardware",controller.AddHardware) // 新增硬件
|
api.POST("/hardware",controller.AddHardware) // 新增硬件
|
||||||
api.GET("/hardware",controller.ReadHardWare) // 读取硬件
|
api.GET("/hardware",controller.ReadHardWare) // 读取硬件
|
||||||
|
api.DELETE("/hardware",controller.DeleteHardWare) // 读取硬件
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
e := r.Run(":" + strconv.Itoa(config.GetPort()))
|
e := r.Run(":" + strconv.Itoa(config.GetPort()))
|
||||||
if nil != e {
|
if nil != e {
|
||||||
|
@ -27,7 +27,7 @@ func HardwareTypeMapping() (string){
|
|||||||
|
|
||||||
// this api is based on elasticsearch
|
// this api is based on elasticsearch
|
||||||
type Hardware struct {
|
type Hardware struct {
|
||||||
ID int32 `json:"id,omitempty"`
|
ID string `json:"_id,omitempty"`
|
||||||
BuyDate string `json:"buy_date,omitempty"` //购入时间
|
BuyDate string `json:"buy_date,omitempty"` //购入时间
|
||||||
Name string `json:"name,omitempty"` // 名字
|
Name string `json:"name,omitempty"` // 名字
|
||||||
Desc string `json:"desc,omitempty"` // 描述
|
Desc string `json:"desc,omitempty"` // 描述
|
||||||
@ -40,11 +40,15 @@ func (this *Hardware )CreateHardware( ) error{
|
|||||||
return errors.New(utils.ERRNULLPOINTER)
|
return errors.New(utils.ERRNULLPOINTER)
|
||||||
}
|
}
|
||||||
log.Print(this.Name)
|
log.Print(this.Name)
|
||||||
matchPhraseQuery := elastic.NewMatchQuery("name", this.Name)
|
matchPhraseQuery := elastic.NewMatchQuery("name", "stm32开发板")
|
||||||
existedHardware,e := QueryHardwares(matchPhraseQuery,10,1)
|
existedHardware,e := QueryHardwares(matchPhraseQuery,10,0)
|
||||||
log.Print(e,existedHardware)
|
log.Print(e,existedHardware)
|
||||||
if len(existedHardware) > 0{
|
|
||||||
return errors.New(ERR_COLUMN_EXISTED)
|
for _,v := range existedHardware{
|
||||||
|
if v.Name == this.Name{
|
||||||
|
log.Print(v.ID)
|
||||||
|
return errors.New(ERR_COLUMN_EXISTED)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
e = db.GetElastic().Create("hardware","0","",*this)
|
e = db.GetElastic().Create("hardware","0","",*this)
|
||||||
if nil != e{
|
if nil != e{
|
||||||
@ -58,24 +62,47 @@ func (this *Hardware )CreateHardware( ) error{
|
|||||||
|
|
||||||
func GetHardwares(limit int,size int) ([]Hardware,error){
|
func GetHardwares(limit int,size int) ([]Hardware,error){
|
||||||
var ret []Hardware
|
var ret []Hardware
|
||||||
data,e := db.GetElastic().Query("hardware",nil,Hardware{},limit,size)
|
data,ids,e := db.GetElastic().Query("hardware",nil,Hardware{},limit,size)
|
||||||
if nil != e{
|
if nil != e{
|
||||||
return nil,e
|
return nil,e
|
||||||
}
|
}
|
||||||
|
i := 0
|
||||||
for _,v := range data{
|
for _,v := range data{
|
||||||
ret = append(ret,v.(Hardware))
|
ret = append(ret,v.(Hardware))
|
||||||
|
ret[i].ID = ids[i]
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
return ret,nil
|
return ret,nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func QueryHardwares(query elastic.Query,limit int,offset int) ([]Hardware,error){
|
func QueryHardwares(query elastic.Query,limit int,offset int) ([]Hardware,error){
|
||||||
var ret []Hardware
|
var ret []Hardware
|
||||||
data,e := db.GetElastic().Query("hardware",query,Hardware{},limit,offset)
|
data,ids,e := db.GetElastic().Query("hardware",query,Hardware{},limit,offset)
|
||||||
|
log.Print(data)
|
||||||
if nil != e{
|
if nil != e{
|
||||||
return nil,e
|
return nil,e
|
||||||
}
|
}
|
||||||
|
i := 0
|
||||||
for _,v := range data{
|
for _,v := range data{
|
||||||
ret = append(ret,v.(Hardware))
|
ret = append(ret,v.(Hardware))
|
||||||
|
ret[i].ID = ids[i]
|
||||||
|
i++
|
||||||
}
|
}
|
||||||
return ret,nil
|
return ret,nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteHardware(name string) error{
|
||||||
|
matchPhraseQuery := elastic.NewMatchQuery("name", "stm32开发板")
|
||||||
|
existedHardware,e := QueryHardwares(matchPhraseQuery,10,0)
|
||||||
|
log.Print(e,existedHardware)
|
||||||
|
|
||||||
|
for _,v := range existedHardware{
|
||||||
|
if v.Name == name{
|
||||||
|
err := db.GetElastic().Delete("hardware","",v.ID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user