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

     1  //
     2  // Copyright (C) 2021 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package requests
     7  
     8  import (
     9  	"encoding/json"
    10  
    11  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common"
    12  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos"
    13  	dtoCommon "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos/common"
    14  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/errors"
    15  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models"
    16  )
    17  
    18  // AddIntervalActionRequest defines the Request Content for POST Interval DTO.
    19  type AddIntervalActionRequest struct {
    20  	dtoCommon.BaseRequest `json:",inline"`
    21  	Action                dtos.IntervalAction `json:"action"`
    22  }
    23  
    24  // Validate satisfies the Validator interface
    25  func (request AddIntervalActionRequest) Validate() error {
    26  	err := common.Validate(request)
    27  	if err != nil {
    28  		return errors.NewCommonEdgeXWrapper(err)
    29  	}
    30  	err = request.Action.Address.Validate()
    31  	if err != nil {
    32  		return errors.NewCommonEdgeXWrapper(err)
    33  	}
    34  	return nil
    35  }
    36  
    37  // UnmarshalJSON implements the Unmarshaler interface for the AddIntervalActionRequest type
    38  func (request *AddIntervalActionRequest) UnmarshalJSON(b []byte) error {
    39  	var alias struct {
    40  		dtoCommon.BaseRequest
    41  		Action dtos.IntervalAction
    42  	}
    43  	if err := json.Unmarshal(b, &alias); err != nil {
    44  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err)
    45  	}
    46  
    47  	*request = AddIntervalActionRequest(alias)
    48  
    49  	// validate AddIntervalActionRequest DTO
    50  	if err := request.Validate(); err != nil {
    51  		return errors.NewCommonEdgeXWrapper(err)
    52  	}
    53  	return nil
    54  }
    55  
    56  // AddIntervalActionReqToIntervalActionModels transforms the AddIntervalActionRequest DTO array to the IntervalAction model array
    57  func AddIntervalActionReqToIntervalActionModels(addRequests []AddIntervalActionRequest) (actions []models.IntervalAction) {
    58  	for _, req := range addRequests {
    59  		d := dtos.ToIntervalActionModel(req.Action)
    60  		actions = append(actions, d)
    61  	}
    62  	return actions
    63  }
    64  
    65  // UpdateIntervalActionRequest defines the Request Content for PUT event as pushed DTO.
    66  type UpdateIntervalActionRequest struct {
    67  	dtoCommon.BaseRequest `json:",inline"`
    68  	Action                dtos.UpdateIntervalAction `json:"action"`
    69  }
    70  
    71  // Validate satisfies the Validator interface
    72  func (request UpdateIntervalActionRequest) Validate() error {
    73  	err := common.Validate(request)
    74  	if err != nil {
    75  		return errors.NewCommonEdgeXWrapper(err)
    76  	}
    77  	if request.Action.Address != nil {
    78  		err = request.Action.Address.Validate()
    79  		if err != nil {
    80  			return errors.NewCommonEdgeXWrapper(err)
    81  		}
    82  	}
    83  	return nil
    84  }
    85  
    86  // UnmarshalJSON implements the Unmarshaler interface for the UpdateIntervalActionRequest type
    87  func (request *UpdateIntervalActionRequest) UnmarshalJSON(b []byte) error {
    88  	var alias struct {
    89  		dtoCommon.BaseRequest
    90  		Action dtos.UpdateIntervalAction
    91  	}
    92  	if err := json.Unmarshal(b, &alias); err != nil {
    93  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err)
    94  	}
    95  
    96  	*request = UpdateIntervalActionRequest(alias)
    97  
    98  	// validate UpdateIntervalActionRequest DTO
    99  	if err := request.Validate(); err != nil {
   100  		return err
   101  	}
   102  	return nil
   103  }
   104  
   105  // ReplaceIntervalActionModelFieldsWithDTO replace existing IntervalAction's fields with DTO patch
   106  func ReplaceIntervalActionModelFieldsWithDTO(action *models.IntervalAction, patch dtos.UpdateIntervalAction) {
   107  	if patch.IntervalName != nil {
   108  		action.IntervalName = *patch.IntervalName
   109  	}
   110  	if patch.Address != nil {
   111  		action.Address = dtos.ToAddressModel(*patch.Address)
   112  	}
   113  	if patch.Content != nil {
   114  		action.Content = *patch.Content
   115  	}
   116  	if patch.ContentType != nil {
   117  		action.ContentType = *patch.ContentType
   118  	}
   119  	if patch.AdminState != nil {
   120  		action.AdminState = models.AdminState(*patch.AdminState)
   121  	}
   122  }
   123  
   124  func NewAddIntervalActionRequest(dto dtos.IntervalAction) AddIntervalActionRequest {
   125  	return AddIntervalActionRequest{
   126  		BaseRequest: dtoCommon.NewBaseRequest(),
   127  		Action:      dto,
   128  	}
   129  }
   130  
   131  func NewUpdateIntervalActionRequest(dto dtos.UpdateIntervalAction) UpdateIntervalActionRequest {
   132  	return UpdateIntervalActionRequest{
   133  		BaseRequest: dtoCommon.NewBaseRequest(),
   134  		Action:      dto,
   135  	}
   136  }