Пример #1
0
func wikiPage(w http.ResponseWriter, r *http.Request) {
	// get the page name in the URL
	vars := mux.Vars(r)
	pageName := vars["page"]

	if r.Method == "GET" {
		// fetch the page
		// if the page does not exist redirect to the new page form
		if page, err := models.GetPage(r, pageName); err != nil {
			renderNewPageForm(w, nil)
		} else {
			renderPageView(w, *page)
		}
	}
	if r.Method == "POST" {
		content := r.FormValue("content")

		err := models.AddPage(r, pageName, content)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// redirect to the wiki front page
		http.Redirect(w, r, "/wiki", http.StatusFound)
		return
	}
}
Пример #2
0
func wikiEdit(w http.ResponseWriter, r *http.Request) {
	// get the page name in the URL
	vars := mux.Vars(r)
	pageName := vars["page"]

	if r.Method == "GET" {
		// fetch the page only if you are already looged in
		if currentUser != nil {
			// if the page does not exist redirect to new page form
			if page, err := models.GetPage(r, pageName); err != nil {
				// redirect to the wiki page
				http.Redirect(w, r, "/wiki/"+pageName, http.StatusFound)
				return
			} else {
				renderNewPageForm(w, page.Content)
			}
		} else {
			// redirect to the login page
			http.Redirect(w, r, "/wiki/login", http.StatusFound)
			return
		}
	}
	if r.Method == "POST" {
		content := r.FormValue("content")
		// if the page does not exist redirect to the new page form
		if page, err := models.GetPage(r, pageName); err != nil {
			renderNewPageForm(w, nil)
		} else {
			// update page
			models.UpdatePage(r, *page, pageName, content)

			// redirect to the wiki page
			http.Redirect(w, r, "/wiki/"+pageName, http.StatusFound)
			return
		}
	}
}