41 lines
995 B
Go
41 lines
995 B
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 EssayHandler struct {
|
|
repo *repository.EssayRepo
|
|
}
|
|
|
|
func NewEssayHandler(repo *repository.EssayRepo) *EssayHandler {
|
|
return &EssayHandler{repo: repo}
|
|
}
|
|
|
|
func (h *EssayHandler) List(w http.ResponseWriter, r *http.Request) {
|
|
docs, err := h.repo.GetAll(r.Context())
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch essays")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, docs)
|
|
}
|
|
|
|
func (h *EssayHandler) GetBySlug(w http.ResponseWriter, r *http.Request) {
|
|
slug := chi.URLParam(r, "slug")
|
|
doc, err := h.repo.GetBySlug(r.Context(), slug)
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch essay")
|
|
return
|
|
}
|
|
if doc == nil {
|
|
response.Error(w, http.StatusNotFound, "essay not found")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, doc)
|
|
}
|