/
vehicle.go
182 lines (161 loc) · 6.45 KB
/
vehicle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*GRP-GNU-AGPL******************************************************************
File: vehicle.go
Copyright (C) 2021 Team Georepublic <info@georepublic.de>
Developer(s):
Copyright (C) 2021 Ashish Kumar <ashishkr23438@gmail.com>
-----
This file is part of pg_scheduleserv.
pg_scheduleserv is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
pg_scheduleserv is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with pg_scheduleserv. If not, see <https://www.gnu.org/licenses/>.
******************************************************************GRP-GNU-AGPL*/
package database
import (
"context"
"github.com/Georepublic/pg_scheduleserv/internal/util"
"github.com/jackc/pgx/v4"
)
type CreateVehicleParams struct {
StartLocation *util.LocationParams `json:"start_location" validate:"required"`
EndLocation *util.LocationParams `json:"end_location" validate:"required"`
Capacity *[]int64 `json:"capacity" validate:"omitempty,dive,min=0" example:"50,25"`
Skills *[]int32 `json:"skills" validate:"omitempty,dive,min=0" example:"1,5"`
TwOpen *string `json:"tw_open" validate:"omitempty,datetime=2006-01-02T15:04:05" example:"2021-12-31T23:00:00"`
TwClose *string `json:"tw_close" validate:"omitempty,datetime=2006-01-02T15:04:05" example:"2021-12-31T23:59:00"`
SpeedFactor *float64 `json:"speed_factor" validate:"omitempty,gt=0" example:"1.0"`
MaxTasks *int32 `json:"max_tasks" validate:"omitempty,gt=0" example:"20"`
ProjectID *int64 `json:"project_id,string" validate:"required" swaggerignore:"true"`
Data *interface{} `json:"data" swaggertype:"object,string" example:"key1:value1,key2:value2"`
}
type UpdateVehicleParams struct {
StartLocation *util.LocationParams `json:"start_location"`
EndLocation *util.LocationParams `json:"end_location"`
Capacity *[]int64 `json:"capacity" validate:"omitempty,dive,min=0" example:"50,25"`
Skills *[]int32 `json:"skills" validate:"omitempty,dive,min=0" example:"1,5"`
TwOpen *string `json:"tw_open" validate:"omitempty,datetime=2006-01-02T15:04:05" example:"2021-12-31T23:00:00"`
TwClose *string `json:"tw_close" validate:"omitempty,datetime=2006-01-02T15:04:05" example:"2021-12-31T23:59:00"`
SpeedFactor *float64 `json:"speed_factor" validate:"omitempty,gt=0" example:"1.0"`
MaxTasks *int32 `json:"max_tasks" validate:"omitempty,gt=0" example:"20"`
ProjectID *int64 `json:"project_id,string" swaggerignore:"true"`
Data *interface{} `json:"data" swaggertype:"object,string" example:"key1:value1,key2:value2"`
}
func (q *Queries) DBCreateVehicle(ctx context.Context, arg CreateVehicleParams) (Vehicle, error) {
tableName := "vehicles"
sql, args := createResource(tableName, arg)
return_sql := " RETURNING " + util.GetOutputFields(Vehicle{}, tableName)
row := q.db.QueryRow(ctx, sql+return_sql, args...)
return scanVehicleRow(row)
}
func (q *Queries) DBGetVehicle(ctx context.Context, id int64) (Vehicle, error) {
tableName := "vehicles"
additionalQuery := " WHERE id = $1 AND deleted = FALSE LIMIT 1"
sql := "SELECT " + util.GetOutputFields(Vehicle{}, tableName) + " FROM " + tableName + additionalQuery
row := q.db.QueryRow(ctx, sql, id)
return scanVehicleRow(row)
}
func (q *Queries) DBListVehicles(ctx context.Context, projectID int64) ([]Vehicle, error) {
_, err := q.DBGetProject(ctx, projectID)
if err != nil {
return nil, err
}
tableName := "vehicles"
additionalQuery := " WHERE project_id = $1 AND deleted = FALSE ORDER BY created_at"
sql := "SELECT " + util.GetOutputFields(Vehicle{}, tableName) + " FROM " + tableName + additionalQuery
rows, err := q.db.Query(ctx, sql, projectID)
if err != nil {
return nil, err
}
defer rows.Close()
return scanVehicleRows(rows)
}
func (q *Queries) DBUpdateVehicle(ctx context.Context, arg UpdateVehicleParams, vehicle_id int64) (Vehicle, error) {
tableName := "vehicles"
sql, args := updateResource(tableName, arg, vehicle_id)
return_sql := " RETURNING " + util.GetOutputFields(Vehicle{}, tableName)
row := q.db.QueryRow(ctx, sql+return_sql, args...)
return scanVehicleRow(row)
}
func (q *Queries) DBDeleteVehicle(ctx context.Context, id int64) (Vehicle, error) {
tableName := "vehicles"
sql := "UPDATE " + tableName + " SET deleted = TRUE WHERE id = $1"
return_sql := " RETURNING " + util.GetOutputFields(Vehicle{}, tableName)
row := q.db.QueryRow(ctx, sql+return_sql, id)
return scanVehicleRow(row)
}
func scanVehicleRow(row pgx.Row) (Vehicle, error) {
var i Vehicle
var start_id, end_id int64
err := row.Scan(
&i.ID,
&start_id,
&end_id,
&i.Capacity,
&i.Skills,
&i.TwOpen,
&i.TwClose,
&i.SpeedFactor,
&i.MaxTasks,
&i.ProjectID,
&i.Data,
&i.CreatedAt,
&i.UpdatedAt,
)
start_latitude, start_longitude := util.GetCoordinates(start_id)
end_latitude, end_longitude := util.GetCoordinates(end_id)
i.StartLocation = util.LocationParams{
Latitude: &start_latitude,
Longitude: &start_longitude,
}
i.EndLocation = util.LocationParams{
Latitude: &end_latitude,
Longitude: &end_longitude,
}
err = util.HandleDBError(err)
return i, err
}
func scanVehicleRows(rows pgx.Rows) ([]Vehicle, error) {
var i Vehicle
items := []Vehicle{}
var start_id, end_id int64
for rows.Next() {
if err := rows.Scan(
&i.ID,
&start_id,
&end_id,
&i.Capacity,
&i.Skills,
&i.TwOpen,
&i.TwClose,
&i.SpeedFactor,
&i.MaxTasks,
&i.ProjectID,
&i.Data,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
start_latitude, start_longitude := util.GetCoordinates(start_id)
end_latitude, end_longitude := util.GetCoordinates(end_id)
i.StartLocation = util.LocationParams{
Latitude: &start_latitude,
Longitude: &start_longitude,
}
i.EndLocation = util.LocationParams{
Latitude: &end_latitude,
Longitude: &end_longitude,
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}