diff --git a/internal/database/break.go b/internal/database/break.go index feb4938..1aaaef8 100644 --- a/internal/database/break.go +++ b/internal/database/break.go @@ -37,13 +37,13 @@ import ( type CreateBreakParams struct { VehicleID *int64 `json:"vehicle_id,string" example:"1234567812345678" validate:"required" swaggerignore:"true"` - Service *int64 `json:"service" validate:"omitempty,min=0" example:"120"` + Service *string `json:"service" validate:"omitempty" example:"00:02:00"` Data *interface{} `json:"data" swaggertype:"object,string" example:"key1:value1,key2:value2"` } type UpdateBreakParams struct { VehicleID *int64 `json:"vehicle_id,string" example:"1234567812345678" swaggerignore:"true"` - Service *int64 `json:"service" validate:"omitempty,min=0" example:"120"` + Service *string `json:"service" validate:"omitempty" example:"00:02:00"` Data *interface{} `json:"data" swaggertype:"object,string" example:"key1:value1,key2:value2"` } diff --git a/internal/database/crud.go b/internal/database/crud.go index 04d5719..d1d0a17 100644 --- a/internal/database/crud.go +++ b/internal/database/crud.go @@ -47,7 +47,7 @@ func createResource(resource string, resourceStruct interface{}) (sql string, ar // Convert any interval field to its type if _, intervalFieldFound := util.IntervalFields[field]; intervalFieldFound { - val = val + "* '1 sec'::INTERVAL" + val = val + "::INTERVAL" } if i == 0 { @@ -74,7 +74,7 @@ func updateResource(resource string, resourceStruct interface{}, id int64) (sql // Convert any interval field to its type if _, intervalFieldFound := util.IntervalFields[field]; intervalFieldFound { - val = val + "* '1 sec'::INTERVAL" + val = val + "::INTERVAL" } if i == 0 { diff --git a/internal/database/job.go b/internal/database/job.go index ae945ed..bf87791 100644 --- a/internal/database/job.go +++ b/internal/database/job.go @@ -37,8 +37,8 @@ import ( type CreateJobParams struct { Location *util.LocationParams `json:"location" validate:"required"` - Setup *int64 `json:"setup" validate:"omitempty,min=0" example:"0"` - Service *int64 `json:"service" validate:"omitempty,min=0" example:"120"` + Setup *string `json:"setup" validate:"omitempty" example:"00:00:00"` + Service *string `json:"service" validate:"omitempty" example:"00:02:00"` Delivery *[]int64 `json:"delivery" validate:"omitempty,dive,min=0" example:"10,20"` Pickup *[]int64 `json:"pickup" validate:"omitempty,dive,min=0" example:"5,15"` Skills *[]int32 `json:"skills" validate:"omitempty,dive,min=0" example:"1,5"` @@ -49,8 +49,8 @@ type CreateJobParams struct { type UpdateJobParams struct { Location *util.LocationParams `json:"location"` - Setup *int64 `json:"setup" validate:"omitempty,min=0" example:"0"` - Service *int64 `json:"service" validate:"omitempty,min=0" example:"120"` + Setup *string `json:"setup" validate:"omitempty" example:"00:00:00"` + Service *string `json:"service" validate:"omitempty" example:"00:02:00"` Delivery *[]int64 `json:"delivery" validate:"omitempty,dive,min=0" example:"10,20"` Pickup *[]int64 `json:"pickup" validate:"omitempty,dive,min=0" example:"5,15"` Skills *[]int32 `json:"skills" validate:"omitempty,dive,min=0" example:"1,5"` diff --git a/internal/database/models.go b/internal/database/models.go index 2531c71..ccb60bb 100644 --- a/internal/database/models.go +++ b/internal/database/models.go @@ -35,7 +35,7 @@ import ( type Break struct { ID int64 `json:"id,string" example:"1234567812345678"` VehicleID int64 `json:"vehicle_id,string" example:"1234567812345678"` - Service int64 `json:"service" example:"120"` + Service string `json:"service" example:"00:02:00"` Data interface{} `json:"data" swaggertype:"object,string" example:"key1:value1,key2:value2"` CreatedAt string `json:"created_at" example:"2021-12-01 13:00:00"` UpdatedAt string `json:"updated_at" example:"2021-12-01 13:00:00"` @@ -52,8 +52,8 @@ type BreakTimeWindow struct { type Job struct { ID int64 `json:"id,string" example:"1234567812345678"` Location util.LocationParams `json:"location"` - Setup int64 `json:"setup" example:"0"` - Service int64 `json:"service" example:"120"` + Setup string `json:"setup" example:"00:00:00"` + Service string `json:"service" example:"00:02:00"` Delivery []int64 `json:"delivery" example:"10,20"` Pickup []int64 `json:"pickup" example:"5,15"` Skills []int32 `json:"skills" example:"1,5"` @@ -83,11 +83,11 @@ type Project struct { type Shipment struct { ID int64 `json:"id,string" example:"1234567812345678"` PLocation util.LocationParams `json:"p_location" ` - PSetup int64 `json:"p_setup" example:"0"` - PService int64 `json:"p_service" example:"120"` + PSetup string `json:"p_setup" example:"00:00:00"` + PService string `json:"p_service" example:"00:02:00"` DLocation util.LocationParams `json:"d_location"` - DSetup int64 `json:"d_setup" example:"0"` - DService int64 `json:"d_service" example:"120"` + DSetup string `json:"d_setup" example:"00:00:00"` + DService string `json:"d_service" example:"00:02:00"` Amount []int64 `json:"amount" example:"5,15"` Skills []int32 `json:"skills" example:"1,5"` Priority int32 `json:"priority" example:"10"` diff --git a/internal/database/shipment.go b/internal/database/shipment.go index 238a394..68128dd 100644 --- a/internal/database/shipment.go +++ b/internal/database/shipment.go @@ -37,11 +37,11 @@ import ( type CreateShipmentParams struct { PLocation *util.LocationParams `json:"p_location" validate:"required"` - PSetup *int64 `json:"p_setup" validate:"omitempty,min=0" example:"0"` - PService *int64 `json:"p_service" validate:"omitempty,min=0" example:"120"` + PSetup *string `json:"p_setup" validate:"omitempty" example:"00:00:00"` + PService *string `json:"p_service" validate:"omitempty" example:"00:02:00"` DLocation *util.LocationParams `json:"d_location" validate:"required"` - DSetup *int64 `json:"d_setup" validate:"omitempty,min=0" example:"0"` - DService *int64 `json:"d_service" validate:"omitempty,min=0" example:"120"` + DSetup *string `json:"d_setup" validate:"omitempty" example:"00:00:00"` + DService *string `json:"d_service" validate:"omitempty" example:"00:02:00"` Amount *[]int64 `json:"amount" validate:"omitempty,dive,min=0" example:"5,15"` Skills *[]int32 `json:"skills" validate:"omitempty,dive,min=0" example:"1,5"` Priority *int32 `json:"priority" validate:"omitempty,min=0,max=100" example:"10"` @@ -51,11 +51,11 @@ type CreateShipmentParams struct { type UpdateShipmentParams struct { PLocation *util.LocationParams `json:"p_location"` - PSetup *int64 `json:"p_setup" validate:"omitempty,min=0" example:"0"` - PService *int64 `json:"p_service" validate:"omitempty,min=0" example:"120"` + PSetup *string `json:"p_setup" validate:"omitempty" example:"00:00:00"` + PService *string `json:"p_service" validate:"omitempty" example:"00:02:00"` DLocation *util.LocationParams `json:"d_location"` - DSetup *int64 `json:"d_setup" validate:"omitempty,min=0" example:"0"` - DService *int64 `json:"d_service" validate:"omitempty,min=0" example:"120"` + DSetup *string `json:"d_setup" validate:"omitempty" example:"00:00:00"` + DService *string `json:"d_service" validate:"omitempty" example:"00:02:00"` Amount *[]int64 `json:"amount" validate:"omitempty,dive,min=0" example:"5,15"` Skills *[]int32 `json:"skills" validate:"omitempty,dive,min=0" example:"1,5"` Priority *int32 `json:"priority" validate:"omitempty,min=0,max=100" example:"10"` diff --git a/internal/util/db_error.go b/internal/util/db_error.go index 6efa4be..5a00aa5 100644 --- a/internal/util/db_error.go +++ b/internal/util/db_error.go @@ -50,8 +50,10 @@ func HandleDBError(err error) error { err = fmt.Errorf("Shipment with the given 'shipment_id' does not exist") case "breaks_vehicle_id_fkey": err = fmt.Errorf("Vehicle with the given 'vehicle_id' does not exist") + case "jobs_check": err = fmt.Errorf("Field 'pickup' and 'delivery' must have same length") + case "breaks_time_windows_check": err = fmt.Errorf("Field 'tw_open' must be less than or equal to field 'tw_close'") case "jobs_time_windows_check": @@ -60,12 +62,28 @@ func HandleDBError(err error) error { err = fmt.Errorf("Field 'tw_open' must be less than or equal to field 'tw_close'") case "shipments_time_windows_check": err = fmt.Errorf("Field 'tw_open' must be less than or equal to field 'tw_close'") + case "jobs_time_windows_pkey": err = fmt.Errorf("Jobs time window with given values already exist") case "shipments_time_windows_pkey": err = fmt.Errorf("Shipments time window with given values already exist") case "breaks_time_windows_pkey": err = fmt.Errorf("Breaks time window with given values already exist") + + case "jobs_service_check": + err = fmt.Errorf("Field 'service' must be non-negative with the format 'HH:MM:SS'") + case "jobs_setup_check": + err = fmt.Errorf("Field 'setup' must be non-negative with the format 'HH:MM:SS'") + case "shipments_p_service_check": + err = fmt.Errorf("Field 'p_service' must be non-negative with the format 'HH:MM:SS'") + case "shipments_d_service_check": + err = fmt.Errorf("Field 'd_service' must be non-negative with the format 'HH:MM:SS'") + case "shipments_p_setup_check": + err = fmt.Errorf("Field 'p_setup' must be non-negative with the format 'HH:MM:SS'") + case "shipments_d_setup_check": + err = fmt.Errorf("Field 'd_setup' must be non-negative with the format 'HH:MM:SS'") + case "breaks_service_check": + err = fmt.Errorf("Field 'service' must be non-negative with the format 'HH:MM:SS'") } } } diff --git a/internal/util/format_ical.go b/internal/util/format_ical.go index 472edc7..5f559f8 100644 --- a/internal/util/format_ical.go +++ b/internal/util/format_ical.go @@ -47,10 +47,10 @@ type Schedule struct { Location LocationParams `json:"location"` Arrival string `json:"arrival" example:"2021-12-01 13:00:00"` Departure string `json:"departure" example:"2021-12-01 13:00:00"` - TravelTime int64 `json:"travel_time" example:"1000"` - SetupTime int64 `json:"setup_time" example:"0"` - ServiceTime int64 `json:"service_time" example:"120"` - WaitingTime int64 `json:"waiting_time" example:"0"` + TravelTime string `json:"travel_time" example:"00:16:40"` + SetupTime string `json:"setup_time" example:"00:00:00"` + ServiceTime string `json:"service_time" example:"00:02:00"` + WaitingTime string `json:"waiting_time" example:"00:00:00"` Load []int64 `json:"load" example:"0,0"` VehicleData interface{} `json:"vehicle_data" swaggertype:"object,string" example:"key1:value1,key2:value2"` TaskData interface{} `json:"task_data" swaggertype:"object,string" example:"key1:value1,key2:value2"` @@ -85,13 +85,6 @@ func parseTime(time_str string) time.Time { return t } -func secondsToTime(totalSecs int64) string { - hours := totalSecs / 3600 - minutes := (totalSecs % 3600) / 60 - seconds := totalSecs % 60 - return fmt.Sprintf("%02d:%02d:%02d", hours, minutes, seconds) -} - func getSummary(schedule Schedule) string { return fmt.Sprintf("%s - Vehicle %d", strings.Title(schedule.Type), schedule.VehicleID) } @@ -104,9 +97,9 @@ func getDescription(schedule Schedule) string { desc := fmt.Sprintf("Project ID: %d\n", schedule.ProjectID) desc += fmt.Sprintf("Vehicle ID: %d\n", schedule.VehicleID) desc += fmt.Sprintf("Task ID: %d\n", schedule.TaskID) - desc += fmt.Sprintf("Travel Time: %s\n", secondsToTime(schedule.TravelTime)) - desc += fmt.Sprintf("Service Time: %s\n", secondsToTime(schedule.ServiceTime)) - desc += fmt.Sprintf("Waiting Time: %s\n", secondsToTime(schedule.WaitingTime)) + desc += fmt.Sprintf("Travel Time: %s\n", schedule.TravelTime) + desc += fmt.Sprintf("Service Time: %s\n", schedule.ServiceTime) + desc += fmt.Sprintf("Waiting Time: %s\n", schedule.WaitingTime) desc += fmt.Sprintf("Load: %d\n", schedule.Load) return desc } diff --git a/internal/util/query.go b/internal/util/query.go index e68450f..eecc068 100644 --- a/internal/util/query.go +++ b/internal/util/query.go @@ -131,7 +131,7 @@ func GetOutputFields(resourceStruct interface{}) (sql string) { } if _, intervalFieldFound := IntervalFields[fieldName]; intervalFieldFound { - fieldName = fmt.Sprintf("EXTRACT(epoch FROM %s)", fieldName) + fieldName = fmt.Sprintf("to_char(%s, 'HH24:MI:SS')", fieldName) } if _, timestampFieldFound := TimestampFields[fieldName]; timestampFieldFound { fieldName = fmt.Sprintf("to_char(%s, 'YYYY-MM-DD HH24:MI:SS')", fieldName)