本文整理汇总了Golang中github.com/turnkey-commerce/go-ping-sites/database.Site.GetSiteContacts方法的典型用法代码示例。如果您正苦于以下问题:Golang Site.GetSiteContacts方法的具体用法?Golang Site.GetSiteContacts怎么用?Golang Site.GetSiteContacts使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/turnkey-commerce/go-ping-sites/database.Site
的用法示例。
在下文中一共展示了Site.GetSiteContacts方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: editGet
func (controller *sitesController) editGet(rw http.ResponseWriter, req *http.Request) (int, error) {
vars := mux.Vars(req)
siteID, err := strconv.ParseInt(vars["siteID"], 10, 64)
if err != nil {
return http.StatusInternalServerError, err
}
// Get the site to edit
site := new(database.Site)
err = site.GetSite(controller.DB, siteID)
if err != nil {
return http.StatusInternalServerError, err
}
// Get all of the contacts to display in the table.
var contacts database.Contacts
err = contacts.GetContacts(controller.DB)
if err != nil {
return http.StatusInternalServerError, err
}
// Also get the site contacts to display in a table.
err = site.GetSiteContacts(controller.DB, siteID)
if err != nil {
return http.StatusInternalServerError, err
}
selectedContacts := []int64{}
for _, contact := range site.Contacts {
selectedContacts = append(selectedContacts, contact.ContactID)
}
isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
siteEdit := new(viewmodels.SitesEditViewModel)
viewmodels.MapSiteDBtoVM(site, siteEdit)
siteEdit.SelectedContacts = selectedContacts
vm := viewmodels.EditSiteViewModel(siteEdit, contacts, isAuthenticated, user, make(map[string]string))
vm.CsrfField = csrf.TemplateField(req)
return http.StatusOK, controller.editTemplate.Execute(rw, vm)
}
示例2: getDetails
func (controller *sitesController) getDetails(rw http.ResponseWriter, req *http.Request) (int, error) {
vars := mux.Vars(req)
siteID, err := strconv.ParseInt(vars["siteID"], 10, 64)
if err != nil {
return http.StatusInternalServerError, err
}
site := new(database.Site)
err = site.GetSite(controller.DB, siteID)
if err != nil {
return http.StatusInternalServerError, err
}
// Also get the contacts to display in a table.
err = site.GetSiteContacts(controller.DB, siteID)
if err != nil {
return http.StatusInternalServerError, err
}
isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
vm := viewmodels.GetSiteDetailsViewModel(site, isAuthenticated, user)
return http.StatusOK, controller.detailsTemplate.Execute(rw, vm)
}
示例3: TestCreateSiteAndContacts
// TestCreateSiteAndContacts tests creating a site and adding new contacts
// in the database and then retrieving it.
func TestCreateSiteAndContacts(t *testing.T) {
db, err := database.InitializeTestDB("")
if err != nil {
t.Fatal("Failed to create database:", err)
}
defer db.Close()
// First create a site to associate with the contacts.
// Note: SiteID is ignored for create but is used in the test comparison
s := database.Site{SiteID: 1, Name: "Test", IsActive: true, URL: "http://www.google.com",
PingIntervalSeconds: 60, TimeoutSeconds: 30, IsSiteUp: true, ContentExpected: "Expected Content",
ContentUnexpected: "Unexpected Content"}
err = s.CreateSite(db)
if err != nil {
t.Fatal("Failed to create new site:", err)
}
// siteID should be 1 on the first create.
if s.SiteID != 1 {
t.Fatal("Expected 1, got ", s.SiteID)
}
//Get the saved site
var site database.Site
err = site.GetSite(db, s.SiteID)
if err != nil {
t.Fatal("Failed to retrieve new site:", err)
}
//Verify the saved site is same as the input.
if !database.CompareSites(site, s) {
t.Error("New site saved not equal to input:\n", site, s)
}
//Update the saved site
sUpdate := database.Site{SiteID: 1, Name: "Test Update", IsActive: false,
URL: "http://www.example.com", PingIntervalSeconds: 30, TimeoutSeconds: 15,
ContentExpected: "Updated Content", ContentUnexpected: "Updated Unexpected",
IsSiteUp: true,
}
site.Name = sUpdate.Name
site.URL = sUpdate.URL
site.IsActive = sUpdate.IsActive
site.PingIntervalSeconds = sUpdate.PingIntervalSeconds
site.TimeoutSeconds = sUpdate.TimeoutSeconds
site.ContentExpected = sUpdate.ContentExpected
site.ContentUnexpected = sUpdate.ContentUnexpected
site.IsSiteUp = sUpdate.IsSiteUp
err = site.UpdateSite(db)
if err != nil {
t.Fatal("Failed to update site:", err)
}
//Get the updated site
var siteUpdated database.Site
err = siteUpdated.GetSite(db, s.SiteID)
if err != nil {
t.Fatal("Failed to retrieve updated site:", err)
}
//Verify the saved site is same as the input.
if !database.CompareSites(siteUpdated, sUpdate) {
t.Error("Updated site saved not equal to input:\n", siteUpdated, sUpdate)
}
// Create first contact - ContactID is for referencing the contact get test
c := database.Contact{Name: "Joe Contact", EmailAddress: "[email protected]", SmsNumber: "5125551212",
SmsActive: false, EmailActive: false, ContactID: 1}
err = c.CreateContact(db)
if err != nil {
t.Fatal("Failed to create new contact:", err)
}
// Associate to the site ID
err = site.AddContactToSite(db, c.ContactID)
if err != nil {
t.Fatal("Failed to associate contact with site:", err)
}
// Create second contact
c2 := database.Contact{Name: "Jill Contact", EmailAddress: "[email protected]", SmsNumber: "5125551213",
SmsActive: false, EmailActive: false}
err = c2.CreateContact(db)
if err != nil {
t.Fatal("Failed to create new site:", err)
}
// Associate the contact to the site
err = site.AddContactToSite(db, c2.ContactID)
if err != nil {
t.Error("Failed to associate contact2 with site:", err)
}
//Get the saved site contacts
err = site.GetSiteContacts(db, site.SiteID)
if err != nil {
t.Error("Failed to retrieve site contacts:", err)
}
// Verify the first contact was Loaded as the last in list by sort order
if !reflect.DeepEqual(c, site.Contacts[1]) {
t.Error("New contact saved not equal to input:\n", site.Contacts[1], c)
//.........这里部分代码省略.........
示例4: TestDeleteContact
// TestDeleteContacts tests creating a site and two contacts
// in the database and then deleting one of the contacts.
func TestDeleteContact(t *testing.T) {
db, err := database.InitializeTestDB("")
if err != nil {
t.Fatal("Failed to create database:", err)
}
defer db.Close()
// First create a site to associate with the contacts.
// Note: SiteID is ignored for create but is used in the test comparison
s := database.Site{SiteID: 1, Name: "Test", IsActive: true, URL: "http://www.google.com", PingIntervalSeconds: 60, TimeoutSeconds: 30, IsSiteUp: true}
err = s.CreateSite(db)
if err != nil {
t.Fatal("Failed to create new site:", err)
}
// Create first contact - ContactID is for referencing the contact get test
c := database.Contact{Name: "Joe Contact", EmailAddress: "[email protected]", SmsNumber: "5125551212",
SmsActive: false, EmailActive: false, ContactID: 1}
err = c.CreateContact(db)
if err != nil {
t.Fatal("Failed to create new contact:", err)
}
// Associate to the site ID
err = s.AddContactToSite(db, c.ContactID)
if err != nil {
t.Fatal("Failed to associate contact with site:", err)
}
// Create second contact
c2 := database.Contact{Name: "Jill Contact", EmailAddress: "[email protected]", SmsNumber: "5125551213",
SmsActive: false, EmailActive: false}
err = c2.CreateContact(db)
if err != nil {
t.Fatal("Failed to create new site:", err)
}
// Associate the contact to the site
err = s.AddContactToSite(db, c2.ContactID)
if err != nil {
t.Error("Failed to associate contact2 with site:", err)
}
err = s.GetSiteContacts(db, s.SiteID)
if err != nil {
t.Error("Failed to retrieve site contacts:", err)
}
if len(s.Contacts) != 2 {
t.Error("There should two contacts before deletion.")
}
// Delete the second contact
err = c2.DeleteContact(db)
if err != nil {
t.Fatal("Failed to delete contact 2:", err)
}
// Verify that it was deleted OK and not associated with the site, and
// that contact1 is still there.
err = s.GetSiteContacts(db, s.SiteID)
if err != nil {
t.Error("Failed to retrieve site contacts:", err)
}
if len(s.Contacts) != 1 {
t.Error("There should only be one contact for the site after deletion.")
}
if !reflect.DeepEqual(c, s.Contacts[0]) {
t.Error("Remaining contact not equal to input:\n", s.Contacts[0], c)
}
// Also verify that the contacts are correct.
var contacts database.Contacts
err = contacts.GetContacts(db)
if err != nil {
t.Fatal("Failed to get all contacts.", err)
}
if len(contacts) != 1 {
t.Error("There should only be one contact in the DB after deletion.")
}
if contacts[0].SiteCount != 1 {
t.Error("Site count should be 1.")
}
}