本文整理汇总了Golang中github.com/turnkey-commerce/go-ping-sites/database.Contact类的典型用法代码示例。如果您正苦于以下问题:Golang Contact类的具体用法?Golang Contact怎么用?Golang Contact使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Contact类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: editGet
func (controller *contactsController) editGet(rw http.ResponseWriter, req *http.Request) (int, error) {
vars := mux.Vars(req)
contactID, err := strconv.ParseInt(vars["contactID"], 10, 64)
if err != nil {
return http.StatusInternalServerError, err
}
// Get the contact to edit
contact := new(database.Contact)
err = contact.GetContact(controller.DB, contactID)
if err != nil {
return http.StatusInternalServerError, err
}
isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
contactEdit := new(viewmodels.ContactsEditViewModel)
contactEdit.Name = contact.Name
contactEdit.ContactID = contact.ContactID
contactEdit.EmailAddress = contact.EmailAddress
contactEdit.SmsNumber = contact.SmsNumber
contactEdit.EmailActive = contact.EmailActive
contactEdit.SmsActive = contact.SmsActive
contactEdit.SelectedSites, err = getContactSiteIDs(controller, contact)
if err != nil {
return http.StatusInternalServerError, err
}
sites, errGet := getAllSites(controller)
if errGet != nil {
return http.StatusInternalServerError, errGet
}
vm := viewmodels.EditContactViewModel(contactEdit, sites, isAuthenticated, user, make(map[string]string))
vm.CsrfField = csrf.TemplateField(req)
return http.StatusOK, controller.editTemplate.Execute(rw, vm)
}
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:34,代码来源:contacts.go
示例2: mapContacts
func mapContacts(contact *database.Contact, formContact *viewmodels.ContactsEditViewModel) {
contact.Name = formContact.Name
contact.EmailAddress = formContact.EmailAddress
contact.EmailActive = formContact.EmailActive
contact.SmsNumber = formContact.SmsNumber
contact.SmsActive = formContact.SmsActive
}
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:7,代码来源:contacts.go
示例3: getContactSiteIDs
func getContactSiteIDs(controller *contactsController, contact *database.Contact) ([]int64, error) {
// Get the site ID's for a given contact
siteIDs := []int64{}
err := contact.GetContactSites(controller.DB)
if err != nil {
return nil, err
}
for _, site := range contact.Sites {
siteIDs = append(siteIDs, site.SiteID)
}
return siteIDs, nil
}
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:12,代码来源:contacts.go
示例4: newPost
func (controller *contactsController) newPost(rw http.ResponseWriter, req *http.Request) (int, error) {
err := req.ParseForm()
if err != nil {
return http.StatusInternalServerError, err
}
decoder := schema.NewDecoder()
// Ignore unknown keys to prevent errors from the CSRF token.
decoder.IgnoreUnknownKeys(true)
formContact := new(viewmodels.ContactsEditViewModel)
err = decoder.Decode(formContact, req.PostForm)
if err != nil {
return http.StatusInternalServerError, err
}
valErrors := validateContactForm(formContact)
if len(valErrors) > 0 {
isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
sites, errGet := getAllSites(controller)
if errGet != nil {
return http.StatusInternalServerError, err
}
vm := viewmodels.NewContactViewModel(formContact, sites, false,
isAuthenticated, user, valErrors)
vm.CsrfField = csrf.TemplateField(req)
return http.StatusOK, controller.newTemplate.Execute(rw, vm)
}
contact := database.Contact{}
mapContacts(&contact, formContact)
err = contact.CreateContact(controller.DB)
if err != nil {
return http.StatusInternalServerError, err
}
//Add contact to any selected sites
for _, siteSelID := range formContact.SelectedSites {
err = addContactToSite(controller, contact.ContactID, siteSelID)
if err != nil {
return http.StatusInternalServerError, err
}
}
// Refresh the pinger with the changes.
// TODO: Check whether this contact has been added to any site first.
err = controller.pinger.UpdateSiteSettings()
if err != nil {
return http.StatusInternalServerError, err
}
http.Redirect(rw, req, "/settings/contacts", http.StatusSeeOther)
return http.StatusSeeOther, nil
}
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:53,代码来源:contacts.go
示例5: deletePost
func (controller *contactsController) deletePost(rw http.ResponseWriter, req *http.Request) (int, error) {
err := req.ParseForm()
if err != nil {
return http.StatusInternalServerError, err
}
decoder := schema.NewDecoder()
// Ignore unknown keys to prevent errors from the CSRF token.
decoder.IgnoreUnknownKeys(true)
formContact := new(viewmodels.ContactsEditViewModel)
err = decoder.Decode(formContact, req.PostForm)
if err != nil {
return http.StatusInternalServerError, err
}
valErrors := validateContactForm(formContact)
if len(valErrors) > 0 {
isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
var noSites = []database.Site{}
vm := viewmodels.EditContactViewModel(formContact, noSites, isAuthenticated,
user, valErrors)
vm.CsrfField = csrf.TemplateField(req)
return http.StatusOK, controller.deleteTemplate.Execute(rw, vm)
}
// Get the contact to delete
contact := new(database.Contact)
err = contact.GetContact(controller.DB, formContact.ContactID)
if err != nil {
return http.StatusInternalServerError, err
}
mapContacts(contact, formContact)
err = contact.DeleteContact(controller.DB)
if err != nil {
return http.StatusInternalServerError, err
}
// Refresh the pinger with the changes.
// TODO: Check whether this contact is associated with any active site first.
err = controller.pinger.UpdateSiteSettings()
if err != nil {
return http.StatusInternalServerError, err
}
http.Redirect(rw, req, "/settings/contacts", http.StatusSeeOther)
return http.StatusSeeOther, nil
}
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:48,代码来源:contacts.go
示例6: TestCreateAndGetUnattachedContacts
// TestCreateAndGetUnattachedContacts tests the creation of contacts not associated with a site.
func TestCreateAndGetUnattachedContacts(t *testing.T) {
db, err := database.InitializeTestDB("")
if err != nil {
t.Fatal("Failed to create database:", err)
}
defer db.Close()
// Create first contact
c := database.Contact{Name: "Joe Contact", EmailAddress: "[email protected]", SmsNumber: "5125551212",
SmsActive: false, EmailActive: false, SiteCount: 0}
err = c.CreateContact(db)
if err != nil {
t.Fatal("Failed to create new contact:", err)
}
// Create second contact
c2 := database.Contact{Name: "Jack Contact", EmailAddress: "[email protected]", SmsNumber: "5125551213",
SmsActive: false, EmailActive: false, SiteCount: 0}
err = c2.CreateContact(db)
if err != nil {
t.Fatal("Failed to create new contact:", err)
}
// Create third contact with name conflict.
c3 := database.Contact{Name: "Jack Contact", EmailAddress: "[email protected]", SmsNumber: "5125551213",
SmsActive: false, EmailActive: false}
err = c3.CreateContact(db)
if err == nil {
t.Fatal("Conflicting contact should throw error.")
}
var contacts database.Contacts
err = contacts.GetContacts(db)
if err != nil {
t.Fatal("Failed to get all contacts.", err)
}
// Verify the first contact was Loaded as the last in list by sort order
if !reflect.DeepEqual(c, contacts[1]) {
t.Fatal("New contact saved not equal to input:\n", contacts[1], c)
}
// Verify the second contact was Loaded as the first in list by sort order
if !reflect.DeepEqual(c2, contacts[0]) {
t.Fatal("New contact saved not equal to input:\n", contacts[0], c2)
}
}
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:47,代码来源:database_test.go
示例7: deleteGet
func (controller *contactsController) deleteGet(rw http.ResponseWriter, req *http.Request) (int, error) {
vars := mux.Vars(req)
contactID, err := strconv.ParseInt(vars["contactID"], 10, 64)
if err != nil {
return http.StatusInternalServerError, err
}
// Get the contact to edit
contact := new(database.Contact)
err = contact.GetContact(controller.DB, contactID)
if err != nil {
return http.StatusInternalServerError, err
}
isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
contactDelete := new(viewmodels.ContactsEditViewModel)
contactDelete.Name = contact.Name
contactDelete.ContactID = contact.ContactID
contactDelete.EmailAddress = contact.EmailAddress
var noSites = []database.Site{}
vm := viewmodels.EditContactViewModel(contactDelete, noSites, isAuthenticated,
user, make(map[string]string))
vm.CsrfField = csrf.TemplateField(req)
return http.StatusOK, controller.deleteTemplate.Execute(rw, vm)
}
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:23,代码来源:contacts.go
示例8: editPost
func (controller *contactsController) editPost(rw http.ResponseWriter, req *http.Request) (int, error) {
err := req.ParseForm()
if err != nil {
return http.StatusInternalServerError, err
}
decoder := schema.NewDecoder()
// Ignore unknown keys to prevent errors from the CSRF token.
decoder.IgnoreUnknownKeys(true)
formContact := new(viewmodels.ContactsEditViewModel)
err = decoder.Decode(formContact, req.PostForm)
if err != nil {
return http.StatusInternalServerError, err
}
valErrors := validateContactForm(formContact)
if len(valErrors) > 0 {
isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
sites, errGet := getAllSites(controller)
if errGet != nil {
return http.StatusInternalServerError, err
}
vm := viewmodels.EditContactViewModel(formContact, sites, isAuthenticated,
user, valErrors)
vm.CsrfField = csrf.TemplateField(req)
return http.StatusOK, controller.editTemplate.Execute(rw, vm)
}
// Get the contact to update
contact := new(database.Contact)
err = contact.GetContact(controller.DB, formContact.ContactID)
if err != nil {
return http.StatusInternalServerError, err
}
mapContacts(contact, formContact)
err = contact.UpdateContact(controller.DB)
if err != nil {
return http.StatusInternalServerError, err
}
contactSiteIDS, getErr := getContactSiteIDs(controller, contact)
if getErr != nil {
return http.StatusInternalServerError, getErr
}
//Loop selected ones first and if it's not already in the site then add it.
for _, siteSelID := range formContact.SelectedSites {
if !int64InSlice(int64(siteSelID), contactSiteIDS) {
err = addContactToSite(controller, contact.ContactID, siteSelID)
if err != nil {
return http.StatusInternalServerError, err
}
}
}
// Loop existing contact sites and if it's not in the selected items then remove it.
for _, contactSiteID := range contactSiteIDS {
if !int64InSlice(int64(contactSiteID), formContact.SelectedSites) {
err = removeContactFromSite(controller, contact.ContactID, contactSiteID)
if err != nil {
return http.StatusInternalServerError, err
}
}
}
// Refresh the pinger with the changes.
// TODO: Check whether this contact is associated with any active site first.
err = controller.pinger.UpdateSiteSettings()
if err != nil {
return http.StatusInternalServerError, err
}
http.Redirect(rw, req, "/settings/contacts", http.StatusSeeOther)
return http.StatusSeeOther, nil
}
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:75,代码来源:contacts.go
示例9: TestCreateAndGetMultipleSites
// TestCreateAndGetMultipleSites tests creating more than one active sites
// with contacts in the database and then retrieving them.
func TestCreateAndGetMultipleSites(t *testing.T) {
var err error
db, err := database.InitializeTestDB("")
if err != nil {
t.Fatal("Failed to create database:", err)
}
defer db.Close()
// Create the first site.
s1 := database.Site{Name: "Test", IsActive: true, URL: "http://www.google.com",
PingIntervalSeconds: 60, TimeoutSeconds: 30, ContentExpected: "Expected 1",
ContentUnexpected: "Unexpected 1"}
err = s1.CreateSite(db)
if err != nil {
t.Fatal("Failed to create first site:", err)
}
// Create the second site.
s2 := database.Site{Name: "Test 2", IsActive: true, URL: "http://www.test.com",
PingIntervalSeconds: 60, TimeoutSeconds: 30,
LastStatusChange: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
LastPing: time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC),
ContentExpected: "Expected 2", ContentUnexpected: "Unexpected 2"}
err = s2.CreateSite(db)
if err != nil {
t.Fatal("Failed to create second site:", err)
}
// Create a third site that is marked inactive.
s3 := database.Site{Name: "Test 3", IsActive: false, URL: "http://www.test3.com",
PingIntervalSeconds: 60, TimeoutSeconds: 30, ContentExpected: "Expected 3",
ContentUnexpected: "Unexpected 3"}
err = s3.CreateSite(db)
if err != nil {
t.Fatal("Failed to create third site:", err)
}
// Create first contact
c1 := database.Contact{Name: "Joe Contact", EmailAddress: "[email protected]", SmsNumber: "5125551212",
SmsActive: false, EmailActive: false}
err = c1.CreateContact(db)
if err != nil {
t.Fatal("Failed to create new contact:", err)
}
// Associate to the first and second site ID
err = s1.AddContactToSite(db, c1.ContactID)
if err != nil {
t.Fatal("Failed to associate contact 1 with first site:", err)
}
err = s2.AddContactToSite(db, c1.ContactID)
if err != nil {
t.Fatal("Failed to associate contact 1 with second site:", err)
}
// Create second contact
c2 := database.Contact{Name: "Jack Contact", EmailAddress: "[email protected]", SmsNumber: "5125551213",
SmsActive: false, EmailActive: false}
err = c2.CreateContact(db)
if err != nil {
t.Fatal("Failed to create new contact:", err)
}
// Associate only to the first site
err = s1.AddContactToSite(db, c2.ContactID)
if err != nil {
t.Fatal("Failed to associate contact 1 with first site:", err)
}
var sites database.Sites
// Get active sites with contacts
err = sites.GetSites(db, true, true)
if err != nil {
t.Fatal("Failed to get all the sites.", err)
}
// Verify that there are only two active sites.
if len(sites) != 2 {
t.Fatal("There should only be two active sites loaded.")
}
// Verify the first site was Loaded with proper attributes.
if !database.CompareSites(s1, sites[0]) {
t.Fatal("First saved site not equal to input:\n", sites[0], s1)
}
// Verify the second site was Loaded with proper attributes.
if !database.CompareSites(s2, sites[1]) {
t.Fatal("Second saved site not equal to input:\n", sites[1], s2)
}
// Verify the first contact was Loaded with proper attributes and sorted last.
if !reflect.DeepEqual(c1, sites[0].Contacts[1]) {
t.Error("Second saved contact not equal to input:\n", sites[0].Contacts[1], c1)
}
// Verify the second contact was loaded with the proper attributes and sorted first.
if !reflect.DeepEqual(c2, sites[0].Contacts[0]) {
t.Error("First saved contact not equal to input:\n", sites[0].Contacts[0], c2)
}
// Verify the first contact was loaded to the second site.
//.........这里部分代码省略.........
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:101,代码来源:database_test.go
示例10: 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)
//.........这里部分代码省略.........
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:101,代码来源:database_test.go
示例11: 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.")
}
}
开发者ID:turnkey-commerce,项目名称:go-ping-sites,代码行数:86,代码来源:database_test.go
注:本文中的github.com/turnkey-commerce/go-ping-sites/database.Contact类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论