2024-04-09 14:30:54 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2024-04-27 22:07:20 +07:00
|
|
|
"time"
|
2024-04-09 14:30:54 +07:00
|
|
|
|
2024-05-29 19:39:02 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/api"
|
2024-04-27 22:07:20 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
2024-04-09 14:30:54 +07:00
|
|
|
"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) {
|
2024-04-13 00:15:31 +07:00
|
|
|
ctx, span := tracer.Start(r.Context(), "routes.PageHome")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-05-04 22:17:50 +07:00
|
|
|
var data homeview.Data
|
|
|
|
|
2024-04-09 14:30:54 +07:00
|
|
|
vc := views.NewContext(routes.Config, r)
|
2024-04-09 16:09:08 +07:00
|
|
|
|
2024-05-13 15:27:36 +07:00
|
|
|
data.ListSubredditParams.FillFromQuery(r.URL.Query())
|
|
|
|
data.ListSubredditParams.Limit = 0
|
2024-05-29 19:39:02 +07:00
|
|
|
data.ListSubredditParams.Offset = 0
|
2024-05-13 15:27:36 +07:00
|
|
|
list, err := routes.API.ListSubreddits(ctx, data.ListSubredditParams)
|
2024-04-09 16:09:08 +07:00
|
|
|
if err != nil {
|
2024-04-27 22:07:20 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to list subreddits")
|
|
|
|
code, message := errs.HTTPMessage(err)
|
2024-05-05 22:34:05 +07:00
|
|
|
data.Error = message
|
2024-04-27 22:07:20 +07:00
|
|
|
rw.WriteHeader(code)
|
2024-05-05 22:34:05 +07:00
|
|
|
if err := homeview.Home(vc, data).Render(ctx, rw); err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to render home view")
|
|
|
|
}
|
2024-04-27 22:07:20 +07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-13 15:27:36 +07:00
|
|
|
data.ImageListParams.FillFromQuery(r.URL.Query())
|
|
|
|
if data.ImageListParams.CreatedAt.IsZero() {
|
|
|
|
data.ImageListParams.CreatedAt = time.Now().Add(-time.Hour * 24) // images in the last 24 hours
|
2024-04-29 21:45:18 +07:00
|
|
|
}
|
2024-05-29 19:39:02 +07:00
|
|
|
if r.URL.Query().Get("limit") == "" {
|
|
|
|
data.ImageListParams.Limit = 100
|
|
|
|
}
|
2024-04-27 22:07:20 +07:00
|
|
|
|
2024-05-13 15:27:36 +07:00
|
|
|
imageList, err := routes.API.ImagesListWithDevicesAndSubreddits(ctx, data.ImageListParams)
|
2024-04-27 22:07:20 +07:00
|
|
|
if err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to list subreddits")
|
|
|
|
code, message := errs.HTTPMessage(err)
|
2024-04-29 15:18:23 +07:00
|
|
|
data := homeview.Data{Error: message}
|
2024-04-27 22:07:20 +07:00
|
|
|
rw.WriteHeader(code)
|
2024-04-29 15:18:23 +07:00
|
|
|
if err := homeview.Home(vc, data).Render(ctx, rw); err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to render home view")
|
|
|
|
}
|
2024-04-27 22:07:20 +07:00
|
|
|
return
|
2024-04-09 16:09:08 +07:00
|
|
|
}
|
|
|
|
|
2024-05-29 19:39:02 +07:00
|
|
|
data.Devices, err = routes.API.GetDevices(ctx, api.DevicesListParams{Status: -1})
|
|
|
|
if err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to list subreddits")
|
|
|
|
code, message := errs.HTTPMessage(err)
|
|
|
|
data := homeview.Data{Error: message}
|
|
|
|
rw.WriteHeader(code)
|
|
|
|
if err := homeview.Home(vc, data).Render(ctx, rw); err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("failed to render home view")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-05-04 22:17:50 +07:00
|
|
|
data.SubredditsList = list
|
|
|
|
data.RecentlyAddedImages = homeview.NewRecentlyAddedImages(imageList.Images)
|
|
|
|
data.Now = time.Now()
|
|
|
|
data.TotalImages = imageList.Total
|
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")
|
|
|
|
}
|
|
|
|
}
|