aboutsummaryrefslogtreecommitdiff
path: root/src/post/routes.go
blob: 09457d3afdae38f8812d5ef96386290dcd020ad8 (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package post

import (
	"fmt"
	"log"
	"net/http"

	"github.com/go-chi/chi/v5"
	"github.com/volatiletech/sqlboiler/v4/boil"
	"gitlab.com/alexkavon/newsstand/src/models"
	"gitlab.com/alexkavon/newsstand/src/server"
	"gitlab.com/alexkavon/newsstand/src/sessions"
)

var Routes = server.Routes{
	server.Route{
		Name:        "Create",
		Method:      "GET",
		Path:        "/p/create",
		HandlerFunc: Create,
		Middlewares: server.NewMiddlewares(sessions.AuthSession),
	},
	server.Route{
		Name:        "Store",
		Method:      "POST",
		Path:        "/p",
		HandlerFunc: Store,
		Middlewares: server.NewMiddlewares(sessions.AuthSession),
	},
	server.Route{
		Name:        "Get",
		Method:      "GET",
		Path:        "/p/{:id}",
		HandlerFunc: Get,
	},
}

func Create(s *server.Server) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		s.Ui.Render(w, r, "post/create", nil)
	}
}

func Store(s *server.Server) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		var post models.Post
		post.Title = r.PostFormValue("title")
		post.Url = r.PostFormValue("url")
		post.Description = r.PostFormValue("description")

		// validate post
		// process post, look for spamminess, bad url
		// match post title with url title if provided
		// check title for tags: Ask NY, subway, crime, culture
		err := post.Insert(r.Context(), s.Db.ToSqlDb(), boil.Infer())
		if err != nil {
			log.Fatal("Insert Error", err)
		}

		// increment user points maybe
		// redirect to new post
		http.Redirect(w, r, fmt.Sprintf("p/%d", post.ID), http.StatusSeeOther)
	}
}

func Get(s *server.Server) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		post := models.FindPost(r.Context(), s.Db.ToSqlDb(), chi.URLParam(r, "id"))
	}
}