diff --git a/controller/hardware.go b/controller/hardware.go index 18631de..eb76fc6 100644 --- a/controller/hardware.go +++ b/controller/hardware.go @@ -1,6 +1,7 @@ package controller import ( + "background/logs" "background/model" "github.com/gin-gonic/gin" "qiniupkg.com/x/log.v7" @@ -20,6 +21,8 @@ func AddHardware(c *gin.Context) { } e = hardware.CreateHardware() if nil != e{ + resp.Status = -100 + resp.Msg = e.Error() log.Print(e) return } @@ -29,7 +32,21 @@ func AddHardware(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) { @@ -43,6 +60,7 @@ func ReadHardWare(c *gin.Context) { }() limit,offset := GetPageParaFromQuery(c) + log.Print(limit,offset) hardware,e := model.GetHardwares(limit,offset) if nil != e{ return diff --git a/db/elasticEngine.go b/db/elasticEngine.go index 8fc87c5..a11e5d9 100644 --- a/db/elasticEngine.go +++ b/db/elasticEngine.go @@ -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){ resp, err := p.cli.Index(). Index(index). - Type(types). BodyJson(data). Do(context.Background()) 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{ if nil != p{ res, err := p.cli.Delete().Index(index). - Type(types). Id(id). Do(context.Background()) 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{}, - limit int,offset int) ([]interface{},error) { + limit int,offset int) ([]interface{},[]string,error) { if nil != p{ if(limit == 0){ - res, err := p.cli. - Search(index). - Query(query).Do(context.Background()) + res, err := p.cli.Search(index).Query(query).Do(context.Background()) if err != nil { print(err) - return nil,err + return nil,nil,err } //var typ Employee 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{ - res, err := p.cli. - Search(index). - Query(query).Size(limit).From(limit*offset).Do(context.Background()) + res, err := p.cli.Search(index).Query(query).Size(limit).From(limit*offset).Do(context.Background()) if err != nil { print(err) - return nil,err + return nil,nil,err } - log.Print(res) + //var typ Employee 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{ - 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 { _, err := p.cli.Update(). Index(index). - Type(types). Id(id). Doc(data). Do(context.Background()) diff --git a/main.go b/main.go index 8b8bbb5..69c8f85 100644 --- a/main.go +++ b/main.go @@ -129,7 +129,9 @@ func main() { api.POST("/hardware",controller.AddHardware) // 新增硬件 api.GET("/hardware",controller.ReadHardWare) // 读取硬件 - + api.DELETE("/hardware",controller.DeleteHardWare) // 读取硬件 + + } e := r.Run(":" + strconv.Itoa(config.GetPort())) if nil != e { diff --git a/model/hardware.go b/model/hardware.go index 4157537..4df4401 100644 --- a/model/hardware.go +++ b/model/hardware.go @@ -27,7 +27,7 @@ func HardwareTypeMapping() (string){ // this api is based on elasticsearch type Hardware struct { - ID int32 `json:"id,omitempty"` + ID string `json:"_id,omitempty"` BuyDate string `json:"buy_date,omitempty"` //购入时间 Name string `json:"name,omitempty"` // 名字 Desc string `json:"desc,omitempty"` // 描述 @@ -40,11 +40,15 @@ func (this *Hardware )CreateHardware( ) error{ return errors.New(utils.ERRNULLPOINTER) } log.Print(this.Name) - matchPhraseQuery := elastic.NewMatchQuery("name", this.Name) - existedHardware,e := QueryHardwares(matchPhraseQuery,10,1) + matchPhraseQuery := elastic.NewMatchQuery("name", "stm32开发板") + existedHardware,e := QueryHardwares(matchPhraseQuery,10,0) 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) if nil != e{ @@ -58,24 +62,47 @@ func (this *Hardware )CreateHardware( ) error{ func GetHardwares(limit int,size int) ([]Hardware,error){ 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{ return nil,e } + i := 0 for _,v := range data{ ret = append(ret,v.(Hardware)) + ret[i].ID = ids[i] + i++ } return ret,nil } func QueryHardwares(query elastic.Query,limit int,offset int) ([]Hardware,error){ 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{ return nil,e } + i := 0 for _,v := range data{ ret = append(ret,v.(Hardware)) + ret[i].ID = ids[i] + i++ } 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 } \ No newline at end of file