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

     1  package requests
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
     8  	commonDtos "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
     9  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    10  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models"
    11  
    12  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos"
    13  )
    14  
    15  // AddDeviceRequest defines the Request Content for POST Device DTO.
    16  // This object and its properties correspond to the AddDeviceRequest object in the APIv2 specification:
    17  // https://app.swaggerhub.com/apis-docs/EdgeXFoundry1/core-metadata/2.1.0#/AddDeviceRequest
    18  type AddApplicationRequest struct {
    19  	commonDtos.BaseRequest `json:",inline"`
    20  	Application            dtos.Application `json:"application"`
    21  }
    22  
    23  // AddDeviceReqToDeviceModels transforms the AddDeviceRequest DTO array to the Device model array
    24  func AddApplicationReqToApplicationModels(addRequests AddApplicationRequest) (Application models.Application) {
    25  	Application = dtos.ToApplicationModel(addRequests.Application)
    26  	return Application
    27  }
    28  
    29  // Validate satisfies the Validator interface
    30  func (d AddApplicationRequest) Validate() error {
    31  	err := common.Validate(d)
    32  	return err
    33  }
    34  
    35  // UnmarshalJSON implements the Unmarshaler interface for the AddDeviceRequest type
    36  func (d *AddApplicationRequest) UnmarshalJSON(b []byte) error {
    37  	var alias struct {
    38  		commonDtos.BaseRequest
    39  		Application dtos.Application
    40  	}
    41  	if err := json.Unmarshal(b, &alias); err != nil {
    42  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err)
    43  	}
    44  
    45  	*d = AddApplicationRequest(alias)
    46  
    47  	// validate AddDeviceRequest DTO
    48  	if err := d.Validate(); err != nil {
    49  		return err
    50  	}
    51  	return nil
    52  }
    53  
    54  type UpdateApplicationRequest struct {
    55  	commonDtos.BaseRequest `json:",inline"`
    56  	Application            dtos.PatchApplication `json:"application"`
    57  }
    58  
    59  // Validate satisfies the Validator interface
    60  func (d UpdateApplicationRequest) Validate() error {
    61  	err := common.Validate(d)
    62  	return err
    63  }
    64  
    65  // UnmarshalJSON implements the Unmarshaler interface for the AddDeviceRequest type
    66  func (d *UpdateApplicationRequest) UnmarshalJSON(b []byte) error {
    67  	var alias struct {
    68  		commonDtos.BaseRequest
    69  		Application dtos.PatchApplication
    70  	}
    71  	if err := json.Unmarshal(b, &alias); err != nil {
    72  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err)
    73  	}
    74  
    75  	*d = UpdateApplicationRequest(alias)
    76  
    77  	// validate AddDeviceRequest DTO
    78  	if err := d.Validate(); err != nil {
    79  		return err
    80  	}
    81  	return nil
    82  }
    83  
    84  func ReplaceApplicationModelFieldsWithDTO(application *models.Application, patch dtos.PatchApplication) errors.EdgeX {
    85  
    86  	validateErr := common.Validate(patch)
    87  	if validateErr != nil {
    88  		return errors.NewCommonEdgeX(errors.KindContractInvalid, fmt.Sprintln("Validate patch application error"), validateErr)
    89  	}
    90  	if patch.Id != nil {
    91  		application.Id = *patch.Id
    92  	}
    93  	if patch.Name != nil {
    94  		application.Name = *patch.Name
    95  	}
    96  	if patch.Latitude != nil {
    97  		application.Latitude = *patch.Latitude
    98  	}
    99  	if patch.Longitude != nil {
   100  		application.Longitude = *patch.Longitude
   101  	}
   102  	if patch.Layer != nil {
   103  		layers := make([]models.Layer, len(*patch.Layer))
   104  		for i, layer := range *patch.Layer {
   105  			layers[i] = models.Layer{
   106  				Id:     layer.Id,
   107  				Name:   layer.Name,
   108  				Folder: layer.Folder,
   109  			}
   110  		}
   111  		application.Layer = layers
   112  	}
   113  	if patch.Devices != nil {
   114  		devices := make([]models.Device, len(*patch.Devices))
   115  		for i, device := range *patch.Devices {
   116  			devices[i] = dtos.ToDeviceModel(device)
   117  		}
   118  		application.Devices = devices
   119  	}
   120  
   121  	return nil
   122  }
   123  
   124  type AddDevicesToApplication struct {
   125  	commonDtos.BaseRequest `json:",inline"`
   126  	Devices                []string `json:"devices"`
   127  }
   128  
   129  type DeleteDevicesToApplication struct {
   130  	commonDtos.BaseRequest `json:",inline"`
   131  	Devices                []string `json:"devices"`
   132  }