91 lines
2.8 KiB
Go
91 lines
2.8 KiB
Go
package helper
|
|
|
|
import (
|
|
"fmt"
|
|
"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 _, device := range devices {
|
|
// Get address from coordinates
|
|
address, err := geocoder.GetAddressFromCoordinates(device.Latitude, device.Longitude)
|
|
log.Printf("Geocoding for device %s: %.6f, %.6f -> %s", device.DeviceCode, device.Latitude, device.Longitude, address)
|
|
if err != nil {
|
|
address = fmt.Sprintf("Coordinates: %.6f, %.6f", device.Latitude, device.Longitude)
|
|
}
|
|
|
|
response := res.DeviceResponse{
|
|
ID: device.ID,
|
|
DeviceCode: device.DeviceCode,
|
|
DeviceType: string(device.DeviceType),
|
|
Longitude: device.Longitude,
|
|
Latitude: device.Latitude,
|
|
Address: address,
|
|
PortAmount: device.PortAmount,
|
|
Status: string(device.Status),
|
|
Province: device.Province, // Nullable field
|
|
City: device.City, // Nullable field
|
|
District: device.District, // Nullable field
|
|
CreatedAt: device.CreatedAt,
|
|
UpdatedAt: device.UpdatedAt,
|
|
}
|
|
|
|
responses = append(responses, response)
|
|
}
|
|
|
|
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,
|
|
Province: devices.Province,
|
|
City: devices.City,
|
|
District: devices.District,
|
|
PortAmount: devices.PortAmount,
|
|
Status: string(devices.Status),
|
|
CreatedAt: devices.CreatedAt,
|
|
UpdatedAt: devices.UpdatedAt,
|
|
}
|
|
|
|
return deviceResp, nil
|
|
} |