-
Notifications
You must be signed in to change notification settings - Fork 67
/
notification.go
63 lines (53 loc) · 1.86 KB
/
notification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//
// Copyright (C) 2021 IOTech Ltd
//
// SPDX-License-Identifier: Apache-2.0
package requests
import (
"encoding/json"
"github.com/edgexfoundry/go-mod-core-contracts/v3/common"
"github.com/edgexfoundry/go-mod-core-contracts/v3/dtos"
dtoCommon "github.com/edgexfoundry/go-mod-core-contracts/v3/dtos/common"
"github.com/edgexfoundry/go-mod-core-contracts/v3/errors"
"github.com/edgexfoundry/go-mod-core-contracts/v3/models"
)
// AddNotificationRequest defines the Request Content for POST Notification DTO.
type AddNotificationRequest struct {
dtoCommon.BaseRequest `json:",inline"`
Notification dtos.Notification `json:"notification"`
}
// Validate satisfies the Validator interface
func (request AddNotificationRequest) Validate() error {
err := common.Validate(request)
return err
}
// UnmarshalJSON implements the Unmarshaler interface for the AddNotificationRequest type
func (request *AddNotificationRequest) UnmarshalJSON(b []byte) error {
var alias struct {
dtoCommon.BaseRequest
Notification dtos.Notification
}
if err := json.Unmarshal(b, &alias); err != nil {
return errors.NewCommonEdgeX(errors.KindContractInvalid, "Failed to unmarshal request body as JSON.", err)
}
*request = AddNotificationRequest(alias)
// validate AddNotificationRequest DTO
if err := request.Validate(); err != nil {
return err
}
return nil
}
// AddNotificationReqToNotificationModels transforms the AddNotificationRequest DTO array to the AddNotificationRequest model array
func AddNotificationReqToNotificationModels(reqs []AddNotificationRequest) (n []models.Notification) {
for _, req := range reqs {
d := dtos.ToNotificationModel(req.Notification)
n = append(n, d)
}
return n
}
func NewAddNotificationRequest(dto dtos.Notification) AddNotificationRequest {
return AddNotificationRequest{
BaseRequest: dtoCommon.NewBaseRequest(),
Notification: dto,
}
}