|
|
@@ -0,0 +1,100 @@
|
|
|
+package v1
|
|
|
+
|
|
|
+import (
|
|
|
+ "github.com/runningwater/gohub/app/models/{{PackageName}}"
|
|
|
+ "github.com/runningwater/gohub/app/policies"
|
|
|
+ "github.com/runningwater/gohub/app/requests"
|
|
|
+ "github.com/runningwater/gohub/pkg/response"
|
|
|
+
|
|
|
+ "github.com/gin-gonic/gin"
|
|
|
+)
|
|
|
+
|
|
|
+type {{StructNamePlural}}Controller struct {
|
|
|
+ BaseApiController
|
|
|
+}
|
|
|
+
|
|
|
+func (ctrl *{{StructNamePlural}}Controller) Index(c *gin.Context) {
|
|
|
+ {{VariableNamePlural}} := {{PackageName}}.All()
|
|
|
+ response.Data(c, {{VariableNamePlural}})
|
|
|
+}
|
|
|
+
|
|
|
+func (ctrl *{{StructNamePlural}}Controller) Show(c *gin.Context) {
|
|
|
+ {{VariableName}}Model := {{PackageName}}.Get(c.Param("id"))
|
|
|
+ if {{VariableName}}Model.ID == 0 {
|
|
|
+ response.Abort404(c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ response.Data(c, {{VariableName}}Model)
|
|
|
+}
|
|
|
+
|
|
|
+func (ctrl *{{StructNamePlural}}Controller) Store(c *gin.Context) {
|
|
|
+
|
|
|
+ request := requests.{{StructName}}Request{}
|
|
|
+ if ok := requests.Validate(c, &request, requests.{{StructName}}Save); !ok {
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ {{VariableName}}Model := {{PackageName}}.{{StructName}}{
|
|
|
+ FieldName: request.FieldName,
|
|
|
+ }
|
|
|
+ {{VariableName}}Model.Create()
|
|
|
+ if {{VariableName}}Model.ID > 0 {
|
|
|
+ response.Created(c, {{VariableName}}Model)
|
|
|
+ } else {
|
|
|
+ response.Abort500(c, "创建失败,请稍后尝试~")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (ctrl *{{StructNamePlural}}Controller) Update(c *gin.Context) {
|
|
|
+
|
|
|
+ {{VariableName}}Model := {{PackageName}}.Get(c.Param("id"))
|
|
|
+ if {{VariableName}}Model.ID == 0 {
|
|
|
+ response.Abort404(c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if ok := policies.CanModify{{StructName}}(c, {{VariableName}}Model); !ok {
|
|
|
+ response.Abort403(c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ request := requests.{{StructName}}Request{}
|
|
|
+ bindOk, errs := requests.Validate(c, &request, requests.{{StructName}}Save)
|
|
|
+ if !bindOk {
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if len(errs) > 0 {
|
|
|
+ response.ValidationError(c, errs)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ {{VariableName}}Model.FieldName = request.FieldName
|
|
|
+ rowsAffected := {{VariableName}}Model.Save()
|
|
|
+ if rowsAffected > 0 {
|
|
|
+ response.Data(c, {{VariableName}}Model)
|
|
|
+ } else {
|
|
|
+ response.Abort500(c, "更新失败,请稍后尝试~")
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (ctrl *{{StructNamePlural}}Controller) Delete(c *gin.Context) {
|
|
|
+
|
|
|
+ {{VariableName}}Model := {{PackageName}}.Get(c.Param("id"))
|
|
|
+ if {{VariableName}}Model.ID == 0 {
|
|
|
+ response.Abort404(c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if ok := policies.CanModify{{StructName}}(c, {{VariableName}}Model); !ok {
|
|
|
+ response.Abort403(c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ rowsAffected := {{VariableName}}Model.Delete()
|
|
|
+ if rowsAffected > 0 {
|
|
|
+ response.Success(c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ response.Abort500(c, "删除失败,请稍后尝试~")
|
|
|
+}
|