89 lines
2.3 KiB
Go
89 lines
2.3 KiB
Go
package helper
|
|
|
|
import (
|
|
"log"
|
|
"users_management/m/model/dto/res"
|
|
"users_management/m/model/entity"
|
|
"users_management/m/utils/service"
|
|
)
|
|
|
|
func ConvertToDeviceTypeResponse(devices []entity.Device) []res.DeviceTypeResponse {
|
|
var responses []res.DeviceTypeResponse
|
|
for _, devices := range devices {
|
|
deviceResp := res.DeviceTypeResponse{
|
|
ID: devices.ID,
|
|
DeviceType: string(devices.DeviceType),
|
|
DeviceCode: devices.DeviceCode,
|
|
}
|
|
responses = append(responses, deviceResp)
|
|
}
|
|
|
|
return responses
|
|
}
|
|
|
|
|
|
func ConvertToDeviceResponse (devices []entity.Device, geocoder service.GeocodingService) ([]res.DeviceResponse, error) {
|
|
var responses []res.DeviceResponse
|
|
for _, devices := range devices {
|
|
var address string
|
|
|
|
if geocoder != nil {
|
|
generatedAddress, err := geocoder.GetAddressFromCoordinates(devices.Latitude, devices.Longitude)
|
|
if err != nil {
|
|
// Log specific geocoding error
|
|
log.Printf("Geocoding error for device %s: %v", devices.DeviceCode, err)
|
|
} else {
|
|
address = generatedAddress
|
|
}
|
|
} else{
|
|
log.Println("WARNING: Geocoder is nil")
|
|
}
|
|
|
|
deviceResp := res.DeviceResponse{
|
|
ID: devices.ID,
|
|
DeviceCode: devices.DeviceCode,
|
|
DeviceType: string(devices.DeviceType),
|
|
Longitude: devices.Longitude,
|
|
Latitude: devices.Latitude,
|
|
Address: address,
|
|
PortAmount: devices.PortAmount,
|
|
Status: string(devices.Status),
|
|
CreatedAt: devices.CreatedAt,
|
|
UpdatedAt: devices.UpdatedAt,
|
|
}
|
|
responses = append(responses, deviceResp)
|
|
}
|
|
|
|
return responses, nil
|
|
}
|
|
|
|
func ConvertToDeviceResponseId (devices entity.Device, geocoder service.GeocodingService) (res.DeviceResponse, error) {
|
|
var address string
|
|
|
|
if geocoder != nil {
|
|
generatedAddress, err := geocoder.GetAddressFromCoordinates(devices.Latitude, devices.Longitude)
|
|
if err != nil {
|
|
// Log specific geocoding error
|
|
log.Printf("Geocoding error for device %s: %v", devices.DeviceCode, err)
|
|
} else {
|
|
address = generatedAddress
|
|
}
|
|
} else{
|
|
log.Println("WARNING: Geocoder is nil")
|
|
}
|
|
|
|
deviceResp := res.DeviceResponse{
|
|
ID: devices.ID,
|
|
DeviceCode: devices.DeviceCode,
|
|
DeviceType: string(devices.DeviceType),
|
|
Longitude: devices.Longitude,
|
|
Latitude: devices.Latitude,
|
|
Address: address,
|
|
PortAmount: devices.PortAmount,
|
|
Status: string(devices.Status),
|
|
CreatedAt: devices.CreatedAt,
|
|
UpdatedAt: devices.UpdatedAt,
|
|
}
|
|
|
|
return deviceResp, nil
|
|
} |