108 lines
3.5 KiB
Go
108 lines
3.5 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 _, device := range devices {
|
|
address := "Address not found"
|
|
if geocoder != nil {
|
|
if addr, err := geocoder.GetAddressFromCoordinates(device.Latitude, device.Longitude); err == nil {
|
|
address = addr
|
|
}
|
|
}
|
|
|
|
// Get all image URLs using the entity method
|
|
allImageURLs := device.GetAllImageURLs()
|
|
|
|
// Handle empty slice vs nil for JSON response
|
|
if len(allImageURLs) == 0 {
|
|
allImageURLs = []string{} // Return empty array instead of nil
|
|
}
|
|
|
|
response := res.DeviceResponse{
|
|
ID: device.ID,
|
|
DeviceCode: device.DeviceCode,
|
|
DeviceType: string(device.DeviceType),
|
|
Longitude: device.Longitude,
|
|
Latitude: device.Latitude,
|
|
PortAmount: device.PortAmount,
|
|
Status: string(device.Status),
|
|
Address: address,
|
|
Province: device.Province,
|
|
City: device.City,
|
|
District: device.District,
|
|
ImageURL: device.ImageURL, // Primary image (can be null)
|
|
ImageURLs: allImageURLs, // All images (empty array if none)
|
|
CreatedAt: device.CreatedAt,
|
|
UpdatedAt: device.UpdatedAt,
|
|
}
|
|
|
|
responses = append(responses, response)
|
|
}
|
|
|
|
return responses, nil
|
|
}
|
|
func ConvertToDeviceResponseId(device entity.Device, geocoder service.GeocodingService) (res.DeviceResponse, error) {
|
|
address := "Address not found"
|
|
|
|
if geocoder != nil {
|
|
if addr, err := geocoder.GetAddressFromCoordinates(device.Latitude, device.Longitude); err == nil {
|
|
address = addr
|
|
} else {
|
|
log.Printf("Geocoding error for device %s: %v", device.DeviceCode, err)
|
|
}
|
|
} else {
|
|
log.Println("WARNING: Geocoder is nil")
|
|
}
|
|
|
|
// Get all image URLs using the entity method
|
|
allImageURLs := device.GetAllImageURLs()
|
|
|
|
// Handle empty slice vs nil for JSON response
|
|
if len(allImageURLs) == 0 {
|
|
allImageURLs = []string{} // Return empty array instead of nil
|
|
}
|
|
|
|
deviceResp := res.DeviceResponse{
|
|
ID: device.ID,
|
|
DeviceCode: device.DeviceCode,
|
|
DeviceType: string(device.DeviceType),
|
|
Longitude: device.Longitude,
|
|
Latitude: device.Latitude,
|
|
Address: address,
|
|
Province: device.Province,
|
|
City: device.City,
|
|
District: device.District,
|
|
PortAmount: device.PortAmount,
|
|
Status: string(device.Status),
|
|
ImageURL: device.ImageURL, // Primary image (can be null)
|
|
ImageURLs: allImageURLs, // All images (empty array if none)
|
|
CreatedAt: device.CreatedAt,
|
|
UpdatedAt: device.UpdatedAt,
|
|
}
|
|
|
|
return deviceResp, nil
|
|
} |