feat: post deletion

This commit is contained in:
Florian Sylvain
2024-05-19 17:12:48 +02:00
parent c1317e9f21
commit 7d2795a9e0
8 changed files with 107 additions and 4 deletions
@@ -86,8 +86,7 @@ func (a *PostRepository) UpdateBody(id uint32, body string) error {
}
func (a *PostRepository) Delete(id uint32) error {
//TODO implement me
panic("implement me")
return a.db.Delete(&entity.Post{}, id).Error
}
var _ gateways.IPostRepository = &PostRepository{}
@@ -44,16 +44,38 @@
<use xlink:href="/static/bootstrap-icons.svg#pen"/>
</svg>
</a>
<a href="/post/{{ $post.Title }}/delete" class="btn btn-outline-danger btn-sm">
<svg width="12px" height="12px" fill="currentColor">
<a href="/post/{{ $post.Title }}/delete" class="btn btn-outline-danger btn-sm" data-bs-toggle="modal"
data-bs-target="#{{ $post.ID }}">
<svg width=" 12px" height="12px" fill="currentColor">
<use xlink:href="/static/bootstrap-icons.svg#trash"/>
</svg>
</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>
</tr>
{{ end }}
</tbody>
</table>
</div>
<script>
+2
View File
@@ -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),
+1
View File
@@ -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)
+24
View File
@@ -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
View File
@@ -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
}
+19
View File
@@ -120,6 +120,20 @@ var TestGetAllPostsSuccess = func(t *testing.T) {
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) {
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)
@@ -134,6 +148,10 @@ var TestPostGetAll = func(t *testing.T) {
t.Run("Should return all posts", TestGetAllPostsSuccess)
}
var TestPostDelete = func(t *testing.T) {
t.Run("Should return success", TestDeletePostSuccess)
}
func TestPost(t *testing.T) {
StartServerIfNotAlready()
WaitForServer()
@@ -141,4 +159,5 @@ func TestPost(t *testing.T) {
t.Run("Create", TestPostCreate)
t.Run("Get", TestPostGet)
t.Run("GetAll", TestPostGetAll)
t.Run("Delete", TestPostDelete)
}
+20
View File
@@ -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)
}