Redmage/server/routes/page_subreddits_details.go

48 lines
1.3 KiB
Go
Raw Normal View History

package routes
import (
"net/http"
"github.com/go-chi/chi/v5"
"github.com/tigorlazuardi/redmage/api"
"github.com/tigorlazuardi/redmage/pkg/errs"
"github.com/tigorlazuardi/redmage/pkg/log"
"github.com/tigorlazuardi/redmage/views"
"github.com/tigorlazuardi/redmage/views/subredditsview/detailsview"
)
func (routes *Routes) PageSubredditsDetails(rw http.ResponseWriter, r *http.Request) {
ctx, span := tracer.Start(r.Context(), "*Routes.PageSubredditsDetails")
defer span.End()
name := chi.URLParam(r, "name")
var params api.SubredditGetByNameImageParams
params.FillFromQuery(r.URL.Query())
var data detailsview.Data
var err error
data.Params = params
c := views.NewContext(routes.Config, r)
result, err := routes.API.SubredditGetByNameWithImages(ctx, name, params)
if err != nil {
log.New(ctx).Err(err).Error("failed to get subreddit by name")
code, message := errs.HTTPMessage(err)
rw.WriteHeader(code)
data.Error = message
if err := detailsview.Detailsview(c, data).Render(ctx, rw); err != nil {
log.New(ctx).Err(err).Error("failed to render subreddit details page")
}
return
}
data.Subreddit = result.Subreddit
data.Images = result.Images
data.TotalImages = result.Total
if err := detailsview.Detailsview(c, data).Render(ctx, rw); err != nil {
log.New(ctx).Err(err).Error("failed to render subreddit details page")
}
}