前言
对日常的crud操作,除了不同表的model不同,基本上大部分操作都一致。为了避免同类型劳动重复操作,特意简制了http服务端的手脚架。
待更
包
import "github.com/fwhezfwhez/model_convert"
list
package main
import (
"encoding/json"
"fmt"
"github.com/fwhezfwhez/model_convert"
)
func main() {
type LingqianOrder struct {
Id int `gorm:"column:id;default:" json:"id" form:"id"`
OrderId string `gorm:"column:order_id;default:" json:"order_id" form:"order_id"`
GameId int `gorm:"column:game_id;default:" json:"game_id" form:"game_id"`
UserId int `gorm:"column:user_id;default:" json:"user_id" form:"user_id"`
OpenId string `gorm:"column:open_id;default:" json:"open_id" form:"open_id"`
State int `gorm:"column:state;default:" json:"state" form:"state"`
Request json.RawMessage `gorm:"column:request;default:" json:"request" form:"request"`
Response string `gorm:"column:response;default:" json:"response" form:"response"`
}
fmt.Println(model_convert.GenerateListAPI(LingqianOrder{}, false, map[string]string{
"${model}": "payModel.LingqianOrder",
"${handler_name}" : "HTTPListLingqianOrder",
"${handle_error}": `common.SaveError(e)`,
}))
}
生成:
// Auto generate by github.com/fwhezfwhez/model_convert.GenerateList().
func HTTPListLingqianOrder(c *gin.Context) {
var engine = db.DB.Model(&payModel.LingqianOrder{})
id := c.DefaultQuery("id", "")
if id != "" {
engine = engine.Where("id != ?", id)
}
orderId := c.DefaultQuery("order_id", "")
if orderId != "" {
engine = engine.Where("order_id != ?", orderId)
}
gameId := c.DefaultQuery("game_id", "")
if gameId != "" {
engine = engine.Where("game_id != ?", gameId)
}
userId := c.DefaultQuery("user_id", "")
if userId != "" {
engine = engine.Where("user_id != ?", userId)
}
openId := c.DefaultQuery("open_id", "")
if openId != "" {
engine = engine.Where("open_id != ?", openId)
}
state := c.DefaultQuery("state", "")
if state != "" {
engine = engine.Where("state != ?", state)
}
response := c.DefaultQuery("response", "")
if response != "" {
engine = engine.Where("response != ?", response)
}
page := c.DefaultQuery("page", "1")
size := c.DefaultQuery("size", "20")
orderBy := c.DefaultQuery("order_by", "")
var count int
if e:= engine.Count(&count).Error; e!=nil {
common.SaveError(e)
c.JSON(500, gin.H{"message": errorx.Wrap(e).Error()})
return
}
var list = make([]payModel.LingqianOrder, 0, 20)
if count == 0 {
c.JSON(200, gin.H{"message": "success", "count": 0, "data": list})
return
}
limit, offset := util.ToLimitOffset(size, page, count)
engine = engine.Limit(limit).Offset(offset)
if orderBy != "" {
engine = engine.Order(util.GenerateOrderBy(orderBy))
}
if e:= engine.Find(&list).Error; e!=nil {
common.SaveError(e)
c.JSON(500, gin.H{"message": errorx.Wrap(e).Error()})
return
}
c.JSON(200, gin.H{"message": "success", "count": 0, "data": list})
}
get-one
Note:
// Generate get-one api code.
// To completely use these code, you might import:
// "github.com/fwhezfwhez/errorx"
// you can get 'errorx.Wrap(e)' above
//
// - ${db_instance} "db.DB"
// - ${handler_name} "HTTPListUser"
// - ${model} "model.User"
// - ${handle_error} "fmt.Println(e)"
func GenerateGetOneAPI()
Usage:
package main
import (
"encoding/json"
"fmt"
"github.com/fwhezfwhez/model_convert"
)
func main() {
type VxTemplateUser struct {
Id int `gorm:"column:id;default:" json:"id" form:"id"`
GameId int `gorm:"column:game_id;default:" json:"game_id" form:"game_id"`
UserId int `gorm:"column:user_id;default:" json:"user_id" form:"user_id"`
OpenId string `gorm:"column:open_id;default:" json:"open_id" form:"open_id"`
TemplateId string `gorm:"column:template_id;default:" json:"template_id" form:"template_id"`
State int `gorm:"column:state;default:" json:"state" form:"state"`
}
rs := model_convert.GenerateGetOneAPI(VxTemplateUser{}, map[string]string{
"${model}": "payModel.LingqianOrder",
"${handler_name}" : "HTTPGetOneLingqianOrder",
"${handle_error}": `common.SaveError(e)`,
})
fmt.Println(rs)
}
Output:
// Auto generate by github.com/fwhezfwhez/model_convert.GenerateGetOneAPI().
func HTTPGetOneLingqianOrder(c *gin.Context) {
id := c.Param("id")
idInt, e := strconv.Atoi(id)
if e!=nil {
c.JSON(400, gin.H{"message": fmt.Sprintf("param 'id' requires int but got %s", id)})
return
}
var count int
if e:=db.DB.Model(&payModel.LingqianOrder{}).Where("id=?", idInt).Count(&count).Error; e!=nil {
common.SaveError(e)
c.JSON(500, gin.H{"message": errorx.Wrap(e).Error()})
return
}
if count ==0 {
c.JSON(200, gin.H{"message": fmt.Sprintf("id '%s' record not found", id)})
return
}
var instance payModel.LingqianOrder
if e:=db.DB.Model(&payModel.LingqianOrder{}).Where("id=?", id).First(&instance).Error; e!=nil {
common.SaveError(e)
c.JSON(500, gin.H{"message": errorx.Wrap(e).Error()})
return
}
c.JSON(200, gin.H{"message": "success", "data": instance})
}
add-one
Note:
// Generate add one api code.
// To completely use these code, you might import:
// "github.com/fwhezfwhez/errorx"
// you can get 'errorx.Wrap(e)' above
//
// - ${db_instance} "db.DB"
// - ${handler_name} "HTTPListUser"
// - ${model} "model.User"
// - ${handle_error} "fmt.Println(e, string(debug.Stack()))"
func GenerateAddOneAPI()
package main
import (
"encoding/json"
"fmt"
"github.com/fwhezfwhez/model_convert"
)
func main() {
type VxTemplateUser struct {
Id int `gorm:"column:id;default:" json:"id" form:"id"`
GameId int `gorm:"column:game_id;default:" json:"game_id" form:"game_id"`
UserId int `gorm:"column:user_id;default:" json:"user_id" form:"user_id"`
OpenId string `gorm:"column:open_id;default:" json:"open_id" form:"open_id"`
TemplateId string `gorm:"column:template_id;default:" json:"template_id" form:"template_id"`
State int `gorm:"column:state;default:" json:"state" form:"state"`
}
rs := model_convert.GenerateAddOneAPI(VxTemplateUser{}, map[string]string{
"${model}": "payModel.LingqianOrder",
"${handler_name}" : "HTTPAddLingqianOrder",
"${handle_error}": `common.SaveError(e)`,
})
fmt.Println(rs)
}
Output:
// Auto generate by github.com/fwhezfwhez/model_convert.GenerateAddOneAPI().
func HTTPAddLingqianOrder (c *gin.Context) {
var param payModel.LingqianOrder
if e := c.Bind(¶m); e!=nil {
c.JSON(400, gin.H{"message": errorx.Wrap(e).Error()})
return
}
if e:=db.DB.Model(&payModel.LingqianOrder{}).Create(¶m).Error; e!=nil {
common.SaveError(e)
c.JSON(500, gin.H{"message": errorx.Wrap(e).Error()})
return
}
c.JSON(200, gin.H{"message": "success", "data": param})
}
delete-one
Note:
// Generate delete one api code.
// To completely use these code, you might import:
// "github.com/fwhezfwhez/errorx"
// you can get 'errorx.Wrap(e)' above
//
// - ${db_instance} "db.DB"
// - ${handler_name} "HTTPListUser"
// - ${model} "model.User"
// - ${handle_error} "fmt.Println(e, string(debug.Stack()))"
func GenerateDeleteOneAPI()
package main
import (
"encoding/json"
"fmt"
"github.com/fwhezfwhez/model_convert"
)
func main() {
type VxTemplateUser struct {
Id int `gorm:"column:id;default:" json:"id" form:"id"`
GameId int `gorm:"column:game_id;default:" json:"game_id" form:"game_id"`
UserId int `gorm:"column:user_id;default:" json:"user_id" form:"user_id"`
OpenId string `gorm:"column:open_id;default:" json:"open_id" form:"open_id"`
TemplateId string `gorm:"column:template_id;default:" json:"template_id" form:"template_id"`
State int `gorm:"column:state;default:" json:"state" form:"state"`
}
rs := GenerateDeleteOneAPI(VxTemplateUser{}, map[string]string{
"${model}": "payModel.LingqianOrder",
"${handler_name}" : "HTTPDeleteLingqianOrder",
"${handle_error}": `common.SaveError(e)`,
})
fmt.Println(rs)
}
Output:
// Auto generate by github.com/fwhezfwhez/model_convert.GenerateDeleteOneAPI().
func HTTPDeleteLingqianOrder(c *gin.Context) {
id := c.Param("id")
idInt, e := strconv.Atoi(id)
if e!=nil {
c.JSON(400, gin.H{"message": fmt.Sprintf("param 'id' requires int but got %s", id)})
return
}
var count int
if e:=db.DB.Model(&payModel.LingqianOrder{}).Where("id=?", idInt).Count(&count).Error; e!=nil {
common.SaveError(e)
c.JSON(500, gin.H{"message": errorx.Wrap(e).Error()})
return
}
if count ==0 {
c.JSON(200, gin.H{"message": fmt.Sprintf("id '%s' record not found", id)})
return
}
var instance payModel.LingqianOrder
if e:=db.DB.Model(&payModel.LingqianOrder{}).Where("id=?", id).Delete(&instance).Error; e!=nil {
common.SaveError(e)
c.JSON(500, gin.H{"message": errorx.Wrap(e).Error()})
return
}
c.JSON(200, gin.H{"message": "success"})
}
update-one
Note:
// Generate update one api code.
// To completely use these code, you might import:
// "github.com/fwhezfwhez/errorx"
// you can get 'errorx.Wrap(e)' above
//
// | field optional | default value | example value
// - ${args_forbid_update} | "" | "user_id, game_id"
// - ${db_instance} "db.DB" | "db.DB"
// - ${handler_name} "HTTPListUser" | HTTPUpdateUser |
// - ${model} "model.User" | "payModel.Order"
// - ${handle_error} "fmt.Println(e, string(debug.Stack()))" | raven.Throw(e)
func GenerateUpdateOneAPI()
package main
import (
"encoding/json"
"fmt"
"github.com/fwhezfwhez/model_convert"
)
func main() {
type VxTemplateUser struct {
Id int `gorm:"column:id;default:" json:"id" form:"id"`
GameId int `gorm:"column:game_id;default:" json:"game_id" form:"game_id"`
UserId int `gorm:"column:user_id;default:" json:"user_id" form:"user_id"`
OpenId string `gorm:"column:open_id;default:" json:"open_id" form:"open_id"`
TemplateId string `gorm:"column:template_id;default:" json:"template_id" form:"template_id"`
State int `gorm:"column:state;default:" json:"state" form:"state"`
}
rs := GenerateUpdateOneAPI(VxTemplateUser{}, map[string]string{
"${model}": "payModel.LingqianOrder",
"${handler_name}" : "HTTPUpdateLingqianOrder",
"${handle_error}": `common.SaveError(e)`,
"${args_forbid_update}": "UserId, game_id",
})
fmt.Println(rs)
}
Output:
// Auto generate by github.com/fwhezfwhez/model_convert.GenerateUpdateOneAPI().
func HTTPUpdateLingqianOrder(c *gin.Context) {
id := c.Param("id")
idInt, e := strconv.Atoi(id)
if e!=nil {
c.JSON(400, gin.H{"message": fmt.Sprintf("param 'id' requires int but got %s", id)})
return
}
var count int
if e:=db.DB.Model(&payModel.LingqianOrder{}).Where("id=?", idInt).Count(&count).Error; e!=nil {
common.SaveError(e)
c.JSON(500, gin.H{"message": errorx.Wrap(e).Error()})
return
}
if count ==0 {
c.JSON(200, gin.H{"message": fmt.Sprintf("id '%s' record not found", id)})
return
}
var param payModel.LingqianOrder
if e:=c.Bind(¶m);e!=nil {
c.JSON(400, gin.H{"message": errorx.Wrap(e).Error()})
return
}
if !util.IfZero(param.UserId) {
c.JSON(400, gin.H{"message": "field 'UserId' can't be modified'"})
return
}
if !util.IfZero(param.GameId) {
c.JSON(400, gin.H{"message": "field 'GameId' can't be modified'"})
return
}
if e:=db.DB.Model(&payModel.LingqianOrder{}).Where("id=?", id).Updates(param).Error; e!=nil {
common.SaveError(e)
c.JSON(500, gin.H{"message": errorx.Wrap(e).Error()})
return
}
c.JSON(200, gin.H{"message": "success"})
}