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 }
func (controller *contactsController) newGet(rw http.ResponseWriter, req *http.Request) (int, error) { isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer) contactEdit := new(viewmodels.ContactsEditViewModel) contactEdit.EmailActive = false contactEdit.SmsActive = false sites, err := getAllSites(controller) if err != nil { return http.StatusInternalServerError, err } vm := viewmodels.NewContactViewModel(contactEdit, sites, true, isAuthenticated, user, make(map[string]string)) vm.CsrfField = csrf.TemplateField(req) return http.StatusOK, controller.newTemplate.Execute(rw, vm) }