50 lines
2.0 KiB
Go
50 lines
2.0 KiB
Go
package handlers
|
|
|
|
import (
|
|
"errors"
|
|
"log"
|
|
"net/http"
|
|
"thamanyah/discovery/internal/api"
|
|
"thamanyah/discovery/internal/db/repositories"
|
|
)
|
|
|
|
// GetVideo serves the catalogue's copy of one video.
|
|
//
|
|
// @Summary Get a catalogued video
|
|
// @Description Returns the catalogue's copy of a video: what a reader needs to show and play it. A video appears here only after the CMS has announced it as ready, so a video that is still transcoding — or one the CMS never made ready — is a 404.
|
|
// @Tags videos
|
|
// @Produce json
|
|
// @Param id path string true "Video id, as issued by the CMS"
|
|
// @Success 200 {object} api.Video
|
|
// @Failure 404 {object} api.ProblemDetails
|
|
// @Failure 500 {object} api.ProblemDetails
|
|
// @Router /api/videos/{id} [get]
|
|
func GetVideo(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
|
|
video, err := repositories.VideoRepo.GetVideoByID(r.Context(), id)
|
|
if errors.Is(err, repositories.ErrVideoNotFound) {
|
|
writeProblem(w, http.StatusNotFound, "Video Not In The Catalogue",
|
|
"No video with that id is in the catalogue. A video appears here once the CMS announces it as ready, so one that is still being transcoded is not here yet.")
|
|
return
|
|
}
|
|
if err != nil {
|
|
log.Printf("Something Went Wrong Loading A Catalogued Video: id=%q: %s", id, err)
|
|
writeProblem(w, http.StatusInternalServerError, "Something Went Wrong Loading The Catalogue",
|
|
"The video could not be read from the database. This is a server-side fault and the request was not processed; retrying in a few moments may succeed.")
|
|
return
|
|
}
|
|
|
|
// Translated field by field rather than returned as-is: models.Video is the
|
|
// row, api.Video is the published schema, and neither should drag the other
|
|
// along when it changes.
|
|
writeJSON(w, http.StatusOK, api.Video{
|
|
ID: video.ID,
|
|
Title: video.Title,
|
|
PlaybackURL: video.PlaybackURL,
|
|
// Never null on the wire: a video with no categories has an empty
|
|
// list, which is a different thing from "unknown".
|
|
Categories: append([]string{}, video.Categories...),
|
|
})
|
|
}
|