summaryrefslogtreecommitdiff
path: root/src/http/drafts.go
blob: aedd8c0a565601a6baf25c4bf625e9b0f383b309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package http

import (
	"net/http"

	"dev.mediocregopher.com/mediocre-blog.git/src/http/apiutil"
)

func (a *api) manageDraftPostsHandler() http.Handler {
	tpl := a.mustParseBasedTpl("draft-posts-manage.html")
	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		a.executeTemplate(rw, r, tpl, nil)
	})
}

func (a *api) postDraftPostHandler() http.Handler {
	return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		var (
			ctx    = r.Context()
			logger = a.params.Logger
		)

		p, err := postFromPostReq(r)
		if err != nil {
			apiutil.BadRequest(ctx, logger, rw, "%w", err)
			return
		}

		if err := a.params.PostDraftStore.Set(p); err != nil {
			apiutil.InternalServerError(
				ctx, logger, rw, "storing post with id %q: %w", p.ID, err,
			)
			return
		}

		a.executeRedirectTpl(
			rw, r, a.urlBuilder.Draft(p.ID).MethodEdit().String(),
		)
	})
}