66 lines
1.7 KiB
Go
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 MagazineHandler struct {
|
|
repo *repository.MagazineRepo
|
|
}
|
|
|
|
func NewMagazineHandler(repo *repository.MagazineRepo) *MagazineHandler {
|
|
return &MagazineHandler{repo: repo}
|
|
}
|
|
|
|
func (h *MagazineHandler) ListMagazines(w http.ResponseWriter, r *http.Request) {
|
|
docs, err := h.repo.GetAllMetas(r.Context())
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch magazines")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, docs)
|
|
}
|
|
|
|
func (h *MagazineHandler) GetMagazine(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 magazine")
|
|
return
|
|
}
|
|
if doc == nil {
|
|
response.Error(w, http.StatusNotFound, "magazine not found")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, doc)
|
|
}
|
|
|
|
func (h *MagazineHandler) ListIssues(w http.ResponseWriter, r *http.Request) {
|
|
book := chi.URLParam(r, "book")
|
|
docs, err := h.repo.GetIssues(r.Context(), book)
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch issues")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, docs)
|
|
}
|
|
|
|
func (h *MagazineHandler) GetIssue(w http.ResponseWriter, r *http.Request) {
|
|
book := chi.URLParam(r, "book")
|
|
slug := chi.URLParam(r, "slug")
|
|
doc, err := h.repo.GetIssue(r.Context(), book, slug)
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch issue")
|
|
return
|
|
}
|
|
if doc == nil {
|
|
response.Error(w, http.StatusNotFound, "issue not found")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, doc)
|
|
}
|