WIP interaction list

This commit is contained in:
Niko Abeler 2024-02-24 19:53:49 +01:00
parent 765698b1a6
commit 0bf7c492c9
7 changed files with 116 additions and 2 deletions

View File

@ -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()
}

View File

@ -48,4 +48,6 @@ type InteractionRepository interface {
Delete(interaction model.Interaction) error Delete(interaction model.Interaction) error
FindById(id string) (model.Interaction, error) FindById(id string) (model.Interaction, error)
FindAll(entryId string) ([]model.Interaction, error) FindAll(entryId string) ([]model.Interaction, error)
// ListAllInteractions lists all interactions, sorted by creation date (descending)
ListAllInteractions() ([]model.Interaction, error)
} }

View File

@ -89,7 +89,7 @@ func (repo *DefaultInteractionRepo) Delete(interaction model.Interaction) error
// FindAll implements repository.InteractionRepository. // FindAll implements repository.InteractionRepository.
func (repo *DefaultInteractionRepo) FindAll(entryId string) ([]model.Interaction, error) { func (repo *DefaultInteractionRepo) FindAll(entryId string) ([]model.Interaction, error) {
data := []sqlInteraction{} 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 { if err != nil {
return nil, err return nil, err
} }
@ -156,3 +156,23 @@ func (repo *DefaultInteractionRepo) sqlInteractionToInteraction(interaction sqlI
return i, nil 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
}

View File

@ -13,8 +13,9 @@
<h2 style="margin-bottom: 1rem;">Content</h2> <h2 style="margin-bottom: 1rem;">Content</h2>
<div class="action-tile-list"> <div class="action-tile-list">
<a class="action-tile" href="/admin/binaries/">Files</a>
<a class="action-tile" href="/admin/drafts/">Drafts</a> <a class="action-tile" href="/admin/drafts/">Drafts</a>
<a class="action-tile" href="/admin/binaries/">Files</a>
<a class="action-tile" href="/admin/interactions/">Interactions</a>
</div> </div>
<br> <br>
<br> <br>

View File

@ -0,0 +1,51 @@
{{define "title"}}Files{{end}}
{{define "main"}}
<a href="/admin">&larr; Back</a>
<br>
<br>
<h2>Recent Interactions</h2>
<table role="grid">
<thead>
<tr>
<th scope="col">Entry</th>
<th scope="col">Interaction</th>
<th scope="col">Created At</th>
</tr>
</thead>
{{ range .Interactions }}
<tr>
<td scope="row">
{{ .Interactions.EntryID }}
</td>
<td>
{{ .Interactions.Content }}
</td>
<td>
{{ .Interactions.CreatedAt }}
</td>
</tr>
{{ end }}
</table>
<hr>
<nav class="row">
{{ if not .FirstPage }}
<div>
<a href="?page={{ .PrevPage }}">Prev</a>
</div>
{{ end }}
<div>Page {{.Page}}</div>
{{ if not .LastPage }}
<div>
<a href="?page={{ .NextPage }}">Next</a>
</div>
{{ end }}
</nav>
{{end}}

View File

@ -2,6 +2,7 @@ package web
import ( import (
"owl-blogs/app/repository" "owl-blogs/app/repository"
"owl-blogs/render"
"github.com/gofiber/fiber/v2" "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 { func (h *AdminInteractionHandler) HandleDelete(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextHTML) c.Set(fiber.HeaderContentType, fiber.MIMETextHTML)

View File

@ -68,6 +68,7 @@ func NewWebApp(
admin.Post("/binaries/new/", binaryManageHandler.HandleUpload) admin.Post("/binaries/new/", binaryManageHandler.HandleUpload)
admin.Post("/binaries/delete", binaryManageHandler.HandleDelete) admin.Post("/binaries/delete", binaryManageHandler.HandleDelete)
admin.Post("/interactions/:id/delete/", adminInteractionHandler.HandleDelete) admin.Post("/interactions/:id/delete/", adminInteractionHandler.HandleDelete)
admin.Get("/interactions/", adminInteractionHandler.HandleGet)
adminApi := admin.Group("/api") adminApi := admin.Group("/api")
adminApi.Post("/binaries", binaryManageHandler.HandleUploadApi) adminApi.Post("/binaries", binaryManageHandler.HandleUploadApi)