158 lines
2.6 KiB
Go
158 lines
2.6 KiB
Go
package bootstrap
|
|
|
|
import (
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type PageKind string
|
|
|
|
const (
|
|
PageIndex PageKind = "main"
|
|
PageCourses PageKind = "courses"
|
|
PageAbout PageKind = "about"
|
|
)
|
|
|
|
func getCompactedValue(value int) string {
|
|
var (
|
|
myValue float64
|
|
dim string
|
|
)
|
|
switch {
|
|
case value/1e6 > 0:
|
|
cutted := value / 1e3
|
|
myValue, dim = float64(cutted)/1e3, "m"
|
|
case value/1e3 > 0:
|
|
myValue, dim = float64(value/1e3), "k"
|
|
default:
|
|
myValue, dim = float64(value), ""
|
|
}
|
|
|
|
return strings.TrimSuffix(strconv.FormatFloat(myValue, 'f', 3, 32), ".000") + dim
|
|
}
|
|
|
|
func MakeNewStats(courses, clients, categories int) stats {
|
|
return stats{
|
|
CoursesCount: getCompactedValue(courses),
|
|
ClientsCount: getCompactedValue(clients),
|
|
CategoriesCount: getCompactedValue(categories),
|
|
}
|
|
}
|
|
|
|
type stats struct {
|
|
CoursesCount string
|
|
ClientsCount string
|
|
CategoriesCount string
|
|
}
|
|
|
|
type NameIDPair struct {
|
|
ID string
|
|
Name string
|
|
}
|
|
|
|
type SortingItem struct {
|
|
NameIDPair
|
|
|
|
Ascending bool
|
|
}
|
|
|
|
type SortingView struct {
|
|
Items []SortingItem
|
|
}
|
|
|
|
type CoursesFilterViewParams struct {
|
|
SelectedSchoolID string
|
|
Schools []NameIDPair
|
|
OrderBy string
|
|
OrderFields []NameIDPair
|
|
Ascending bool
|
|
}
|
|
|
|
type Category struct {
|
|
ID string
|
|
Name string
|
|
}
|
|
|
|
func (c Category) Empty() bool {
|
|
return c == (Category{})
|
|
}
|
|
|
|
type BreadcrumbsParams struct {
|
|
ActiveLearningType Category
|
|
ActiveCourseThematic Category
|
|
}
|
|
|
|
type FilterFormParams struct {
|
|
BreadcrumbsParams
|
|
|
|
Schools CoursesFilterViewParams
|
|
AvailableLearningTypes []Category
|
|
AvailableCourseThematics []Category
|
|
Render bool
|
|
}
|
|
|
|
type CourseInfo struct {
|
|
ID string
|
|
Name string
|
|
FullPrice int
|
|
ImageLink string
|
|
OriginLink string
|
|
}
|
|
|
|
type CategoryBaseInfo struct {
|
|
ID string
|
|
Name string
|
|
Description string
|
|
Count int
|
|
}
|
|
|
|
type CategoryContainer struct {
|
|
CategoryBaseInfo
|
|
|
|
Subcategories []SubcategoryContainer
|
|
}
|
|
|
|
type SubcategoryContainer struct {
|
|
CategoryBaseInfo
|
|
|
|
Courses []CourseInfo
|
|
}
|
|
|
|
type ListCoursesParams struct {
|
|
FilterForm FilterFormParams
|
|
Categories []CategoryContainer
|
|
Pagination Pagination
|
|
Items int
|
|
Courses []CourseInfo
|
|
}
|
|
|
|
func GetOrFallback[T comparable](value T, fallback T) T {
|
|
var zeroValue T
|
|
if value == zeroValue {
|
|
return fallback
|
|
}
|
|
return value
|
|
}
|
|
|
|
type ordered interface {
|
|
~int8 | ~int16 | ~int32 | ~int64 | ~int |
|
|
~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uint |
|
|
~float32 | float64 | ~string
|
|
}
|
|
|
|
func min[T ordered](lhs, rhs T) T {
|
|
if lhs < rhs {
|
|
return lhs
|
|
}
|
|
|
|
return rhs
|
|
}
|
|
|
|
func max[T ordered](lhs, rhs T) T {
|
|
if lhs > rhs {
|
|
return lhs
|
|
}
|
|
|
|
return rhs
|
|
}
|