-
Notifications
You must be signed in to change notification settings - Fork 0
/
blog.go
116 lines (106 loc) · 4.09 KB
/
blog.go
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
package blog
import (
"database/sql"
"fmt"
"log"
"time"
)
const TimestampFormat = time.RFC1123
const (
UsersTable = "tUsers"
BlogEntriesTable = "tEntries"
CommentsTable = "tComments"
)
var (
ErrEntryNotFound = fmt.Errorf("blog entry not found")
ErrUnableToScan = fmt.Errorf("could not scan entry to struct")
ErrUnableToPrepare = fmt.Errorf("could not prepare sql statement")
ErrUnableToExecute = fmt.Errorf("could not execute sql statement")
ErrUnableToQuery = fmt.Errorf("could not query with sql statement")
ErrCantFindComments = fmt.Errorf("could not find comments for post")
ErrCantGetLastInsertID = fmt.Errorf("could not get LastInsertID")
)
func InsertEntry(db *sql.DB, title, body string, userID int64, entryType EntryType) (Entry, error) {
timestamp := time.Now().Format(TimestampFormat)
sql_string := "INSERT INTO " + BlogEntriesTable + "(timestamp, title, body, userid, type) VALUES (?, ?, ?, ?, ?)"
prepared, err := db.Prepare(sql_string)
if err != nil {
return Entry{}, fmt.Errorf("CreateEntryAndInsert %w: %s", ErrUnableToPrepare, err)
}
n, err := prepared.Exec(timestamp, title, body, userID, entryType)
if err != nil {
return Entry{}, fmt.Errorf("CreateEntryAndInsert %w: %s", ErrUnableToExecute, err)
}
id, err := n.LastInsertId()
if err != nil {
return Entry{}, fmt.Errorf("CreateEntryAndInsert %w: %s", ErrCantGetLastInsertID, err)
}
return GetEntry(db, id)
}
func entrySELECTStatement() string {
return fmt.Sprintf("SELECT %s.id, %s.timestamp, %s.title, %s.body, %s.userid, %s.type, %s.username FROM %s INNER JOIN %s ON %s.userid=%s.id WHERE %s.id=?;",
BlogEntriesTable, BlogEntriesTable, BlogEntriesTable, BlogEntriesTable, BlogEntriesTable, BlogEntriesTable, UsersTable, BlogEntriesTable, UsersTable, BlogEntriesTable, UsersTable, BlogEntriesTable)
}
func entriesSELECTStatement() string {
return fmt.Sprintf("SELECT %s.id, %s.timestamp, %s.title, %s.body, %s.userid, %s.type, %s.username FROM %s INNER JOIN %s ON %s.userid=%s.id ORDER BY %s.id DESC;",
BlogEntriesTable, BlogEntriesTable, BlogEntriesTable, BlogEntriesTable, BlogEntriesTable, BlogEntriesTable, UsersTable, BlogEntriesTable, UsersTable, BlogEntriesTable, UsersTable, BlogEntriesTable)
}
func GetEntry(db *sql.DB, id int64) (Entry, error) {
sql_string := entrySELECTStatement()
prepared, err := db.Prepare(sql_string)
if err != nil {
return Entry{}, fmt.Errorf("GetEntry %w: %s", ErrUnableToPrepare, err)
}
row := prepared.QueryRow(id)
var entry Entry
err = row.Scan(&entry.ID, &entry.Timestamp, &entry.Title, &entry.Body, &entry.UserID, &entry.Type, &entry.Username)
if err != nil {
return Entry{}, fmt.Errorf("GetEntry %w: %s", ErrUnableToQuery, err)
}
comments, err := GetComments(db, entry.ID)
if err != nil {
log.Println("GetEntries %w", err)
}
entry.Comments = comments
return entry, nil
}
func GetEntries(db *sql.DB) ([]Entry, error) {
sql_string := entriesSELECTStatement()
prepared, err := db.Prepare(sql_string)
if err != nil {
return nil, fmt.Errorf("GetEntries %w: %s", ErrUnableToPrepare, err)
}
rows, err := prepared.Query()
if err != nil {
return nil, fmt.Errorf("GetEntries %w: %s", ErrUnableToQuery, err)
}
defer rows.Close()
entries := make([]Entry, 0)
for rows.Next() {
var entry Entry
err = rows.Scan(&entry.ID, &entry.Timestamp, &entry.Title, &entry.Body, &entry.UserID, &entry.Type, &entry.Username)
if err != nil {
log.Println("GetEntries %w: %s", ErrUnableToScan, err)
continue
}
comments, err := GetComments(db, entry.ID)
if err != nil {
log.Println("GetEntries %w", err)
}
entry.Comments = comments
entries = append(entries, entry)
}
return entries, nil
}
func UpdateEntry(db *sql.DB, title, body string, postID int64) (Entry, error) {
sql_string := "UPDATE " + BlogEntriesTable + " SET title=?, body=? WHERE id=?"
prepared, err := db.Prepare(sql_string)
if err != nil {
return Entry{}, fmt.Errorf("UpdateEntry %w: %s", ErrUnableToPrepare, err)
}
_, err = prepared.Exec(title, body, postID)
if err != nil {
return Entry{}, fmt.Errorf("UpdateEntry %w: %s", ErrUnableToExecute, err)
}
return GetEntry(db, postID)
}