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

     1  //
     2  // Copyright (C) 2021 IOTech Ltd
     3  //
     4  // SPDX-License-Identifier: Apache-2.0
     5  
     6  package dtos
     7  
     8  import (
     9  	"dev.azure.com/aidainnovazione0090/DeviceManager/_git/go-mod-core-contracts/models"
    10  
    11  	"github.com/google/uuid"
    12  )
    13  
    14  type Notification struct {
    15  	DBTimestamp `json:",inline"`
    16  	Id          string   `json:"id,omitempty" validate:"omitempty,uuid"`
    17  	Category    string   `json:"category,omitempty" validate:"required_without=Labels,omitempty,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
    18  	Labels      []string `json:"labels,omitempty" validate:"required_without=Category,omitempty,gt=0,dive,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
    19  	Content     string   `json:"content" validate:"required,edgex-dto-none-empty-string"`
    20  	ContentType string   `json:"contentType,omitempty"`
    21  	Description string   `json:"description,omitempty"`
    22  	Sender      string   `json:"sender" validate:"required,edgex-dto-none-empty-string,edgex-dto-rfc3986-unreserved-chars"`
    23  	Severity    string   `json:"severity" validate:"required,oneof='MINOR' 'NORMAL' 'CRITICAL'"`
    24  	Status      string   `json:"status,omitempty" validate:"omitempty,oneof='NEW' 'PROCESSED' 'ESCALATED'"`
    25  }
    26  
    27  // NewNotification creates and returns a Notification DTO
    28  func NewNotification(labels []string, category, content, sender, severity string) Notification {
    29  	return Notification{
    30  		Id:       uuid.NewString(),
    31  		Labels:   labels,
    32  		Category: category,
    33  		Content:  content,
    34  		Sender:   sender,
    35  		Severity: severity,
    36  	}
    37  }
    38  
    39  // ToNotificationModel transforms the Notification DTO to the Notification Model
    40  func ToNotificationModel(n Notification) models.Notification {
    41  	var m models.Notification
    42  	m.Id = n.Id
    43  	m.DBTimestamp = models.DBTimestamp(n.DBTimestamp)
    44  	m.Category = n.Category
    45  	m.Labels = n.Labels
    46  	m.Content = n.Content
    47  	m.ContentType = n.ContentType
    48  	m.Description = n.Description
    49  	m.Sender = n.Sender
    50  	m.Severity = models.NotificationSeverity(n.Severity)
    51  	m.Status = models.NotificationStatus(n.Status)
    52  	return m
    53  }
    54  
    55  // ToNotificationModels transforms the Notification DTO array to the Notification model array
    56  func ToNotificationModels(notifications []Notification) []models.Notification {
    57  	models := make([]models.Notification, len(notifications))
    58  	for i, n := range notifications {
    59  		models[i] = ToNotificationModel(n)
    60  	}
    61  	return models
    62  }
    63  
    64  // FromNotificationModelToDTO transforms the Notification Model to the Notification DTO
    65  func FromNotificationModelToDTO(n models.Notification) Notification {
    66  	return Notification{
    67  		DBTimestamp: DBTimestamp(n.DBTimestamp),
    68  		Id:          n.Id,
    69  		Category:    string(n.Category),
    70  		Labels:      n.Labels,
    71  		Content:     n.Content,
    72  		ContentType: n.ContentType,
    73  		Description: n.Description,
    74  		Sender:      n.Sender,
    75  		Severity:    string(n.Severity),
    76  		Status:      string(n.Status),
    77  	}
    78  }
    79  
    80  // FromNotificationModelsToDTOs transforms the Notification model array to the Notification DTO array
    81  func FromNotificationModelsToDTOs(notifications []models.Notification) []Notification {
    82  	dtos := make([]Notification, len(notifications))
    83  	for i, n := range notifications {
    84  		dtos[i] = FromNotificationModelToDTO(n)
    85  	}
    86  	return dtos
    87  }