-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
572c950
commit 56eac52
Showing
2 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
package v1 | ||
|
||
import "github.com/gin-gonic/gin" | ||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/golang-minibear2333/gin-blog/pkg/app" | ||
"github.com/golang-minibear2333/gin-blog/pkg/errcode" | ||
) | ||
|
||
type Article struct{} | ||
|
||
func NewArticle() Article { | ||
return Article{} | ||
} | ||
|
||
func (a Article) Get(c *gin.Context) {} | ||
func (a Article) List(c *gin.Context) {} | ||
func (a Article) Get(c *gin.Context) { | ||
} | ||
func (a Article) List(c *gin.Context) { | ||
// 接口测试 http://localhost:8000/api/v1/article | ||
app.NewResponse(c).ToErrorResponse(errcode.ServerError) | ||
return | ||
} | ||
func (a Article) Create(c *gin.Context) {} | ||
func (a Article) Update(c *gin.Context) {} | ||
func (a Article) Delete(c *gin.Context) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package app | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/golang-minibear2333/gin-blog/pkg/errcode" | ||
"net/http" | ||
) | ||
|
||
type Response struct { | ||
Ctx *gin.Context | ||
} | ||
|
||
type Pager struct { | ||
Page int `json:"page"` | ||
PageSize int `json:"page_size"` | ||
TotalRows int `json:"total_rows"` | ||
} | ||
|
||
func NewResponse(ctx *gin.Context) *Response { | ||
return &Response{Ctx: ctx} | ||
} | ||
|
||
func (r *Response) ToResponse(data interface{}) { | ||
if data == nil { | ||
data = gin.H{} | ||
} | ||
r.Ctx.JSON(http.StatusOK, data) | ||
} | ||
|
||
func (r *Response) ToResponseList(list interface{}, totalRows int) { | ||
r.Ctx.JSON(http.StatusOK, gin.H{ | ||
"list": list, | ||
"pager": Pager{ | ||
Page: GetPage(r.Ctx), | ||
PageSize: GetPageSize(r.Ctx), | ||
TotalRows: totalRows, | ||
}, | ||
}) | ||
} | ||
|
||
func (r *Response) ToErrorResponse(err *errcode.Error) { | ||
response := gin.H{"code": err.Code(), "msg": err.Msg()} | ||
details := err.Details() | ||
if len(details) > 0 { | ||
response["details"] = details | ||
} | ||
|
||
r.Ctx.JSON(err.StatusCode(), response) | ||
} |