Files
spsays-bff/internal/handler/journal.go
T
2026-05-09 23:19:19 +05:30

66 lines
1.7 KiB
Go

package handler
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/prabhupadasays/spsays-bff/internal/repository"
"github.com/prabhupadasays/spsays-bff/pkg/response"
)
type JournalHandler struct {
repo *repository.JournalRepo
}
func NewJournalHandler(repo *repository.JournalRepo) *JournalHandler {
return &JournalHandler{repo: repo}
}
func (h *JournalHandler) ListJournals(w http.ResponseWriter, r *http.Request) {
docs, err := h.repo.GetAllMetas(r.Context())
if err != nil {
response.Error(w, http.StatusInternalServerError, "failed to fetch journals")
return
}
response.JSON(w, http.StatusOK, docs)
}
func (h *JournalHandler) GetJournal(w http.ResponseWriter, r *http.Request) {
book := chi.URLParam(r, "book")
doc, err := h.repo.GetMeta(r.Context(), book)
if err != nil {
response.Error(w, http.StatusInternalServerError, "failed to fetch journal")
return
}
if doc == nil {
response.Error(w, http.StatusNotFound, "journal not found")
return
}
response.JSON(w, http.StatusOK, doc)
}
func (h *JournalHandler) ListChapters(w http.ResponseWriter, r *http.Request) {
book := chi.URLParam(r, "book")
docs, err := h.repo.GetChapters(r.Context(), book)
if err != nil {
response.Error(w, http.StatusInternalServerError, "failed to fetch chapters")
return
}
response.JSON(w, http.StatusOK, docs)
}
func (h *JournalHandler) GetChapter(w http.ResponseWriter, r *http.Request) {
book := chi.URLParam(r, "book")
slug := chi.URLParam(r, "slug")
doc, err := h.repo.GetChapter(r.Context(), book, slug)
if err != nil {
response.Error(w, http.StatusInternalServerError, "failed to fetch chapter")
return
}
if doc == nil {
response.Error(w, http.StatusNotFound, "chapter not found")
return
}
response.JSON(w, http.StatusOK, doc)
}