diff --git a/app/webmention_service.go b/app/webmention_service.go index 940ec77..5c712ed 100644 --- a/app/webmention_service.go +++ b/app/webmention_service.go @@ -130,10 +130,6 @@ func (s *WebmentionService) ProcessWebmention(source string, target string) erro } entryId := UrlToEntryId(target) - println(entryId) - println(entryId) - println(entryId) - println(entryId) _, err = s.EntryRepository.FindById(entryId) if err != nil { return err diff --git a/infra/entry_repository.go b/infra/entry_repository.go index fc2580c..e4fc563 100644 --- a/infra/entry_repository.go +++ b/infra/entry_repository.go @@ -70,6 +70,9 @@ func (r *DefaultEntryRepo) Create(entry model.Entry) error { // Delete implements repository.EntryRepository. func (r *DefaultEntryRepo) Delete(entry model.Entry) error { + if entry.ID() == "" { + return errors.New("entry not found") + } _, err := r.db.Exec("DELETE FROM entries WHERE id = ?", entry.ID()) return err } diff --git a/infra/interaction_repository.go b/infra/interaction_repository.go index e9e88fe..7ef340b 100644 --- a/infra/interaction_repository.go +++ b/infra/interaction_repository.go @@ -78,8 +78,12 @@ func (repo *DefaultInteractionRepo) Create(interaction model.Interaction) error } // Delete implements repository.InteractionRepository. -func (*DefaultInteractionRepo) Delete(interaction model.Interaction) error { - panic("unimplemented") +func (repo *DefaultInteractionRepo) Delete(interaction model.Interaction) error { + if interaction.ID() == "" { + return errors.New("interaction not found") + } + _, err := repo.db.Exec("DELETE FROM interactions WHERE id = ?", interaction.ID()) + return err } // FindAll implements repository.InteractionRepository. diff --git a/render/templates/views/entry.tmpl b/render/templates/views/entry.tmpl index 0d36fc8..da649be 100644 --- a/render/templates/views/entry.tmpl +++ b/render/templates/views/entry.tmpl @@ -51,6 +51,15 @@ {{range .Interactions}}
  • {{ .Content }} + {{ if $.LoggedIn }} +
    + + +
    + {{ end }}
  • {{end}} diff --git a/web/admin_interaction_handler.go b/web/admin_interaction_handler.go new file mode 100644 index 0000000..0c071bf --- /dev/null +++ b/web/admin_interaction_handler.go @@ -0,0 +1,41 @@ +package web + +import ( + "owl-blogs/app/repository" + + "github.com/gofiber/fiber/v2" +) + +type AdminInteractionHandler struct { + interactionRepo repository.InteractionRepository + configRepo repository.ConfigRepository +} + +func NewAdminInteractionHandler(configRepo repository.ConfigRepository, interactionRepo repository.InteractionRepository) *AdminInteractionHandler { + return &AdminInteractionHandler{ + interactionRepo: interactionRepo, + configRepo: configRepo, + } +} + +func (h *AdminInteractionHandler) HandleDelete(c *fiber.Ctx) error { + c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) + + id := c.Params("id") + inter, err := h.interactionRepo.FindById(id) + entryId := inter.EntryID() + if err != nil { + return err + } + + confirm := c.FormValue("confirm") + if confirm != "on" { + return c.Redirect("/posts/" + inter.ID() + "/") + } + + err = h.interactionRepo.Delete(inter) + if err != nil { + return err + } + return c.Redirect("/posts/" + entryId + "/") +} diff --git a/web/app.go b/web/app.go index e345534..ad6fcb2 100644 --- a/web/app.go +++ b/web/app.go @@ -57,6 +57,7 @@ func NewWebApp( adminHandler := NewAdminHandler(configRepo, configRegister, typeRegistry) draftHandler := NewDraftHandler(entryService, configRepo) binaryManageHandler := NewBinaryManageHandler(configRepo, binService) + adminInteractionHandler := NewAdminInteractionHandler(configRepo, interactionRepo) admin := app.Group("/admin") admin.Use(middleware.NewAuthMiddleware(authorService).Handle) admin.Get("/", adminHandler.Handle) @@ -66,6 +67,7 @@ func NewWebApp( admin.Get("/binaries/", binaryManageHandler.Handle) admin.Post("/binaries/new/", binaryManageHandler.HandleUpload) admin.Post("/binaries/delete", binaryManageHandler.HandleDelete) + admin.Post("/interactions/:id/delete/", adminInteractionHandler.HandleDelete) // Editor editor := app.Group("/editor") diff --git a/web/webmention_handler.go b/web/webmention_handler.go index c92c9a1..9473048 100644 --- a/web/webmention_handler.go +++ b/web/webmention_handler.go @@ -26,6 +26,7 @@ func (h *WebmentionHandler) Handle(c *fiber.Ctx) error { target := c.FormValue("target") source := c.FormValue("source") + println("Incoming webmention") println("target", target) println("source", source)