content page

This commit is contained in:
Daniel Noulet 2026-09-19 15:14:16 +02:00
parent 36b2c56987
commit 381ed6270e
8 changed files with 59 additions and 5 deletions

View file

@ -41,6 +41,11 @@ type Category struct {
Name string
}
type Badge struct {
ID uint
Name string
}
type ContentHistory struct {
ID uint
ContentID uint

View file

@ -90,6 +90,17 @@ func (r *GORMContentRepository) ListCategories(ctx context.Context) ([]domain.Ca
}
return out, nil
}
func (r *GORMContentRepository) ListBadges(ctx context.Context) ([]domain.Badge, error) {
var rows []persistence.BadgeModel
if err := r.db.WithContext(ctx).Order("name ASC").Find(&rows).Error; err != nil {
return nil, err
}
out := make([]domain.Badge, len(rows))
for i := range rows {
out[i] = domain.Badge{ID: rows[i].ID, Name: rows[i].Name}
}
return out, nil
}
func (r *GORMContentRepository) FindBySlug(ctx context.Context, slug string) (domain.Content, error) {
var row persistence.ContentModel
if err := r.db.WithContext(ctx).Preload("Category").Preload("Badges").Where("slug = ?", slug).First(&row).Error; err != nil {

View file

@ -21,6 +21,7 @@ type ISessionRepository interface {
type IContentRepository interface {
List(context.Context, int, int, string, []string) ([]domain.Content, error)
ListCategories(context.Context) ([]domain.Category, error)
ListBadges(context.Context) ([]domain.Badge, error)
FindBySlug(context.Context, string) (domain.Content, error)
Update(context.Context, domain.Content, domain.ContentHistory) error
}

View file

@ -14,6 +14,7 @@ import (
type IContentService interface {
List(context.Context, int, int, string, []string) ([]domain.Content, error)
ListCategories(context.Context) ([]domain.Category, error)
ListBadges(context.Context) ([]domain.Badge, error)
Get(context.Context, string) (domain.Content, error)
Update(context.Context, domain.Content, domain.Session) (domain.Content, error)
}
@ -40,6 +41,9 @@ func (s *ContentService) List(ctx context.Context, limit, offset int, category s
func (s *ContentService) ListCategories(ctx context.Context) ([]domain.Category, error) {
return s.repo.ListCategories(ctx)
}
func (s *ContentService) ListBadges(ctx context.Context) ([]domain.Badge, error) {
return s.repo.ListBadges(ctx)
}
func (s *ContentService) Get(ctx context.Context, slug string) (domain.Content, error) {
if !validSlug(slug) {
return domain.Content{}, errors.New("invalid slug")

View file

@ -38,6 +38,7 @@ func (h *Handler) Routes(origins string) http.Handler {
mux.HandleFunc("/api/protected", h.protected)
mux.HandleFunc("/api/content", h.contentList)
mux.HandleFunc("/api/content/categories", h.contentCategories)
mux.HandleFunc("/api/content/badges", h.contentBadges)
mux.HandleFunc("/api/content/", h.contentDetail)
return cors(mux, origins)
}
@ -209,6 +210,26 @@ func (h *Handler) contentCategories(w http.ResponseWriter, r *http.Request) {
}
jsonWrite(w, 200, map[string]any{"items": categories})
}
func (h *Handler) contentBadges(w http.ResponseWriter, r *http.Request) {
if h.content == nil {
jsonWrite(w, 503, map[string]string{"error": "content unavailable"})
return
}
if _, ok := h.sessionForRequest(r); !ok {
jsonWrite(w, 401, map[string]string{"error": "authentication required"})
return
}
if r.Method != http.MethodGet {
jsonWrite(w, http.StatusMethodNotAllowed, nil)
return
}
badges, err := h.content.ListBadges(r.Context())
if err != nil {
jsonWrite(w, 500, map[string]string{"error": err.Error()})
return
}
jsonWrite(w, 200, map[string]any{"items": badges})
}
func (h *Handler) contentDetail(w http.ResponseWriter, r *http.Request) {
if h.content == nil {
jsonWrite(w, 503, map[string]string{"error": "content unavailable"})