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

66 lines
1.8 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 OtherDocumentHandler struct {
repo *repository.OtherDocumentRepo
}
func NewOtherDocumentHandler(repo *repository.OtherDocumentRepo) *OtherDocumentHandler {
return &OtherDocumentHandler{repo: repo}
}
func (h *OtherDocumentHandler) ListDocuments(w http.ResponseWriter, r *http.Request) {
docs, err := h.repo.GetAllMetas(r.Context())
if err != nil {
response.Error(w, http.StatusInternalServerError, "failed to fetch documents")
return
}
response.JSON(w, http.StatusOK, docs)
}
func (h *OtherDocumentHandler) GetDocument(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 document")
return
}
if doc == nil {
response.Error(w, http.StatusNotFound, "document not found")
return
}
response.JSON(w, http.StatusOK, doc)
}
func (h *OtherDocumentHandler) 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 *OtherDocumentHandler) 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)
}