60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package storage
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type Vehicle struct {
|
|
ID string `json:"id" bson:"_id"`
|
|
Type string `json:"type"`
|
|
Namespace string `json:"namespace"`
|
|
Administrators []string `json:"administrators"`
|
|
Data map[string]any `json:"data"`
|
|
Metadata map[string]any `json:"metadata"`
|
|
|
|
Bookings []Booking
|
|
}
|
|
|
|
type VehicleFilters struct {
|
|
Types []string
|
|
AvailableFrom time.Time
|
|
AvailableTo time.Time
|
|
}
|
|
|
|
func (v Vehicle) Free(start time.Time, end time.Time) bool {
|
|
for _, b := range v.Bookings {
|
|
fmt.Println("Bookings for", v)
|
|
fmt.Println(b)
|
|
if (start.Before(b.Unavailablefrom) && end.After(b.Unavailablefrom.Add(24*time.Hour))) ||
|
|
(start.Before(b.Unavailableto) && end.After(b.Unavailableto.Add(24*time.Hour))) ||
|
|
(start.After(b.Unavailablefrom) && end.Before(b.Unavailableto.Add(24*time.Hour))) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (v Vehicle) MatchesFilters(filters VehicleFilters) bool {
|
|
if len(filters.Types) > 0 {
|
|
found := false
|
|
for _, t := range filters.Types {
|
|
if t == v.Type {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
if !found {
|
|
return false
|
|
}
|
|
}
|
|
|
|
if !v.Free(filters.AvailableFrom, filters.AvailableTo) {
|
|
return false
|
|
}
|
|
|
|
fmt.Println(filters)
|
|
|
|
return true
|
|
}
|