summaryrefslogtreecommitdiff
path: root/src/blog/routes.go
blob: c16f0466b0e5a9094d4a10185785c3bfeab17a75 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * vidhublog: Vidhu Kant's Blog
 * Copyright (C) 2022  Vidhu Kant Sharma <vidhukant@protonmail.ch>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package blog

import (
	"html/template"
	"net/http"
	"strconv"
	"strings"

	"github.com/MikunoNaka/vidhukant.xyz/db"
	"github.com/gin-gonic/gin"
)

type tagsSelection struct {
  TagID      int
  TagName    string
  IsSelected bool
}

// database connection
var base *dbhandler
func init() {
  connection := db.ConnectDB()
  base = newHandler(connection)
}

// receives tags through form POST and redirects to /posts?tags=....
func filterByTagInput(ctx *gin.Context) {
  var tagInput struct {
    Tags []int `form:"tags"`
  }
  ctx.ShouldBind(&tagInput)
  tags := tagInput.Tags

  var tagsStringified string
  for i, j := range tags {
    tagsStringified = tagsStringified + strconv.Itoa(j)
    if i != len(tags) - 1 {
      tagsStringified = tagsStringified + ","
    }
  }

  ctx.Redirect(http.StatusMovedPermanently, "/posts?tags=" + tagsStringified)
}

func getPosts(ctx *gin.Context) {
  limitOptions := []int{10, 20, 30}
  limit := 10
  // if limit is in url query use that
  if l := ctx.Query("limit"); l != "" {
    limit, _ = strconv.Atoi(l)
  }

  pageNum := 1
  // if pageNum is in url query use that
  if p := ctx.Query("page"); p != "" {
    pageNum, _ = strconv.Atoi(p)
    // pageNum can't be less than 1
    if pageNum < 1 { pageNum = 1 }
  }

  // if firstPost is in url query use that
  firstPost := limit * (pageNum - 1)
  if f := ctx.Query("first"); f != "" {
    firstPost, _ = strconv.Atoi(f)
    // firstPost can't be less than 0
    if firstPost < 0 {firstPost = 0}
  }

  tags := ctx.Query("tags")

  // check and sort by oldest/newest first with oldest as fallback
  sortByOldest := false
  if s := ctx.Query("sort_by"); s == "oldest" {
    sortByOldest = true
  }

  // get posts from database
  posts := base.getPosts(firstPost, limit, sortByOldest, tags)

  showNext := true
  // TODO: if tags are specified, replace with nil
  // check if difference between all post count and posts shown is same
  if base.getPostCount(nil) - (firstPost + len(posts)) < 1 {
    showNext = false
  }

  // turn the tags from URL query into []int
  var tagsSlice []int
  for _, i := range strings.Split(tags, ",") {
    t, _ := strconv.Atoi(i)
    tagsSlice = append(tagsSlice, t)
  }

  // check if particular tags are selected
  var selectedTags []tagsSelection
  for _, i := range base.getTags() {
    var t tagsSelection 
    t.TagID = i.ID
    t.TagName = i.Name

    // check and set t.IsSelected to true
    for _, j := range tagsSlice {
      if t.TagID == j {
        t.IsSelected = true
        break
      }
    }

    selectedTags = append(selectedTags, t)
  }

  ctx.HTML(http.StatusOK, "views/posts.html", gin.H {
    "LimitOptions": limitOptions,
    "Limit": limit,
    "FirstPost": firstPost,
    "PageNumber": pageNum,
    "PrevPage": pageNum - 1,
	  "ShowPrev": !(firstPost == 0),
	  "ShowNext": showNext,
    "NextPage": pageNum + 1,
    "PrevFirst": firstPost - limit,
    "NextFirst": firstPost + limit,
    "Posts": posts,
    "SortByOldest": sortByOldest,
    "Tags": selectedTags,
  })
}

func getPost(ctx *gin.Context) {
  id, _ := strconv.Atoi(ctx.Param("id"))
  post := base.getPost(id)

  ctx.HTML(http.StatusOK, "views/post.html", gin.H {
    "Title": post.Title,
    "CreatedAt": post.CreatedAt,
    "UpdatedAt": post.UpdatedAt,
    "Content": template.HTML(post.Content),
  })
}

func HomePage(ctx *gin.Context) {
  recentPosts := base.getPosts(0, 10, true, "")
  tags := base.getTags()

  ctx.HTML(http.StatusOK, "views/home.html", gin.H {
    "RecentPosts": recentPosts,
    "Tags":        tags,
  })
}