feat: 统一sql命名、函数命名
This commit is contained in:
123
internal/handler/admin/post.go
Normal file
123
internal/handler/admin/post.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"server/internal/model/common"
|
||||
"server/internal/model/request"
|
||||
"server/internal/pkg/httputil"
|
||||
"server/internal/router"
|
||||
"server/internal/service/admin"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
)
|
||||
|
||||
type PostHandler struct {
|
||||
postService *admin.PostService
|
||||
}
|
||||
|
||||
var _ router.Registrar = (*PostHandler)(nil)
|
||||
|
||||
func NewPostHandler(postService *admin.PostService) *PostHandler {
|
||||
return &PostHandler{postService: postService}
|
||||
}
|
||||
|
||||
func (h *PostHandler) Register(r chi.Router) {
|
||||
r.Route("/posts", func(r chi.Router) {
|
||||
r.Get("/", h.List)
|
||||
r.Get("/{id}", h.GetPostByID)
|
||||
r.Post("/", h.Create)
|
||||
r.Patch("/{id}", h.Update)
|
||||
r.Delete("/{id}", h.Delete)
|
||||
})
|
||||
}
|
||||
|
||||
func (h *PostHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
pagination := httputil.Pagination(r)
|
||||
|
||||
result, err := h.postService.List(r.Context(), pagination)
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
}
|
||||
|
||||
resp := common.PageResponse{
|
||||
Page: pagination.Page,
|
||||
PageSize: pagination.PageSize,
|
||||
List: result.List,
|
||||
Total: result.Total,
|
||||
}
|
||||
|
||||
httputil.OkWithPage(w, &resp)
|
||||
}
|
||||
|
||||
func (h *PostHandler) GetPostByID(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := httputil.URLParamInt32(r, "id")
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
post, err := h.postService.FindByID(r.Context(), id)
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w, post)
|
||||
}
|
||||
|
||||
func (h *PostHandler) 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.postService.Create(r.Context(), req)
|
||||
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w, map[string]int32{
|
||||
"post_id": postID,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *PostHandler) 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.postService.Update(r.Context(), id, req); err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w)
|
||||
}
|
||||
|
||||
func (h *PostHandler) Delete(w http.ResponseWriter, r *http.Request) {
|
||||
id, err := httputil.URLParamInt32(r, "id")
|
||||
if err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err = h.postService.Delete(r.Context(), id); err != nil {
|
||||
httputil.Fail(w, err)
|
||||
return
|
||||
}
|
||||
|
||||
httputil.Ok(w)
|
||||
}
|
||||
Reference in New Issue
Block a user