107 lines
2.9 KiB
Go
107 lines
2.9 KiB
Go
package filicop
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
type Point struct {
|
|
X float32 `json:"x"`
|
|
Y float32 `json:"y"`
|
|
}
|
|
|
|
type VehicleLocation struct {
|
|
Id int64 `json:"id"`
|
|
IdVehicle int64 `json:"id_vehicle"`
|
|
Location Point `json:"location"`
|
|
Date time.Time `json:"date"`
|
|
}
|
|
|
|
func (v Vehicle) GetLocations() (locations []VehicleLocation, err error) {
|
|
if rows, errr := DBQuery("SELECT id_location, id_vehicle, location, date FROM vehicles_locations WHERE id_vehicle = ?", v.Id); errr != nil {
|
|
return nil, errr
|
|
} else {
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
var l VehicleLocation
|
|
if err = rows.Scan(&l.Id, &l.IdVehicle, &l.Location, &l.Date); err != nil {
|
|
return
|
|
}
|
|
locations = append(locations, l)
|
|
}
|
|
if err = rows.Err(); err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
func GetVehicleLocation(id int64) (l VehicleLocation, err error) {
|
|
err = DBQueryRow("SELECT id_location, id_vehicle, location, date FROM vehicle_locations WHERE id_location = ?", id).Scan(&l.Id, &l.IdVehicle, &l.Location, &l.Date)
|
|
return
|
|
}
|
|
|
|
func (v Vehicle) GetLocation(id int64) (l VehicleLocation, err error) {
|
|
err = DBQueryRow("SELECT id_location, id_vehicle, location, date FROM vehicle_locations WHERE id_location = ? AND id_vehicle = ?", id, v.Id).Scan(&l.Id, &l.IdVehicle, &l.Location, &l.Date)
|
|
return
|
|
}
|
|
|
|
func (l VehicleLocation) GetVehicle() (Vehicle, error) {
|
|
return GetVehicle(l.IdVehicle)
|
|
}
|
|
|
|
func (l VehicleLocation) SetVehicle(v Vehicle) {
|
|
l.IdVehicle = v.Id
|
|
}
|
|
|
|
func (v Vehicle) NewLocation(point Point) (VehicleLocation, error) {
|
|
if res, err := DBExec("INSERT INTO vehicle_locations (id_vehicle, location) VALUES (?, ?)", v.Id, point); err != nil {
|
|
return VehicleLocation{}, err
|
|
} else if lid, err := res.LastInsertId(); err != nil {
|
|
return VehicleLocation{}, err
|
|
} else {
|
|
return VehicleLocation{lid, v.Id, point, time.Now()}, nil
|
|
}
|
|
}
|
|
|
|
func (l VehicleLocation) Update() (int64, error) {
|
|
if res, err := DBExec("UPDATE vehicle_locations SET id_vehicle = ?, location = ?, date = ? WHERE id_location = ?", l.IdVehicle, l.Location, l.Date, l.Id); err != nil {
|
|
return 0, err
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
return 0, err
|
|
} else {
|
|
return nb, err
|
|
}
|
|
}
|
|
|
|
func (l VehicleLocation) Delete() (int64, error) {
|
|
if res, err := DBExec("DELETE FROM vehicle_locations WHERE id_location = ?", l.Id); err != nil {
|
|
return 0, err
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
return 0, err
|
|
} else {
|
|
return nb, err
|
|
}
|
|
}
|
|
|
|
func ClearVehicleLocations() (int64, error) {
|
|
if res, err := DBExec("DELETE FROM vehicle_locations"); err != nil {
|
|
return 0, err
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
return 0, err
|
|
} else {
|
|
return nb, err
|
|
}
|
|
}
|
|
|
|
func (v Vehicle) ClearVehicleLocations() (int64, error) {
|
|
if res, err := DBExec("DELETE FROM vehicle_locations WHERE id_vehicle = ?", v.Id); err != nil {
|
|
return 0, err
|
|
} else if nb, err := res.RowsAffected(); err != nil {
|
|
return 0, err
|
|
} else {
|
|
return nb, err
|
|
}
|
|
}
|