硬件管理后端接口实现

master
a7458969 2020-03-28 00:00:46 +08:00
parent f3e54bd866
commit ac8850da45
6 changed files with 66 additions and 15 deletions

View File

@ -2,9 +2,12 @@
<project version="4"> <project version="4">
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="7cf7b6b3-0082-44ef-bb0f-bfcc57e19eb1" name="Default Changelist" comment=""> <list default="true" id="7cf7b6b3-0082-44ef-bb0f-bfcc57e19eb1" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.gitignore" beforeDir="false" afterPath="$PROJECT_DIR$/.gitignore" afterDir="false" /> <change afterPath="$PROJECT_DIR$/utils/const.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/controller/mail.go" beforeDir="false" afterPath="$PROJECT_DIR$/controller/mail.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/controller/file.go" beforeDir="false" afterPath="$PROJECT_DIR$/controller/file.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/controller/hardware.go" beforeDir="false" afterPath="$PROJECT_DIR$/controller/hardware.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/main.go" beforeDir="false" afterPath="$PROJECT_DIR$/main.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/model/hardware.go" beforeDir="false" afterPath="$PROJECT_DIR$/model/hardware.go" afterDir="false" />
</list> </list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" /> <option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
@ -26,12 +29,20 @@
<expand /> <expand />
<select /> <select />
</component> </component>
<component name="FileTemplateManagerImpl">
<option name="RECENT_TEMPLATES">
<list>
<option value="Go File" />
</list>
</option>
</component>
<component name="GOROOT" path="D:/programs/GOROOT" /> <component name="GOROOT" path="D:/programs/GOROOT" />
<component name="Git.Settings"> <component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" /> <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component> </component>
<component name="ProjectId" id="1OeBjD7R4KFQzfWAqpMf9jlmzxr" /> <component name="ProjectId" id="1OeBjD7R4KFQzfWAqpMf9jlmzxr" />
<component name="PropertiesComponent"> <component name="PropertiesComponent">
<property name="DefaultGoTemplateProperty" value="Go File" />
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" /> <property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" /> <property name="WebServerToolWindowFactoryState" value="false" />
<property name="configurable.Global.GOPATH.is.expanded" value="true" /> <property name="configurable.Global.GOPATH.is.expanded" value="true" />

View File

@ -99,3 +99,34 @@ func (this *FileController) OnDownLoad(c *gin.Context) {
"/image/" +fileName) "/image/" +fileName)
c.Writer.Write(bytes) c.Writer.Write(bytes)
} }
func (this *FileController) OnThumbnail(c *gin.Context) {
resp := RespBase{Msg:"FAIL",Status: 211}
fileName := c.Param("file")
if "" == fileName{
c.JSON(200,resp)
return
}
file,e := os.Open("/home/ubuntu/api/bin/image/" +fileName)
if nil != e{
log.Print(e.Error())
c.JSON(200,resp)
return
}
bytes,e :=ioutil.ReadAll(file)
if nil != e{
log.Print(e.Error())
c.JSON(200,resp)
return
}
c.Header("X-Content-Type-Options", "nosniff")
c.Header("Content-Type","image/png")
c.Header("Content-Disposition",
"/image/" +fileName)
c.Writer.Write(bytes)
}

View File

@ -5,8 +5,6 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"qiniupkg.com/x/log.v7" "qiniupkg.com/x/log.v7"
) )
func AddHardware(c *gin.Context) { func AddHardware(c *gin.Context) {
resp := RespBase{"unkown error",-231,nil} resp := RespBase{"unkown error",-231,nil}
defer func() { defer func() {
@ -38,18 +36,16 @@ func UpdateHardWare(c *gin.Context) {
} }
func ReadHardWare(c *gin.Context) { func ReadHardWare(c *gin.Context) {
var req model.Hardware
rsp := RespBase{"ERR",-1,nil} rsp := RespBase{"ERR",-1,nil}
defer func() { defer func() {
c.JSON(200,rsp) c.JSON(200,rsp)
}() }()
e := c.BindJSON(&req)
if nil != e{
log.Error(e.Error())
return
}
limit,offset := GetPageParaFromQuery(c) limit,offset := GetPageParaFromQuery(c)
hardware,e := model.GetHardwares(limit,offset) hardware,e := model.GetHardwares(limit,offset)
if nil != e{
return
}
rsp.Data = hardware rsp.Data = hardware
rsp.Msg = "OK" rsp.Msg = "OK"
rsp.Status = 0 rsp.Status = 0

View File

@ -123,13 +123,14 @@ func main() {
api.DELETE("/article/:id",controller.DeleteArticle) ////删除文章 api.DELETE("/article/:id",controller.DeleteArticle) ////删除文章
api.POST("/image_upload",fileController.OnUpload) // 上传图片 api.POST("/image_upload",fileController.OnUpload) // 上传图片
api.GET("/image_download/:file",fileController.OnDownLoad) // 下载图片 api.GET("/image_download/:file",fileController.OnDownLoad) // 下载图片
api.GET("/image_thumbnail/:file",fileController.OnDownLoad) // 下载图片
api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型 api.GET("/doc_types",controller.ArticlesTypes) // 获取所有的文章类型
api.POST("/hardware",controller.AddHardware) // 新增硬件 api.POST("/hardware",controller.AddHardware) // 新增硬件
api.GET("/hardware",controller.ReadHardWare) // 读取硬件 api.GET("/hardware",controller.ReadHardWare) // 读取硬件
} }
e := r.Run(":" + strconv.Itoa(config.GetPort())) e := r.Run(":" + strconv.Itoa(config.GetPort()))
if nil != e { if nil != e {
log.Print(e.Error()) log.Print(e.Error())

View File

@ -2,10 +2,14 @@ package model
import ( import (
"background/db" "background/db"
"background/utils"
"github.com/pkg/errors" "github.com/pkg/errors"
"qiniupkg.com/x/log.v7" "qiniupkg.com/x/log.v7"
) )
const (
ERR_COLUMN_EXISTED = "column_existed"
)
func HardwareTypeMapping() (string){ func HardwareTypeMapping() (string){
return `"mappings":{ return `"mappings":{
"hardware":{ "hardware":{
@ -13,7 +17,6 @@ func HardwareTypeMapping() (string){
"id":{"type":"keyword"}, "id":{"type":"keyword"},
"name":{"type":"text"}, "name":{"type":"text"},
"desc":{"type":"text"}, "desc":{"type":"text"},
"id":{"type":"interger"},
"pic":{"type":"doc"}, "pic":{"type":"doc"},
"doc":{"type":"doc"} "doc":{"type":"doc"}
} }
@ -33,9 +36,13 @@ type Hardware struct {
func (this *Hardware )CreateHardware( ) error{ func (this *Hardware )CreateHardware( ) error{
if nil == this{ if nil == this{
return errors.New("null pointer") return errors.New(utils.ERRNULLPOINTER)
} }
e := db.GetElastic().Create("hardware","0","",*this) name,e := GetHardwares(1,1)
if len(name) != 0{
return errors.New(ERR_COLUMN_EXISTED)
}
e = db.GetElastic().Create("hardware","0","",*this)
if nil != e{ if nil != e{
log.Print(e.Error()) log.Print(e.Error())
return e return e

5
utils/const.go Normal file
View File

@ -0,0 +1,5 @@
package utils
const (
ERRNULLPOINTER = "nullpointer"
)