mirror of
https://github.com/gabehf/Koito.git
synced 2026-03-18 11:46:34 -07:00
chore: initial public commit
This commit is contained in:
commit
fc9054b78c
250 changed files with 32809 additions and 0 deletions
77
engine/handlers/handlers.go
Normal file
77
engine/handlers/handlers.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
// package handlers implements route handlers
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gabehf/koito/internal/db"
|
||||
"github.com/gabehf/koito/internal/logger"
|
||||
)
|
||||
|
||||
const defaultLimitSize = 100
|
||||
const maximumLimit = 500
|
||||
|
||||
func OptsFromRequest(r *http.Request) db.GetItemsOpts {
|
||||
l := logger.FromContext(r.Context())
|
||||
|
||||
limitStr := r.URL.Query().Get("limit")
|
||||
limit, err := strconv.Atoi(limitStr)
|
||||
if err != nil {
|
||||
l.Debug().Msgf("query parameter 'limit' not specified, using default %d", defaultLimitSize)
|
||||
limit = defaultLimitSize
|
||||
}
|
||||
if limit > maximumLimit {
|
||||
l.Debug().Msgf("limit must not be greater than %d, using default %d", maximumLimit, defaultLimitSize)
|
||||
limit = defaultLimitSize
|
||||
}
|
||||
pageStr := r.URL.Query().Get("page")
|
||||
page, _ := strconv.Atoi(pageStr)
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
|
||||
weekStr := r.URL.Query().Get("week")
|
||||
week, _ := strconv.Atoi(weekStr)
|
||||
monthStr := r.URL.Query().Get("month")
|
||||
month, _ := strconv.Atoi(monthStr)
|
||||
yearStr := r.URL.Query().Get("year")
|
||||
year, _ := strconv.Atoi(yearStr)
|
||||
|
||||
artistIdStr := r.URL.Query().Get("artist_id")
|
||||
artistId, _ := strconv.Atoi(artistIdStr)
|
||||
albumIdStr := r.URL.Query().Get("album_id")
|
||||
albumId, _ := strconv.Atoi(albumIdStr)
|
||||
trackIdStr := r.URL.Query().Get("track_id")
|
||||
trackId, _ := strconv.Atoi(trackIdStr)
|
||||
|
||||
var period db.Period
|
||||
switch strings.ToLower(r.URL.Query().Get("period")) {
|
||||
case "day":
|
||||
period = db.PeriodDay
|
||||
case "week":
|
||||
period = db.PeriodWeek
|
||||
case "month":
|
||||
period = db.PeriodMonth
|
||||
case "year":
|
||||
period = db.PeriodYear
|
||||
case "all_time":
|
||||
period = db.PeriodAllTime
|
||||
default:
|
||||
l.Debug().Msgf("Using default value '%s' for period", db.PeriodDay)
|
||||
period = db.PeriodDay
|
||||
}
|
||||
|
||||
return db.GetItemsOpts{
|
||||
Limit: limit,
|
||||
Period: period,
|
||||
Page: page,
|
||||
Week: week,
|
||||
Month: month,
|
||||
Year: year,
|
||||
ArtistID: artistId,
|
||||
AlbumID: albumId,
|
||||
TrackID: trackId,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue