Exemplo n.º 1
0
func (controller *sitesController) newGet(rw http.ResponseWriter, req *http.Request) (int, error) {
	isAuthenticated, user := getCurrentUser(rw, req, controller.authorizer)
	siteNew := new(viewmodels.SitesEditViewModel)
	siteNew.IsActive = true
	// 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
	}
	// These are strings in the ViewModel.
	siteNew.PingIntervalSeconds = "60"
	siteNew.TimeoutSeconds = "15"
	siteNew.SelectedContacts = []int64{}
	vm := viewmodels.NewSiteViewModel(siteNew, contacts, isAuthenticated, user, make(map[string]string))
	vm.CsrfField = csrf.TemplateField(req)
	return http.StatusOK, controller.newTemplate.Execute(rw, vm)
}
Exemplo n.º 2
0
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)
}