2024-04-30 15:28:04 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-05-01 18:16:54 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
2024-04-30 15:28:04 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
|
|
|
"github.com/tigorlazuardi/redmage/views"
|
2024-05-27 21:10:37 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/views/subreddits"
|
2024-04-30 15:28:04 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func (routes *Routes) PageSubreddits(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx, span := tracer.Start(r.Context(), "*Routes.PageSubreddits")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
c := views.NewContext(routes.Config, r)
|
2024-05-27 21:10:37 +07:00
|
|
|
var data subreddits.Data
|
2024-05-28 16:39:38 +07:00
|
|
|
data.Params.FillFromQuery(r.URL.Query())
|
2024-05-01 18:16:54 +07:00
|
|
|
|
2024-05-28 16:39:38 +07:00
|
|
|
var err error
|
|
|
|
data.Subreddits, err = routes.API.ListSubredditsWithCover(ctx, data.Params)
|
2024-05-01 18:16:54 +07:00
|
|
|
if err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to list subreddits")
|
|
|
|
code, message := errs.HTTPMessage(err)
|
|
|
|
rw.WriteHeader(code)
|
|
|
|
data.Error = message
|
2024-05-27 21:10:37 +07:00
|
|
|
if err := subreddits.View(c, data).Render(ctx, rw); err != nil {
|
2024-05-01 18:16:54 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to render subreddits")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-27 21:10:37 +07:00
|
|
|
if err := subreddits.View(c, data).Render(r.Context(), rw); err != nil {
|
2024-04-30 15:28:04 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to render subreddits view")
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|