Compare commits

..

2 Commits

Author SHA1 Message Date
Niko Abeler 8200e3384c fix for interactions 2024-02-24 20:11:09 +01:00
Niko Abeler 0bf7c492c9 WIP interaction list 2024-02-24 19:53:49 +01:00
7 changed files with 112 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
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)
}

View File

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

View File

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

View File

@ -0,0 +1,47 @@
{{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">Created At</th>
</tr>
</thead>
{{ range .Interactions }}
<tr>
<td scope="row">
<a href="/posts/{{ .EntryID }}">{{ .EntryID }}</a>
</td>
<td>
{{ .CreatedAt.Format "2006-01-02 15:04:05" }}
</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 (
"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{
"Interactions": 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)

View File

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