From 0bf7c492c9b8ce0ee3cfbf44023f7d6444bdb04d Mon Sep 17 00:00:00 2001 From: Niko Abeler Date: Sat, 24 Feb 2024 19:53:49 +0100 Subject: [PATCH] WIP interaction list --- app/interaction_service.go | 14 +++++ app/repository/interfaces.go | 2 + infra/interaction_repository.go | 22 +++++++- render/templates/views/admin.tmpl | 3 +- .../templates/views/interaction_manager.tmpl | 51 +++++++++++++++++++ web/admin_interaction_handler.go | 25 +++++++++ web/app.go | 1 + 7 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 app/interaction_service.go create mode 100644 render/templates/views/interaction_manager.tmpl diff --git a/app/interaction_service.go b/app/interaction_service.go new file mode 100644 index 0000000..c5f3add --- /dev/null +++ b/app/interaction_service.go @@ -0,0 +1,14 @@ +package app + +import ( + "owl-blogs/app/repository" + "owl-blogs/domain/model" +) + +type InteractionService struct { + repo repository.InteractionRepository +} + +func (s *InteractionService) ListInteractions() ([]model.Interaction, error) { + return s.repo.ListAllInteractions() +} diff --git a/app/repository/interfaces.go b/app/repository/interfaces.go index 7206614..b0aeed2 100644 --- a/app/repository/interfaces.go +++ b/app/repository/interfaces.go @@ -48,4 +48,6 @@ type InteractionRepository interface { Delete(interaction model.Interaction) error FindById(id string) (model.Interaction, error) FindAll(entryId string) ([]model.Interaction, error) + // ListAllInteractions lists all interactions, sorted by creation date (descending) + ListAllInteractions() ([]model.Interaction, error) } diff --git a/infra/interaction_repository.go b/infra/interaction_repository.go index 7ef340b..28b6075 100644 --- a/infra/interaction_repository.go +++ b/infra/interaction_repository.go @@ -89,7 +89,7 @@ func (repo *DefaultInteractionRepo) Delete(interaction model.Interaction) error // FindAll implements repository.InteractionRepository. func (repo *DefaultInteractionRepo) FindAll(entryId string) ([]model.Interaction, error) { data := []sqlInteraction{} - err := repo.db.Select(&data, "SELECT * FROM interactions WHERE entry_id = ?", entryId) + err := repo.db.Select(&data, "SELECT * FROM interactions WHERE entry_id = ? ORDER BY created_at DESC", entryId) if err != nil { return nil, err } @@ -156,3 +156,23 @@ func (repo *DefaultInteractionRepo) sqlInteractionToInteraction(interaction sqlI return i, nil } + +// ListAllInteractions implements repository.InteractionRepository. +func (repo *DefaultInteractionRepo) ListAllInteractions() ([]model.Interaction, error) { + data := []sqlInteraction{} + err := repo.db.Select(&data, "SELECT * FROM interactions ORDER BY created_at DESC") + if err != nil { + return nil, err + } + + interactions := []model.Interaction{} + for _, d := range data { + i, err := repo.sqlInteractionToInteraction(d) + if err != nil { + return nil, err + } + interactions = append(interactions, i) + } + + return interactions, nil +} diff --git a/render/templates/views/admin.tmpl b/render/templates/views/admin.tmpl index 512d0c1..b28deae 100644 --- a/render/templates/views/admin.tmpl +++ b/render/templates/views/admin.tmpl @@ -13,8 +13,9 @@

Content

- Files Drafts + Files + Interactions


diff --git a/render/templates/views/interaction_manager.tmpl b/render/templates/views/interaction_manager.tmpl new file mode 100644 index 0000000..2fffda7 --- /dev/null +++ b/render/templates/views/interaction_manager.tmpl @@ -0,0 +1,51 @@ +{{define "title"}}Files{{end}} + +{{define "main"}} + +← Back +
+
+ +

Recent Interactions

+ + + + + + + + + + {{ range .Interactions }} + + + + + + {{ end }} +
EntryInteractionCreated At
+ {{ .Interactions.EntryID }} + + {{ .Interactions.Content }} + + {{ .Interactions.CreatedAt }} +
+ +
+ + +{{end}} \ No newline at end of file diff --git a/web/admin_interaction_handler.go b/web/admin_interaction_handler.go index 0c071bf..c16a4ca 100644 --- a/web/admin_interaction_handler.go +++ b/web/admin_interaction_handler.go @@ -2,6 +2,7 @@ package web import ( "owl-blogs/app/repository" + "owl-blogs/render" "github.com/gofiber/fiber/v2" ) @@ -18,6 +19,30 @@ func NewAdminInteractionHandler(configRepo repository.ConfigRepository, interact } } +func (h *AdminInteractionHandler) HandleGet(c *fiber.Ctx) error { + siteConfig := getSiteConfig(h.configRepo) + + filter := c.Query("filter", "") + + interactions, err := h.interactionRepo.ListAllInteractions() + if err != nil { + return err + } + pageData := paginate(c, interactions, 50) + + c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) + return render.RenderTemplateWithBase(c, siteConfig, "views/interaction_manager", fiber.Map{ + "Binaries": pageData.items, + "Page": pageData.page, + "NextPage": pageData.page + 1, + "PrevPage": pageData.page - 1, + "FirstPage": pageData.page == 1, + "LastPage": pageData.lastPage, + "Filter": filter, + }) + +} + func (h *AdminInteractionHandler) HandleDelete(c *fiber.Ctx) error { c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) diff --git a/web/app.go b/web/app.go index 82e1da4..a604cd4 100644 --- a/web/app.go +++ b/web/app.go @@ -68,6 +68,7 @@ func NewWebApp( admin.Post("/binaries/new/", binaryManageHandler.HandleUpload) admin.Post("/binaries/delete", binaryManageHandler.HandleDelete) admin.Post("/interactions/:id/delete/", adminInteractionHandler.HandleDelete) + admin.Get("/interactions/", adminInteractionHandler.HandleGet) adminApi := admin.Group("/api") adminApi.Post("/binaries", binaryManageHandler.HandleUploadApi)