130 lines
3.8 KiB
Go
130 lines
3.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/prabhupadasays/spsays-bff/internal/repository"
|
|
"github.com/prabhupadasays/spsays-bff/pkg/response"
|
|
)
|
|
|
|
type TranscriptionHandler struct {
|
|
repo *repository.TranscriptionRepo
|
|
}
|
|
|
|
func NewTranscriptionHandler(repo *repository.TranscriptionRepo) *TranscriptionHandler {
|
|
return &TranscriptionHandler{repo: repo}
|
|
}
|
|
|
|
func (h *TranscriptionHandler) 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 transcription")
|
|
return
|
|
}
|
|
if doc == nil {
|
|
response.Error(w, http.StatusNotFound, "transcription not found")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, doc)
|
|
}
|
|
|
|
func (h *TranscriptionHandler) GetByYear(w http.ResponseWriter, r *http.Request) {
|
|
yearStr := chi.URLParam(r, "year")
|
|
year, err := strconv.Atoi(yearStr)
|
|
if err != nil {
|
|
response.Error(w, http.StatusBadRequest, "invalid year")
|
|
return
|
|
}
|
|
docs, err := h.repo.GetByYear(r.Context(), year)
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch transcriptions")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, docs)
|
|
}
|
|
|
|
func (h *TranscriptionHandler) GetByPlace(w http.ResponseWriter, r *http.Request) {
|
|
place := chi.URLParam(r, "place")
|
|
docs, err := h.repo.GetByPlace(r.Context(), place)
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch transcriptions")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, docs)
|
|
}
|
|
|
|
func (h *TranscriptionHandler) GetByType(w http.ResponseWriter, r *http.Request) {
|
|
docType := chi.URLParam(r, "type")
|
|
if docType != "lecture" && docType != "conversation" {
|
|
response.Error(w, http.StatusBadRequest, "invalid type: must be 'lecture' or 'conversation'")
|
|
return
|
|
}
|
|
docs, err := h.repo.GetByType(r.Context(), docType)
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch transcriptions")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, docs)
|
|
}
|
|
|
|
func (h *TranscriptionHandler) GetYears(w http.ResponseWriter, r *http.Request) {
|
|
docs, err := h.repo.GetYears(r.Context())
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch years")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, docs)
|
|
}
|
|
|
|
func (h *TranscriptionHandler) GetPlaces(w http.ResponseWriter, r *http.Request) {
|
|
docs, err := h.repo.GetPlaces(r.Context())
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch places")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, docs)
|
|
}
|
|
|
|
func (h *TranscriptionHandler) GetTypeDistribution(w http.ResponseWriter, r *http.Request) {
|
|
lectures, conversations, err := h.repo.GetTypeDistribution(r.Context())
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch type distribution")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, map[string]int64{
|
|
"lectures": lectures,
|
|
"conversations": conversations,
|
|
})
|
|
}
|
|
|
|
func (h *TranscriptionHandler) Count(w http.ResponseWriter, r *http.Request) {
|
|
count, err := h.repo.Count(r.Context())
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to count transcriptions")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, map[string]int64{"count": count})
|
|
}
|
|
|
|
func (h *TranscriptionHandler) GetAdjacent(w http.ResponseWriter, r *http.Request) {
|
|
slug := chi.URLParam(r, "slug")
|
|
yearStr := r.URL.Query().Get("year")
|
|
year, err := strconv.Atoi(yearStr)
|
|
if err != nil {
|
|
response.Error(w, http.StatusBadRequest, "invalid year")
|
|
return
|
|
}
|
|
prev, next, err := h.repo.GetAdjacent(r.Context(), slug, year)
|
|
if err != nil {
|
|
response.Error(w, http.StatusInternalServerError, "failed to fetch adjacent transcriptions")
|
|
return
|
|
}
|
|
response.JSON(w, http.StatusOK, map[string]interface{}{
|
|
"prev": prev,
|
|
"next": next,
|
|
})
|
|
}
|