mirror of
https://github.com/Floriansylvain/RenewCMS.git
synced 2026-08-19 11:43:22 +02:00
Merge pull request #34 from Floriansylvain/feature/postDeletion
feat: post deletion
This commit is contained in:
@@ -86,8 +86,7 @@ func (a *PostRepository) UpdateBody(id uint32, body string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *PostRepository) Delete(id uint32) error {
|
func (a *PostRepository) Delete(id uint32) error {
|
||||||
//TODO implement me
|
return a.db.Delete(&entity.Post{}, id).Error
|
||||||
panic("implement me")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ gateways.IPostRepository = &PostRepository{}
|
var _ gateways.IPostRepository = &PostRepository{}
|
||||||
|
|||||||
@@ -44,16 +44,38 @@
|
|||||||
<use xlink:href="/static/bootstrap-icons.svg#pen"/>
|
<use xlink:href="/static/bootstrap-icons.svg#pen"/>
|
||||||
</svg>
|
</svg>
|
||||||
</a>
|
</a>
|
||||||
<a href="/post/{{ $post.Title }}/delete" class="btn btn-outline-danger btn-sm">
|
<a href="/post/{{ $post.Title }}/delete" class="btn btn-outline-danger btn-sm" data-bs-toggle="modal"
|
||||||
<svg width="12px" height="12px" fill="currentColor">
|
data-bs-target="#{{ $post.ID }}">
|
||||||
|
<svg width=" 12px" height="12px" fill="currentColor">
|
||||||
<use xlink:href="/static/bootstrap-icons.svg#trash"/>
|
<use xlink:href="/static/bootstrap-icons.svg#trash"/>
|
||||||
</svg>
|
</svg>
|
||||||
</a>
|
</a>
|
||||||
|
<div class="modal fade" id="{{ $post.ID }}" tabindex="-1" aria-labelledby="{{ $post.ID }}Label"
|
||||||
|
aria-hidden="true">
|
||||||
|
<div class="modal-dialog">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h1 class="modal-title fs-5" id="{{ $post.ID }}Label">Delete post</h1>
|
||||||
|
<button type="button" class="btn-close" data-bs-dismiss="modal"
|
||||||
|
aria-label="Close"></button>
|
||||||
|
</div>
|
||||||
|
<div class="modal-body">
|
||||||
|
<p>You are about to delete the post <b>« {{ $post.Title }} »</b>, this is definitive, are you sure?</p>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer">
|
||||||
|
<button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel
|
||||||
|
</button>
|
||||||
|
<a href="/post/{{$post.ID}}/delete" class="btn btn-danger">Delete</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ type UseCases struct {
|
|||||||
GetPostUseCase *useCases.GetPostUseCase
|
GetPostUseCase *useCases.GetPostUseCase
|
||||||
ListPostsUseCase *useCases.ListPostsUseCase
|
ListPostsUseCase *useCases.ListPostsUseCase
|
||||||
UpdatePostUseCase *useCases.UpdatePostUseCase
|
UpdatePostUseCase *useCases.UpdatePostUseCase
|
||||||
|
DeletePostUseCase *useCases.DeletePostUseCase
|
||||||
GetUserUseCase *useCases.GetUserUseCase
|
GetUserUseCase *useCases.GetUserUseCase
|
||||||
CreateUserUseCase *useCases.CreateUserUseCase
|
CreateUserUseCase *useCases.CreateUserUseCase
|
||||||
UpdateUserUseCase *useCases.UpdateUserUseCase
|
UpdateUserUseCase *useCases.UpdateUserUseCase
|
||||||
@@ -51,6 +52,7 @@ func InitContainer() {
|
|||||||
GetPostUseCase: useCases.NewGetPostUseCase(db),
|
GetPostUseCase: useCases.NewGetPostUseCase(db),
|
||||||
ListPostsUseCase: useCases.NewListPostsUseCase(db),
|
ListPostsUseCase: useCases.NewListPostsUseCase(db),
|
||||||
UpdatePostUseCase: useCases.NewUpdatePostUseCase(db),
|
UpdatePostUseCase: useCases.NewUpdatePostUseCase(db),
|
||||||
|
DeletePostUseCase: useCases.NewDeletePostUseCase(db),
|
||||||
GetUserUseCase: useCases.NewGetUserUseCase(db),
|
GetUserUseCase: useCases.NewGetUserUseCase(db),
|
||||||
CreateUserUseCase: useCases.NewCreateUserUseCase(db),
|
CreateUserUseCase: useCases.NewCreateUserUseCase(db),
|
||||||
UpdateUserUseCase: useCases.NewUpdateUserUseCase(db),
|
UpdateUserUseCase: useCases.NewUpdateUserUseCase(db),
|
||||||
|
|||||||
@@ -123,6 +123,7 @@ func NewPageRouter() http.Handler {
|
|||||||
r.Get("/home", GetHomePage)
|
r.Get("/home", GetHomePage)
|
||||||
r.Get("/post", GetPostsPage)
|
r.Get("/post", GetPostsPage)
|
||||||
r.Get("/post/{name}/edit", GetPostEditPage)
|
r.Get("/post/{name}/edit", GetPostEditPage)
|
||||||
|
r.Get("/post/{id}/delete", GetPostDeletePage)
|
||||||
r.Post("/post/{name}/edit", PostPostEditPage)
|
r.Post("/post/{name}/edit", PostPostEditPage)
|
||||||
r.Get("/post/create", GetPostCreatePage)
|
r.Get("/post/create", GetPostCreatePage)
|
||||||
r.Post("/post/create", PostPostCreatePage)
|
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)
|
_, _ = 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 {
|
func NewPostRouter() http.Handler {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Get("/{id}", getPost)
|
r.Get("/{id}", getPost)
|
||||||
r.Post("/", postPost)
|
r.Post("/", postPost)
|
||||||
r.Get("/", listPosts)
|
r.Get("/", listPosts)
|
||||||
|
r.Delete("/{id}", deletePost)
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -120,6 +120,20 @@ var TestGetAllPostsSuccess = func(t *testing.T) {
|
|||||||
assert.Equal(t, createdPost.Body, response[0].Body)
|
assert.Equal(t, createdPost.Body, response[0].Body)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var TestDeletePostSuccess = func(t *testing.T) {
|
||||||
|
var createdPost post.Post
|
||||||
|
var postToCreate = post.Post{
|
||||||
|
Title: "Test Title",
|
||||||
|
Body: "Test Body",
|
||||||
|
}
|
||||||
|
db := GetDb()
|
||||||
|
db.Create(&postToCreate).Scan(&createdPost)
|
||||||
|
|
||||||
|
r, _ := ApiRequest("DELETE", "/post/"+strconv.Itoa(int(createdPost.ID)), nil)
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, r.StatusCode)
|
||||||
|
}
|
||||||
|
|
||||||
var TestPostCreate = func(t *testing.T) {
|
var TestPostCreate = func(t *testing.T) {
|
||||||
t.Run("Should return a post with the given title and body", TestCreatePostSuccess)
|
t.Run("Should return a post with the given title and body", TestCreatePostSuccess)
|
||||||
t.Run("Should return an error if the title is missing", TestCreatePostFailTitleMissing)
|
t.Run("Should return an error if the title is missing", TestCreatePostFailTitleMissing)
|
||||||
@@ -134,6 +148,10 @@ var TestPostGetAll = func(t *testing.T) {
|
|||||||
t.Run("Should return all posts", TestGetAllPostsSuccess)
|
t.Run("Should return all posts", TestGetAllPostsSuccess)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var TestPostDelete = func(t *testing.T) {
|
||||||
|
t.Run("Should return success", TestDeletePostSuccess)
|
||||||
|
}
|
||||||
|
|
||||||
func TestPost(t *testing.T) {
|
func TestPost(t *testing.T) {
|
||||||
StartServerIfNotAlready()
|
StartServerIfNotAlready()
|
||||||
WaitForServer()
|
WaitForServer()
|
||||||
@@ -141,4 +159,5 @@ func TestPost(t *testing.T) {
|
|||||||
t.Run("Create", TestPostCreate)
|
t.Run("Create", TestPostCreate)
|
||||||
t.Run("Get", TestPostGet)
|
t.Run("Get", TestPostGet)
|
||||||
t.Run("GetAll", TestPostGetAll)
|
t.Run("GetAll", TestPostGetAll)
|
||||||
|
t.Run("Delete", TestPostDelete)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package useCases
|
||||||
|
|
||||||
|
import (
|
||||||
|
"GoCMS/adapters/secondary/gateways"
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DeletePostUseCase struct {
|
||||||
|
postRepository gateways.PostRepository
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDeletePostUseCase(db *gorm.DB) *DeletePostUseCase {
|
||||||
|
return &DeletePostUseCase{
|
||||||
|
postRepository: *gateways.NewPostRepository(db),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *DeletePostUseCase) DeletePost(userId uint32) error {
|
||||||
|
return g.postRepository.Delete(userId)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user