2024-04-09 14:30:54 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
|
|
|
"github.com/tigorlazuardi/redmage/views"
|
|
|
|
"github.com/tigorlazuardi/redmage/views/homeview"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (routes *Routes) PageHome(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
vc := views.NewContext(routes.Config, r)
|
2024-04-09 16:09:08 +07:00
|
|
|
|
|
|
|
params := parseSubredditListQuery(r)
|
|
|
|
|
|
|
|
list, err := routes.API.ListSubreddits(ctx, params)
|
|
|
|
if err != nil {
|
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := homeview.Data{
|
|
|
|
SubredditsList: list,
|
2024-04-09 21:49:23 +07:00
|
|
|
Error: err,
|
2024-04-09 16:09:08 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := homeview.Home(vc, data).Render(ctx, rw); err != nil {
|
2024-04-09 14:30:54 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to render home view")
|
|
|
|
}
|
|
|
|
}
|