dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/requests/notification.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  // AddNotificationRequest defines the Request Content for POST Notification DTO.
    19  type AddNotificationRequest struct {
    20  	dtoCommon.BaseRequest `json:",inline"`
    21  	Notification          dtos.Notification `json:"notification"`
    22  }
    23  
    24  // Validate satisfies the Validator interface
    25  func (request AddNotificationRequest) Validate() error {
    26  	err := common.Validate(request)
    27  	return err
    28  }
    29  
    30  // UnmarshalJSON implements the Unmarshaler interface for the AddNotificationRequest type
    31  func (request *AddNotificationRequest) UnmarshalJSON(b []byte) error {
    32  	var alias struct {
    33  		dtoCommon.BaseRequest
    34  		Notification dtos.Notification
    35  	}
    36  	if err := json.Unmarshal(b, &alias); err != nil {
    37  		return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err)
    38  	}
    39  
    40  	*request = AddNotificationRequest(alias)
    41  
    42  	// validate AddNotificationRequest DTO
    43  	if err := request.Validate(); err != nil {
    44  		return err
    45  	}
    46  	return nil
    47  }
    48  
    49  // AddNotificationReqToNotificationModels transforms the AddNotificationRequest DTO array to the AddNotificationRequest model array
    50  func AddNotificationReqToNotificationModels(reqs []AddNotificationRequest) (n []models.Notification) {
    51  	for _, req := range reqs {
    52  		d := dtos.ToNotificationModel(req.Notification)
    53  		n = append(n, d)
    54  	}
    55  	return n
    56  }
    57  
    58  func NewAddNotificationRequest(dto dtos.Notification) AddNotificationRequest {
    59  	return AddNotificationRequest{
    60  		BaseRequest:  dtoCommon.NewBaseRequest(),
    61  		Notification: dto,
    62  	}
    63  }