Exemple #1
0
func HandlePageDetail(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	var content struct {
		Page *Page
		User *auth.User
	}
	if u, ok := auth.CurrentUser(ctx); ok {
		content.User = u.User
	}
	page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug"))
	if err == ErrNotFound {
		http.Redirect(w, r, r.URL.Path+"/edit", http.StatusFound)
		return
	}
	if err != nil {
		log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err)
		web.StdHTMLResp(w, http.StatusServiceUnavailable)
		return
	}

	if protectPrivatePage(ctx, w, r, page) {
		return
	}

	content.Page = page
	if web.CheckLastModified(ctx, w, r, page.Updated) {
		return
	}
	render(w, "page-details", content)
}
Exemple #2
0
func HandlePageHistory(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug"))
	if err != nil {
		if err == ErrNotFound {
			web.HTMLErr(w, "Page does not exist", http.StatusNotFound)
		} else {
			log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err)
			web.StdHTMLResp(w, http.StatusServiceUnavailable)
		}
		return
	}

	if protectPrivatePage(ctx, w, r, page) {
		return
	}

	content := struct {
		Page *Page
		User *auth.User
	}{
		Page: page,
	}
	if u, ok := auth.CurrentUser(ctx); ok {
		content.User = u.User
	}
	render(w, "page-history", content)
}
Exemple #3
0
func HandlePageDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	slug := web.Args(ctx).ByName("slug")
	page, err := Store(ctx).GetBySlug(slug)
	if err != nil {
		if err == ErrNotFound {
			web.HTMLErr(w, "Page does not exist", http.StatusNotFound)
		} else {
			log.Printf("cannot get page %q: %s", slug, err)
			web.StdHTMLResp(w, http.StatusServiceUnavailable)
		}
		return
	}

	if protectPrivatePage(ctx, w, r, page) {
		return
	}

	if r.Method == "GET" {
		render(w, "page-delete-confirm", page)
		return
	}

	page.Deleted = true
	if _, err := Store(ctx).Put(*page); err != nil {
		log.Printf("cannot delete page %d: %s", page.PageID, err)
		web.StdHTMLResp(w, http.StatusServiceUnavailable)
	} else {
		http.Redirect(w, r, "/", http.StatusFound)
	}
}
Exemple #4
0
func HandlePageEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug"))

	if err == ErrNotFound {
		page = &Page{
			Slug: web.Args(ctx).ByName("slug"),
		}
		render(w, "page-edit", page)
		return
	}

	if err != nil {
		log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err)
		web.StdHTMLResp(w, http.StatusServiceUnavailable)
		return
	}

	if protectPrivatePage(ctx, w, r, page) {
		return
	}

	render(w, "page-edit", page)
}
Exemple #5
0
func HandlePageSave(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	page, err := Store(ctx).GetBySlug(web.Args(ctx).ByName("slug"))
	if err != nil && err != ErrNotFound {
		log.Printf("cannot get page %q: %s", web.Args(ctx).ByName("slug"), err)
		web.StdHTMLResp(w, http.StatusServiceUnavailable)
		return
	}

	user, _ := auth.CurrentUser(ctx)
	if err == ErrNotFound {
		page = &Page{
			IsPublic: true,
			OwnerID:  user.User.UserID,
			Slug:     web.Args(ctx).ByName("slug"),
		}
	}

	if protectPrivatePage(ctx, w, r, page) {
		return
	}

	content := r.FormValue("content")
	if content == "" {
		web.HTMLErr(w, "content is required", http.StatusBadRequest)
		return
	}

	page.IsPublic = r.FormValue("public") == "on" // checkbox
	page.Content = content
	if p, err := Store(ctx).Put(*page); err != nil {
		log.Printf("cannot update page: %s", err)
		web.StdHTMLResp(w, http.StatusServiceUnavailable)
	} else {
		http.Redirect(w, r, fmt.Sprintf("/p/%s", p.Slug), http.StatusFound)
	}
}