Implement CRUD operations for blog posts with Create, Edit, and Delete handlers
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 52s
All checks were successful
CI - Build and Push / Build and Push Docker Image (push) Successful in 52s
This commit is contained in:
29
app/internal/handler/createposthandler.go
Normal file
29
app/internal/handler/createposthandler.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/logic"
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
// Create a new blog post
|
||||
func CreatePostHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.CreatePostReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewCreatePostLogic(r.Context(), svcCtx)
|
||||
resp, err := l.CreatePost(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
29
app/internal/handler/deleteposthandler.go
Normal file
29
app/internal/handler/deleteposthandler.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/logic"
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
// Delete a blog post
|
||||
func DeletePostHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.DeletePostReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewDeletePostLogic(r.Context(), svcCtx)
|
||||
resp, err := l.DeletePost(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
29
app/internal/handler/editposthandler.go
Normal file
29
app/internal/handler/editposthandler.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/logic"
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/svc"
|
||||
"git.cialloo.com/CiallooWeb/Blog/app/internal/types"
|
||||
"github.com/zeromicro/go-zero/rest/httpx"
|
||||
)
|
||||
|
||||
// Edit an existing blog post
|
||||
func EditPostHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
var req types.EditPostReq
|
||||
if err := httpx.Parse(r, &req); err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
return
|
||||
}
|
||||
|
||||
l := logic.NewEditPostLogic(r.Context(), svcCtx)
|
||||
resp, err := l.EditPost(&req)
|
||||
if err != nil {
|
||||
httpx.ErrorCtx(r.Context(), w, err)
|
||||
} else {
|
||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,33 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
||||
rest.WithPrefix("/api/blog"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.SuperAdminAuthMiddleware},
|
||||
[]rest.Route{
|
||||
{
|
||||
// Create a new blog post
|
||||
Method: http.MethodPost,
|
||||
Path: "/create",
|
||||
Handler: CreatePostHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// Delete a blog post
|
||||
Method: http.MethodPost,
|
||||
Path: "/delete",
|
||||
Handler: DeletePostHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
// Edit an existing blog post
|
||||
Method: http.MethodPost,
|
||||
Path: "/edit",
|
||||
Handler: EditPostHandler(serverCtx),
|
||||
},
|
||||
}...,
|
||||
),
|
||||
rest.WithPrefix("/api/blog/post"),
|
||||
)
|
||||
|
||||
server.AddRoutes(
|
||||
rest.WithMiddlewares(
|
||||
[]rest.Middleware{serverCtx.SuperAdminAuthMiddleware},
|
||||
|
||||
Reference in New Issue
Block a user