-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
52 lines (44 loc) · 1.7 KB
/
common.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
package main
import (
"database/sql"
"log"
"net/http"
"github.com/gorilla/mux"
"github.com/jmoiron/sqlx"
)
type postLinks struct {
EditLink string `json:"editLink,omitempty"`
ViewLink string `json:"viewLink"`
}
// Used when retrieving posts from the database
type post struct {
PostID int64 `json:"-" db:"post_id,omitempty"`
Title string `json:"title,omitempty" db:"title,omitempty"`
Body string `json:"body,omitempty" db:"body,omitempty"`
Scope string `json:"scope,omitempty" db:"scope,omitempty"` // Private or Public
LinkID string `json:"link,omitempty" db:"link_id,omitempty"`
Access string `json:"-" db:"access,omitempty"` // Edit or View
Epoch int64 `json:"epoch,omitempty" db:"epoch,omitempty"`
ReportReason string `json:"reason,omitempty" db:"reason,omitempty"`
}
var db *sqlx.DB
const navBarTemplatePath string = "dist/templates/navBar.tmpl"
const footerTemplatePath string = "dist/templates/footer.tmpl"
// Parse the URL to find the post link ID and scan the database for the corresponding entry
func getEntryForRequestedLink(w http.ResponseWriter, r *http.Request) (post, error) {
postLinkID := mux.Vars(r)["link_id"]
// Retrieve the post from the database
query := `SELECT *
FROM Posts, Links
WHERE Posts.post_id = Links.post_id and link_id = $1`
entry := post{}
err := db.Get(&entry, query, postLinkID)
if err == sql.ErrNoRows {
http.Error(w, http.StatusText(http.StatusNotFound), http.StatusNotFound)
log.Println("error: no entries found")
} else if err != nil {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
log.Println("error: unsuccessful data lookup")
}
return entry, err
}