orangesite/hn/handlers.go

95 lines
2.2 KiB
Go
Raw Normal View History

2020-03-24 20:32:35 +00:00
package hn
import (
"net/http"
2020-03-25 00:49:19 +00:00
"time"
2020-03-24 20:32:35 +00:00
2020-03-25 00:49:19 +00:00
"github.com/flosch/pongo2"
2020-03-25 03:55:03 +00:00
"github.com/jinzhu/gorm"
2020-03-24 20:32:35 +00:00
"github.com/labstack/echo"
)
2020-03-25 03:55:03 +00:00
type RequestHandlerSet struct {
db *gorm.DB
}
func NewRequestHandlerSet(db *gorm.DB) *RequestHandlerSet {
rhs := new(RequestHandlerSet)
rhs.db = db
return rhs
}
func (r *RequestHandlerSet) indexHandler(c echo.Context) error {
var fpi []HNFrontPage
r.db.Where("disappeared is not ?", SQLITE_NULL_DATETIME).Order("disappeared desc").Find(&fpi)
type fprow struct {
Duration string
DurationSecs uint
URL string
Title string
HighestRank uint
HNID uint
Score uint
TimeGone string
TimeGoneSecs uint
2020-03-25 03:55:03 +00:00
}
var fprows []fprow
for _, item := range fpi {
fprows = append(fprows, fprow{
Duration: timeDiffHuman(item.Disappeared, item.Appeared),
DurationSecs: timeDiffAbsSeconds(item.Disappeared, item.Appeared),
URL: item.URL,
HNID: item.HNID,
Score: item.Score,
Title: item.Title,
HighestRank: item.HighestRank,
TimeGone: timeDiffHuman(time.Now(), item.Disappeared),
TimeGoneSecs: timeDiffAbsSeconds(time.Now(), item.Disappeared),
2020-03-25 03:55:03 +00:00
})
}
type rowtwo struct {
Duration string
DurationSecs uint
URL string
Title string
Score uint
HighestRank uint
HNID uint
Rank uint
2020-03-25 03:55:03 +00:00
}
var currentfp []rowtwo
var cur []HNFrontPage
r.db.Where("disappeared is ?", SQLITE_NULL_DATETIME).Order("rank asc").Find(&cur)
for _, item := range cur {
currentfp = append(currentfp, rowtwo{
Duration: timeDiffHuman(time.Now(), item.Appeared),
DurationSecs: timeDiffAbsSeconds(time.Now(), item.Appeared),
URL: item.URL,
HNID: item.HNID,
Score: item.Score,
Title: item.Title,
HighestRank: item.HighestRank,
Rank: item.Rank,
2020-03-25 03:55:03 +00:00
})
}
2020-03-25 00:49:19 +00:00
tc := pongo2.Context{
2020-03-25 03:55:03 +00:00
"time": time.Now().UTC().Format(time.RFC3339Nano),
"exits": fprows,
"current": currentfp,
2020-03-25 00:49:19 +00:00
}
return c.Render(http.StatusOK, "index.html", tc)
}
2020-03-25 03:55:03 +00:00
func (r *RequestHandlerSet) aboutHandler(c echo.Context) error {
2020-03-25 00:49:19 +00:00
tc := pongo2.Context{
"time": time.Now().UTC().Format(time.RFC3339Nano),
}
return c.Render(http.StatusOK, "about.html", tc)
2020-03-24 20:32:35 +00:00
}