mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
feat: post deletion
This commit is contained in:
@@ -15,6 +15,7 @@ type UseCases struct {
|
||||
GetPostUseCase *useCases.GetPostUseCase
|
||||
ListPostsUseCase *useCases.ListPostsUseCase
|
||||
UpdatePostUseCase *useCases.UpdatePostUseCase
|
||||
DeletePostUseCase *useCases.DeletePostUseCase
|
||||
GetUserUseCase *useCases.GetUserUseCase
|
||||
CreateUserUseCase *useCases.CreateUserUseCase
|
||||
UpdateUserUseCase *useCases.UpdateUserUseCase
|
||||
@@ -51,6 +52,7 @@ func InitContainer() {
|
||||
GetPostUseCase: useCases.NewGetPostUseCase(db),
|
||||
ListPostsUseCase: useCases.NewListPostsUseCase(db),
|
||||
UpdatePostUseCase: useCases.NewUpdatePostUseCase(db),
|
||||
DeletePostUseCase: useCases.NewDeletePostUseCase(db),
|
||||
GetUserUseCase: useCases.NewGetUserUseCase(db),
|
||||
CreateUserUseCase: useCases.NewCreateUserUseCase(db),
|
||||
UpdateUserUseCase: useCases.NewUpdateUserUseCase(db),
|
||||
|
||||
@@ -123,6 +123,7 @@ func NewPageRouter() http.Handler {
|
||||
r.Get("/home", GetHomePage)
|
||||
r.Get("/post", GetPostsPage)
|
||||
r.Get("/post/{name}/edit", GetPostEditPage)
|
||||
r.Get("/post/{id}/delete", GetPostDeletePage)
|
||||
r.Post("/post/{name}/edit", PostPostEditPage)
|
||||
r.Get("/post/create", GetPostCreatePage)
|
||||
r.Post("/post/create", PostPostCreatePage)
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func GetPostDeletePage(w http.ResponseWriter, r *http.Request) {
|
||||
postId := chi.URLParam(r, "id")
|
||||
id, err := strconv.Atoi(postId)
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(400), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err = Container.DeletePostUseCase.DeletePost(uint32(id))
|
||||
if err != nil {
|
||||
http.Error(w, http.StatusText(400), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
http.Redirect(w, r, "/post", http.StatusSeeOther)
|
||||
}
|
||||
+16
@@ -64,10 +64,26 @@ func listPosts(w http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = w.Write(postsJson)
|
||||
}
|
||||
|
||||
func deletePost(w http.ResponseWriter, r *http.Request) {
|
||||
idParam := chi.URLParam(r, "id")
|
||||
id, err := strconv.Atoi(idParam)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
}
|
||||
|
||||
err = Container.DeletePostUseCase.DeletePost(uint32(id))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusNotFound)
|
||||
}
|
||||
|
||||
_, _ = w.Write([]byte("post deleted"))
|
||||
}
|
||||
|
||||
func NewPostRouter() http.Handler {
|
||||
r := chi.NewRouter()
|
||||
r.Get("/{id}", getPost)
|
||||
r.Post("/", postPost)
|
||||
r.Get("/", listPosts)
|
||||
r.Delete("/{id}", deletePost)
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user