diff --git a/router/editor.go b/router/editor.go index 8b7c8f7..2dab46a 100644 --- a/router/editor.go +++ b/router/editor.go @@ -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) +} diff --git a/service/editor.go b/service/editor.go index fc67511..6c0c331 100644 --- a/service/editor.go +++ b/service/editor.go @@ -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 +} diff --git a/templates/project.html b/templates/project.html index a6b7b01..6c635f6 100644 --- a/templates/project.html +++ b/templates/project.html @@ -9,9 +9,13 @@