49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/infraboard/mcube/v2/http/gin/response"
|
|
"gitlab.com/go-course-project/go17/vblog/apps/blog"
|
|
)
|
|
|
|
type BlogApiHandler struct {
|
|
blog blog.Service
|
|
}
|
|
|
|
// BODY
|
|
func (h *BlogApiHandler) CreateBlog(ctx *gin.Context) {
|
|
in := &blog.CreateBlogRequest{}
|
|
if err := ctx.BindJSON(in); err != nil {
|
|
response.Failed(ctx, err)
|
|
return
|
|
}
|
|
ins, err := h.blog.CreateBlog(ctx.Request.Context(), in)
|
|
if err != nil {
|
|
response.Failed(ctx, err)
|
|
return
|
|
}
|
|
response.Success(ctx, ins)
|
|
}
|
|
|
|
func (h *BlogApiHandler) QueryBlog(ctx *gin.Context) {
|
|
in := blog.NewQueryBlogRequest()
|
|
// GET /search?name=John&age=30&country=USA
|
|
// type SearchQuery struct {
|
|
// Name string `form:"name"`
|
|
// Age int `form:"age"`
|
|
// Country string `form:"country"`
|
|
// }
|
|
if err := ctx.BindQuery(in); err != nil {
|
|
response.Failed(ctx, err)
|
|
return
|
|
}
|
|
in.SetTag(ctx.Query("tag"))
|
|
|
|
ins, err := h.blog.QueryBlog(ctx.Request.Context(), in)
|
|
if err != nil {
|
|
response.Failed(ctx, err)
|
|
return
|
|
}
|
|
response.Success(ctx, ins)
|
|
}
|