DRY up api module

This commit is contained in:
Dan Sosedoff 2015-05-02 20:32:16 -05:00
parent cb3c3e0e2e
commit 04fe0023b7
2 changed files with 21 additions and 59 deletions

View File

@ -67,13 +67,7 @@ func Connect(c *gin.Context) {
func GetDatabases(c *gin.Context) { func GetDatabases(c *gin.Context) {
names, err := DbClient.Databases() names, err := DbClient.Databases()
serveResult(names, err, c)
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, names)
} }
func RunQuery(c *gin.Context) { func RunQuery(c *gin.Context) {
@ -84,7 +78,7 @@ func RunQuery(c *gin.Context) {
return return
} }
GetHandleQuery(query, c) HandleQuery(query, c)
} }
func ExplainQuery(c *gin.Context) { func ExplainQuery(c *gin.Context) {
@ -95,40 +89,22 @@ func ExplainQuery(c *gin.Context) {
return return
} }
GetHandleQuery(fmt.Sprintf("EXPLAIN ANALYZE %s", query), c) HandleQuery(fmt.Sprintf("EXPLAIN ANALYZE %s", query), c)
} }
func GetSchemas(c *gin.Context) { func GetSchemas(c *gin.Context) {
names, err := DbClient.Schemas() names, err := DbClient.Schemas()
serveResult(names, err, c)
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, names)
} }
func GetTables(c *gin.Context) { func GetTables(c *gin.Context) {
names, err := DbClient.Tables() names, err := DbClient.Tables()
serveResult(names, err, c)
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, names)
} }
func GetTable(c *gin.Context) { func GetTable(c *gin.Context) {
res, err := DbClient.Table(c.Params.ByName("table")) res, err := DbClient.Table(c.Params.ByName("table"))
serveResult(res, err, c)
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, res)
} }
func GetTableRows(c *gin.Context) { func GetTableRows(c *gin.Context) {
@ -158,13 +134,7 @@ func GetTableRows(c *gin.Context) {
} }
res, err := DbClient.TableRows(c.Params.ByName("table"), opts) res, err := DbClient.TableRows(c.Params.ByName("table"), opts)
serveResult(res, err, c)
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, res)
} }
func GetTableInfo(c *gin.Context) { func GetTableInfo(c *gin.Context) {
@ -195,26 +165,15 @@ func GetConnectionInfo(c *gin.Context) {
func GetActivity(c *gin.Context) { func GetActivity(c *gin.Context) {
res, err := DbClient.Activity() res, err := DbClient.Activity()
if err != nil { serveResult(res, err, c)
c.JSON(400, NewError(err))
return
}
c.JSON(200, res)
} }
func GetTableIndexes(c *gin.Context) { func GetTableIndexes(c *gin.Context) {
res, err := DbClient.TableIndexes(c.Params.ByName("table")) res, err := DbClient.TableIndexes(c.Params.ByName("table"))
serveResult(res, err, c)
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, res)
} }
func GetHandleQuery(query string, c *gin.Context) { func HandleQuery(query string, c *gin.Context) {
result, err := DbClient.Query(query) result, err := DbClient.Query(query)
if err != nil { if err != nil {
@ -236,11 +195,5 @@ func GetHandleQuery(query string, c *gin.Context) {
func GetBookmarks(c *gin.Context) { func GetBookmarks(c *gin.Context) {
bookmarks, err := bookmarks.ReadAll(bookmarks.Path()) bookmarks, err := bookmarks.ReadAll(bookmarks.Path())
serveResult(bookmarks, err, c)
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, bookmarks)
} }

View File

@ -79,8 +79,17 @@ func serveStaticAsset(path string, c *gin.Context) {
data, err := data.Asset("static" + path) data, err := data.Asset("static" + path)
if err != nil { if err != nil {
c.String(400, err.Error()) c.String(400, err.Error())
c.Abort() return
} }
c.Data(200, assetContentType(path), data) c.Data(200, assetContentType(path), data)
} }
func serveResult(result interface{}, err error, c *gin.Context) {
if err != nil {
c.JSON(400, NewError(err))
return
}
c.JSON(200, result)
}