summaryrefslogtreecommitdiff
path: root/src/blog/controller.go
blob: 8355e87f53c498a1a5d9edebd1b5073569ef719c (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
/*
 * 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 (
  "strconv"
  "fmt"

	_ "github.com/go-sql-driver/mysql"
)

type Post struct {
  ID        int
  CreatedAt string
  UpdatedAt *string
  Title     string
  Content   string
  Tags      []Tag
}

type Tag struct {
  ID   int
  Name string
}

func (db *dbhandler) getPostCount(tag *int) int {
  var query string
  if tag != nil {
    query = "SELECT COUNT(DISTINCT(PostID)) FROM Post_Tags WHERE TagID = " + strconv.Itoa(*tag)
  } else {
	// because some posts might not even have tags
    query = "SELECT COUNT(*) FROM Posts"
  }

  rows, err := db.connection.Prepare(query)
  if err != nil {
    panic(err.Error())
  }
  defer rows.Close()

  var count int
  if err := rows.QueryRow().Scan(&count); err != nil {
    panic(err)
  }

  return count
}

// start = read from nth row, limit = read n rows
func (db *dbhandler) getPosts(start, limit int, reversed bool, tags string) []Post {
  var qry string
  if len(tags) < 1 {
    qry = "SELECT ID, DATE_FORMAT(CreatedAt, '%D %M %Y'), Title FROM Posts"
  } else {
    qry = `SELECT DISTINCT Posts.ID, DATE_FORMAT(Posts.CreatedAt, '%D %M %Y'), Posts.Title
    FROM Post_Tags 
    INNER JOIN Posts ON Post_Tags.PostID = Posts.ID
    WHERE Post_Tags.TagID IN (` + tags + `)`
  }
  if reversed {
    qry = qry + " ORDER BY ID DESC"
  }
  qry = fmt.Sprintf("%s LIMIT %d,%d", qry, start, limit)

  rows, err := db.connection.Query(qry) 
  if err != nil {
    panic(err)
  }
  defer rows.Close()

  var posts []Post
  for rows.Next() {
    var p Post
    err := rows.Scan(&p.ID, &p.CreatedAt, &p.Title)
    if err != nil {
      panic(err)
    }
    // load post's tags
    p.Tags = db.getPostTags(p.ID)
    posts = append(posts, p)
  }

  return posts
}

func (db *dbhandler) getPost(id int) Post {
  rows, err := db.connection.Prepare(
    `SELECT ID, DATE_FORMAT(CreatedAt, "%D %M %Y"), DATE_FORMAT(UpdatedAt, "%D %M %Y"), Title, Content FROM Posts WHERE ID = ?`,
  )
  if err != nil {
    panic(err.Error())
  }
  defer rows.Close()

  var post Post
  if err := rows.QueryRow(id).Scan(&post.ID, &post.CreatedAt, &post.UpdatedAt, &post.Title, &post.Content); err != nil {
    // TODO: handle error when rows are empty
    post.Content = "404"
    // panic(err)
  }

  return post
}

func (db *dbhandler) getPostTags(id int) []Tag {
  rows, err := db.connection.Query(
    `SELECT Tags.ID, Tags.Name FROM Post_Tags 
    INNER JOIN Tags ON Post_Tags.TagID = Tags.ID 
    WHERE Post_Tags.PostID = ` + strconv.Itoa(id),
  ) 
  if err != nil {
		panic(err.Error())
	}
	defer rows.Close()

  var tags []Tag
  for rows.Next() {
    var tag Tag
    err := rows.Scan(&tag.ID, &tag.Name)
    if err != nil {
      panic(err)
    }
    tags = append(tags, tag)
  }

  return tags
}

// returns all tags
func (db *dbhandler) getTags() []Tag {
  rows, err := db.connection.Query("SELECT ID, Name FROM Tags") 
  if err != nil {
    panic(err.Error())
  }
  defer rows.Close()

  var tags []Tag
  for rows.Next() {
    var t Tag
    err := rows.Scan(&t.ID, &t.Name)
    if err != nil {
      panic(err)
    }
    tags = append(tags, t)
  }

  return tags
}