dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts@v1.0.2/dtos/requests/device_model.go (about) 1 package requests 2 3 import ( 4 "encoding/json" 5 6 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/common" 7 "dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/dtos" 8 dtoCommon "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 13 /* 14 Device Model CRUD Requests 15 */ 16 17 type AddDeviceModelRequest struct { 18 dtoCommon.BaseRequest `json:",inline"` 19 DeviceModel dtos.DeviceModel `json:"deviceModel"` 20 } 21 22 func (dm AddDeviceModelRequest) Validate() error { 23 return common.Validate(dm) 24 } 25 26 // UnmarshalJSON implements the Unmarshaler interface for the AddDeviceModelRequest type 27 func (dm *AddDeviceModelRequest) UnmarshalJSON(b []byte) error { 28 var alias struct { 29 dtoCommon.BaseRequest 30 DeviceModel dtos.DeviceModel 31 } 32 if err := json.Unmarshal(b, &alias); err != nil { 33 return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) 34 } 35 36 *dm = AddDeviceModelRequest(alias) 37 38 // validate AddDeviceRequest DTO 39 if err := dm.Validate(); err != nil { 40 return err 41 } 42 return nil 43 } 44 45 func AddDeviceModelReqToModel(addRequest []AddDeviceModelRequest) (deviceModels []models.DeviceModel) { 46 for _, req := range addRequest { 47 dm := dtos.DeviceModelToModel(req.DeviceModel) 48 deviceModels = append(deviceModels, dm) 49 } 50 return deviceModels 51 } 52 53 type UpdateDeviceModelRequest struct { 54 dtoCommon.BaseRequest `json:",inline"` 55 DeviceModel dtos.UpdateDeviceModel `json:"deviceModel"` 56 } 57 58 func (dm UpdateDeviceModelRequest) Validate() error { 59 return common.Validate(dm) 60 } 61 62 // UnmarshalJSON implements the Unmarshaler interface for the AddDeviceModelRequest type 63 func (dm *UpdateDeviceModelRequest) UnmarshalJSON(b []byte) error { 64 var alias struct { 65 dtoCommon.BaseRequest 66 DeviceModel dtos.UpdateDeviceModel 67 } 68 if err := json.Unmarshal(b, &alias); err != nil { 69 return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err) 70 } 71 72 *dm = UpdateDeviceModelRequest(alias) 73 74 // validate AddDeviceRequest DTO 75 if err := dm.Validate(); err != nil { 76 return err 77 } 78 return nil 79 } 80 81 func DeviceModelReplaceModelFieldsWithDTO(deviceModel *models.DeviceModel, patch dtos.UpdateDeviceModel) { 82 if patch.Name != nil { 83 deviceModel.Name = *patch.Name 84 } 85 if patch.ManufacturerName != nil { 86 deviceModel.ManufacturerName = *patch.ManufacturerName 87 } 88 if patch.DeviceProfileName != nil { 89 deviceModel.DeviceProfileName = *patch.DeviceProfileName 90 } 91 if patch.Description != nil { 92 deviceModel.Description = *patch.Description 93 } 94 }