72 lines
1.6 KiB
Go
72 lines
1.6 KiB
Go
package handlers
|
|
|
|
import (
|
|
"time"
|
|
|
|
// "Documents/go_projet/go/pkg/mod/git.coopgo.io/coopgo-platform/fleets@v0.0.0-20230310144446-feb935f8bf4e/storage"
|
|
|
|
"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
|
|
}
|
|
|
|
func (h FleetsHandler) GetVehicles(namespaces []string, filters storage.VehicleFilters) ([]storage.Vehicle, error) {
|
|
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
|
|
|
|
if !v.MatchesFilters(filters) {
|
|
continue
|
|
}
|
|
|
|
newvehicles = append(newvehicles, v)
|
|
}
|
|
return newvehicles, err
|
|
}
|
|
|
|
/////////////////update///////////////
|
|
func (h FleetsHandler) UpdateVehicle(vehicle storage.Vehicle) (*storage.Vehicle, error) {
|
|
|
|
// Store the account
|
|
if err := h.storage.UpdateVehicle(vehicle); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &vehicle, nil
|
|
}
|