fleets/handlers/vehicles.go

59 lines
1.2 KiB
Go
Raw Permalink Normal View History

2022-08-12 12:49:16 +00:00
package handlers
import (
"time"
"git.coopgo.io/coopgo-platform/fleets/storage"
"github.com/google/uuid"
)
func (h FleetsHandler) AddVehicle(vehicle storage.Vehicle) (*storage.Vehicle, error) {
if vehicle.ID == "" {
vehicle.ID = uuid.NewString()
}
vehicle.Metadata = map[string]any{
"created": time.Now(),
}
// Store the account
if err := h.storage.CreateVehicle(vehicle); err != nil {
return nil, err
}
return &vehicle, nil
}
func (h FleetsHandler) GetVehicle(id string) (vehicle *storage.Vehicle, err error) {
vehicle, err = h.storage.GetVehicle(id)
if err != nil {
return nil, err
}
bookings, err := h.storage.GetBookingsForVehicle(id)
if err != nil {
return nil, err
}
vehicle.Bookings = bookings
return vehicle, nil
}
2022-11-07 00:33:53 +00:00
func (h FleetsHandler) GetVehicles(namespaces []string, filters storage.VehicleFilters) ([]storage.Vehicle, error) {
2022-08-12 12:49:16 +00:00
vehicles, err := h.storage.GetVehicles(namespaces)
newvehicles := []storage.Vehicle{}
for _, v := range vehicles {
bookings, err := h.storage.GetBookingsForVehicle(v.ID)
if err != nil {
return nil, err
}
v.Bookings = bookings
2022-11-07 00:33:53 +00:00
if !v.MatchesFilters(filters) {
continue
}
2022-08-12 12:49:16 +00:00
newvehicles = append(newvehicles, v)
}
return newvehicles, err
}