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 @@
Entry | +Interaction | +Created At | +
---|---|---|
+ {{ .Interactions.EntryID }} + | ++ {{ .Interactions.Content }} + | ++ {{ .Interactions.CreatedAt }} + | +