dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/application.go (about)

     1  package dtos
     2  
     3  import (
     4  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models"
     5  )
     6  
     7  type Application struct {
     8  	DBTimestamp    `json:",inline"`
     9  	Id             string   `json:"id,omitempty" validate:"omitempty"`
    10  	Name           string   `json:"name" validate:"required,edgex-dto-none-empty-string,edgex-dto-alphanumeric-with-symbols"`
    11  	OrganizationId string   `json:"organization_id" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
    12  	Devices        []Device `json:"devices"`
    13  	DevicesCount   int      `json:"devices_count,omitempty"`
    14  	Layer          []Layer  `json:"layer,omitempty"`
    15  	Latitude       float64  `json:"latitude"`
    16  	Longitude      float64  `json:"longitude"`
    17  }
    18  
    19  type Layer struct {
    20  	Id     string `json:"id,omitempty"`
    21  	Name   string `json:"name"`
    22  	Folder string `json:"folder"`
    23  }
    24  
    25  type ApplicationWithLinks struct {
    26  	Application
    27  	Layer        []interface{} `json:"layer"`
    28  	Links        []Link        `json:"links"`
    29  	Devices      []Device      `json:"devices"`
    30  	DevicesCount int           `json:"devices_count,omitempty"`
    31  }
    32  
    33  type PatchApplication struct {
    34  	Id             *string   `json:"id,omitempty" validate:"omitempty"`
    35  	Name           *string   `json:"name" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
    36  	OrganizationId *string   `json:"organization_id" validate:"omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
    37  	Devices        *[]Device `json:"devices" validate:"omitempty"`
    38  	Layer          *[]Layer  `json:"layer,omitempty"`
    39  	Latitude       *float64  `json:"latitude" validate:"omitempty,gte=-90,lte=90"`
    40  	Longitude      *float64  `json:"longitude" validate:"omitempty,gte=-180,lte=180"`
    41  }
    42  
    43  // ToApplicationModel transforms the Application DTO to the Application Model
    44  func ToApplicationModel(dto Application) models.Application {
    45  	var d models.Application
    46  	d.Id = dto.Id
    47  	d.Name = dto.Name
    48  	d.OrganizationId = dto.OrganizationId
    49  	d.Latitude = dto.Latitude
    50  	d.Longitude = dto.Longitude
    51  
    52  	devices := make([]models.Device, len(dto.Devices))
    53  	for i, device := range dto.Devices {
    54  		devices[i] = ToDeviceModel(device)
    55  	}
    56  	d.Devices = devices
    57  
    58  	layers := make([]models.Layer, len(dto.Layer))
    59  	for _, l := range dto.Layer {
    60  		layer := models.Layer{
    61  			Id:     l.Id,
    62  			Name:   l.Name,
    63  			Folder: l.Folder,
    64  		}
    65  		layers = append(layers, layer)
    66  	}
    67  
    68  	d.Layer = layers
    69  
    70  	return d
    71  }
    72  
    73  // FromDeviceModelToDTO transforms the Device Model to the Device DTO
    74  func FromApplicationModelToDTO(d models.Application) Application {
    75  	var dto Application
    76  	dto.DBTimestamp = DBTimestamp(d.DBTimestamp)
    77  	dto.Id = d.Id
    78  	dto.Name = d.Name
    79  	dto.OrganizationId = d.OrganizationId
    80  	dto.Latitude = d.Latitude
    81  	dto.Longitude = d.Longitude
    82  
    83  	devices := make([]Device, len(d.Devices))
    84  	for i, device := range d.Devices {
    85  		devices[i] = FromDeviceModelToDTO(device)
    86  	}
    87  	dto.Devices = devices
    88  	dto.DevicesCount = len(dto.Devices)
    89  
    90  	layers := []Layer{}
    91  	for _, l := range d.Layer {
    92  		layer := Layer{
    93  			Id:     l.Id,
    94  			Name:   l.Name,
    95  			Folder: l.Folder,
    96  		}
    97  		layers = append(layers, layer)
    98  	}
    99  	dto.Layer = layers
   100  
   101  	return dto
   102  }
   103  
   104  type ApplicationDevice struct {
   105  	DBTimestamp   `json:",inline"`
   106  	Id            string  `json:"id"`
   107  	ApplicationId string  `json:"applicationId"`
   108  	DeviceId      string  `json:"deviceId"`
   109  	Device        *Device `json:"device,omitempty"`
   110  }
   111  
   112  func FromApplicationDeviceModelToDTOs(m models.ApplicationDevice) (dto ApplicationDevice) {
   113  	dto.Id = m.Id
   114  	dto.ApplicationId = m.ApplicationId
   115  	dto.DeviceId = m.DeviceId
   116  
   117  	return dto
   118  }