github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/model/notify_history.go (about)

     1  package model
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/evergreen-ci/evergreen/db"
     7  	"github.com/evergreen-ci/evergreen/db/bsonutil"
     8  	"gopkg.in/mgo.v2"
     9  	"gopkg.in/mgo.v2/bson"
    10  )
    11  
    12  const (
    13  	NotifyHistoryCollection = "notify_history"
    14  )
    15  
    16  type NotificationHistory struct {
    17  	Id                    bson.ObjectId `bson:"_id,omitempty"`
    18  	PrevNotificationId    string        `bson:"p_nid"`
    19  	CurrNotificationId    string        `bson:"c_nid"`
    20  	NotificationName      string        `bson:"n_name"`
    21  	NotificationType      string        `bson:"n_type"`
    22  	NotificationTime      time.Time     `bson:"n_time"`
    23  	NotificationProject   string        `bson:"n_branch"`
    24  	NotificationRequester string        `bson:"n_requester"`
    25  }
    26  
    27  var (
    28  	// bson fields for the notification history struct
    29  	NHIdKey     = bsonutil.MustHaveTag(NotificationHistory{}, "Id")
    30  	NHPrevIdKey = bsonutil.MustHaveTag(NotificationHistory{},
    31  		"PrevNotificationId")
    32  	NHCurrIdKey = bsonutil.MustHaveTag(NotificationHistory{},
    33  		"CurrNotificationId")
    34  	NHNameKey = bsonutil.MustHaveTag(NotificationHistory{},
    35  		"NotificationName")
    36  	NHTypeKey = bsonutil.MustHaveTag(NotificationHistory{},
    37  		"NotificationType")
    38  	NHTimeKey = bsonutil.MustHaveTag(NotificationHistory{},
    39  		"NotificationTime")
    40  	NHProjectKey = bsonutil.MustHaveTag(NotificationHistory{},
    41  		"NotificationProject")
    42  	NHRequesterKey = bsonutil.MustHaveTag(NotificationHistory{},
    43  		"NotificationRequester")
    44  )
    45  
    46  func FindNotificationRecord(notificationId, notificationName, notificationType,
    47  	notificationProject, notificationRequester string) (*NotificationHistory,
    48  	error) {
    49  	return FindOneNotification(
    50  		bson.M{
    51  			NHPrevIdKey:    notificationId,
    52  			NHNameKey:      notificationName,
    53  			NHTypeKey:      notificationType,
    54  			NHProjectKey:   notificationProject,
    55  			NHRequesterKey: notificationRequester,
    56  		},
    57  		bson.M{
    58  			NHIdKey:     1,
    59  			NHPrevIdKey: 1,
    60  		},
    61  	)
    62  }
    63  
    64  func InsertNotificationRecord(prevNotification, currNotification,
    65  	notificationName, notificationType, notificationProject,
    66  	notificationRequester string) error {
    67  	nh := &NotificationHistory{
    68  		PrevNotificationId:    prevNotification,
    69  		CurrNotificationId:    currNotification,
    70  		NotificationName:      notificationName,
    71  		NotificationType:      notificationType,
    72  		NotificationTime:      time.Now(),
    73  		NotificationProject:   notificationProject,
    74  		NotificationRequester: notificationRequester,
    75  	}
    76  	return nh.Insert()
    77  }
    78  
    79  func (self *NotificationHistory) Insert() error {
    80  	return db.Insert(NotifyHistoryCollection, self)
    81  }
    82  
    83  func FindOneNotification(query interface{},
    84  	projection interface{}) (*NotificationHistory, error) {
    85  	notificationHistory := &NotificationHistory{}
    86  	err := db.FindOne(
    87  		NotifyHistoryCollection,
    88  		query,
    89  		projection,
    90  		db.NoSort,
    91  		notificationHistory,
    92  	)
    93  	if err == mgo.ErrNotFound {
    94  		return nil, nil
    95  	}
    96  	return notificationHistory, err
    97  }
    98  
    99  func UpdateOneNotification(query interface{}, update interface{}) error {
   100  	return db.Update(
   101  		NotifyHistoryCollection,
   102  		query,
   103  		update,
   104  	)
   105  }