feat: delete pages of projects

This commit is contained in:
Guz
2025-03-25 14:58:46 -03:00
parent bd5132354f
commit 7e78726bcb
3 changed files with 55 additions and 2 deletions

View File

@@ -37,12 +37,16 @@ func (router *router) pages(w http.ResponseWriter, r *http.Request) {
case http.MethodPost:
router.addPage(w, r)
case http.MethodDelete:
router.deletePage(w, r)
default:
exception.
MethodNotAllowed([]string{
http.MethodGet,
http.MethodHead,
http.MethodPost,
http.MethodDelete,
}).
ServeHTTP(w, r)
}
@@ -105,3 +109,22 @@ func (router *router) getPage(w http.ResponseWriter, r *http.Request) {
}
}
func (router *router) deletePage(w http.ResponseWriter, r *http.Request) {
router.assert.NotNil(w)
router.assert.NotNil(r)
router.assert.NotNil(router.service)
id := r.PathValue("ID")
router.assert.NotZero(id, "This method should be used after the path values are checked")
imgID := r.PathValue("PageID")
router.assert.NotZero(imgID, "This method should be used after the path values are checked")
err := router.service.DeletePage(id, imgID)
if err != nil {
exception.InternalServerError(err).ServeHTTP(w, r)
return
}
http.Redirect(w, r, fmt.Sprintf("/projects/%s/", id), http.StatusSeeOther)
}

View File

@@ -80,3 +80,29 @@ func (s *Service) GetPage(projectID string, imgID string) (io.Reader, error) {
return res.Body, nil
}
func (s *Service) DeletePage(projectID string, id string) error {
s.assert.NotNil(s.ctx)
s.assert.NotNil(s.s3)
s.assert.NotNil(s.bucket)
s.assert.NotZero(projectID)
s.assert.NotNil(id)
p, err := s.GetProject(projectID)
if err != nil {
return errors.Join(errors.New("unable to get project"), err)
}
k := fmt.Sprintf("%s/%s", projectID, id)
_, err = s.s3.DeleteObject(s.ctx, &s3.DeleteObjectInput{
Key: &k,
Bucket: &s.bucket,
})
if err != nil {
return err
}
delete(p.Pages, id)
err = s.UpdateProject(projectID, p)
return err
}

View File

@@ -9,9 +9,13 @@
<div class="flex flex-col gap-10 h-fit">
{{range $pageID, $page := .Pages}}
<section id="{{$pageID}}" class="bg-blue-500 w-100">
{{if $.ID}}
<img src="/projects/{{$.ID}}/pages/{{$pageID}}/">
{{end}}
<form action="/projects/{{$.ID}}/pages/{{$pageID}}/" method="post">
<input type="hidden" name="x-method" value="delete">
<button class="rounded-full bg-red-700 p-1 px-3 text-sm text-slate-100">
Delete
</button>
</form>
</section>
{{end}}
<form action="/projects/{{.ID}}/pages/" method="post" enctype="multipart/form-data">