109 lines
2.1 KiB
Go
109 lines
2.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"server/internal/model/common"
|
|
"server/internal/model/request"
|
|
"server/internal/pkg/httputil"
|
|
"server/internal/service"
|
|
)
|
|
|
|
type SysPostHandler struct {
|
|
sysPostService *service.SysPostService
|
|
}
|
|
|
|
func NewSysPostHandler(postService *service.SysPostService) *SysPostHandler {
|
|
return &SysPostHandler{sysPostService: postService}
|
|
}
|
|
|
|
func (h *SysPostHandler) ListPage(w http.ResponseWriter, r *http.Request) {
|
|
pagination := httputil.Pagination(r)
|
|
|
|
list, total, err := h.sysPostService.ListPage(r.Context(), pagination)
|
|
if err != nil {
|
|
httputil.Fail(w, err)
|
|
}
|
|
|
|
resp := common.PageResponse{
|
|
Page: pagination.Page,
|
|
PageSize: pagination.PageSize,
|
|
List: list,
|
|
Total: total,
|
|
}
|
|
|
|
httputil.OkWithPage(w, &resp)
|
|
}
|
|
|
|
func (h *SysPostHandler) GetPostById(w http.ResponseWriter, r *http.Request) {
|
|
id, err := httputil.URLParamInt32(r, "id")
|
|
if err != nil {
|
|
httputil.Fail(w, err)
|
|
return
|
|
}
|
|
|
|
post, err := h.sysPostService.FindByID(r.Context(), id)
|
|
if err != nil {
|
|
httputil.Fail(w, err)
|
|
return
|
|
}
|
|
|
|
httputil.Ok(w, post)
|
|
}
|
|
|
|
func (h *SysPostHandler) Create(w http.ResponseWriter, r *http.Request) {
|
|
var req request.CreatePostRequest
|
|
|
|
if err := httputil.BindJson(r, &req); err != nil {
|
|
httputil.Fail(w, err)
|
|
return
|
|
}
|
|
|
|
postID, err := h.sysPostService.Create(r.Context(), req)
|
|
|
|
if err != nil {
|
|
httputil.Fail(w, err)
|
|
return
|
|
}
|
|
|
|
httputil.Ok(w, map[string]int32{
|
|
"post_id": postID,
|
|
})
|
|
}
|
|
|
|
func (h *SysPostHandler) Update(w http.ResponseWriter, r *http.Request) {
|
|
id, err := httputil.URLParamInt32(r, "id")
|
|
if err != nil {
|
|
httputil.Fail(w, err)
|
|
return
|
|
}
|
|
|
|
var req request.UpdatePostRequest
|
|
|
|
if err = httputil.BindJson(r, &req); err != nil {
|
|
httputil.Fail(w, err)
|
|
return
|
|
}
|
|
|
|
if err = h.sysPostService.Update(r.Context(), id, req); err != nil {
|
|
httputil.Fail(w, err)
|
|
return
|
|
}
|
|
|
|
httputil.Ok(w)
|
|
}
|
|
|
|
func (h *SysPostHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
|
id, err := httputil.URLParamInt32(r, "id")
|
|
if err != nil {
|
|
httputil.Fail(w, err)
|
|
return
|
|
}
|
|
|
|
if err = h.sysPostService.Delete(r.Context(), id); err != nil {
|
|
httputil.Fail(w, err)
|
|
return
|
|
}
|
|
|
|
httputil.Ok(w)
|
|
}
|