-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathposts.go
137 lines (107 loc) · 3.77 KB
/
posts.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
/*
* Post... - a post struct for all my posts
*/
type Post struct {
ID int `json:"id"`
Title string `json:"title"`
Desc string `json:"description"`
Body string `json:"body"`
IsLive int `json:"isLive"`
Author string `json:"author"`
Slug string `json:"slug"`
}
func AllPosts(w http.ResponseWriter, r *http.Request) {
db := connect()
defer db.Close()
var posts []Post
results, err := db.Query("SELECT id, title, description, body, slug, isLive, author FROM posts")
for results.Next() {
var post Post
err = results.Scan(&post.ID, &post.Title, &post.Desc, &post.Body, &post.Slug, &post.IsLive, &post.Author)
if err != nil {
json.NewEncoder(w).Encode(HttpResp{Status: 500, Description: "Failed to select all from posts"})
}
posts = append(posts, post)
}
json.NewEncoder(w).Encode(posts)
}
func GetPost(w http.ResponseWriter, r *http.Request) {
db := connect()
defer db.Close()
vars := mux.Vars(r)
postID, err := strconv.Atoi(vars["id"])
if err != nil {
fmt.Fprintln(w, "Not a Valid id")
}
var post Post
// Execute the query
err = db.QueryRow("SELECT id, title, description, body, slug, isLive, author FROM posts where id = ?", postID).Scan(&post.ID, &post.Title, &post.Desc, &post.Body, &post.Slug, &post.IsLive, &post.Author)
if err != nil {
log.Print(err.Error()) // proper error handling instead of panic in your app
json.NewEncoder(w).Encode(HttpResp{Status: 500, Description: "Failed to select tag from database"})
}
json.NewEncoder(w).Encode(post)
}
func InsertPost(w http.ResponseWriter, r *http.Request) {
db := connect()
defer db.Close()
decoder := json.NewDecoder(r.Body)
var post Post
err := decoder.Decode(&post)
if err != nil {
log.Print(err.Error())
}
stmt, _ := db.Prepare("INSERT INTO posts (title, description, body, isLive, author, slug, created_at, updated_at, published_at) values (?,?,?,?,?,?, NOW(), NOW(), NOW()) ")
res, err := stmt.Exec(post.Title, post.Desc, post.Body, post.IsLive, post.Author, post.Slug)
if err != nil {
log.Print(err.Error()) // proper error handling instead of panic in your app
json.NewEncoder(w).Encode(HttpResp{Status: 500, Description: "Failed to insert post into database"})
}
id, err := res.LastInsertId()
if err != nil {
json.NewEncoder(w).Encode(HttpResp{Status: 500, Description: "Failed to get last insert id"})
}
json.NewEncoder(w).Encode(HttpResp{Status: 200, Description: "Successfully Inserted Post Into the Database", Body: strconv.Itoa(int(id))})
}
func DeletePost(w http.ResponseWriter, r *http.Request) {
db := connect()
defer db.Close()
vars := mux.Vars(r)
postID := vars["id"]
ID, _ := strconv.Atoi(postID)
stmt, err := db.Prepare("DELETE FROM posts where id = ?")
if err != nil {
log.Print(err.Error())
}
_, err = stmt.Exec(ID)
if err != nil {
json.NewEncoder(w).Encode(HttpResp{Status: 500, Description: "Failed to delete post from database"})
}
json.NewEncoder(w).Encode(HttpResp{Status: 200, Description: "Successfully Deleted Post from the Database"})
}
func EditPost(w http.ResponseWriter, r *http.Request) {
db := connect()
defer db.Close()
decoder := json.NewDecoder(r.Body)
var post Post
err := decoder.Decode(&post)
vars := mux.Vars(r)
postID := vars["id"]
ID, _ := strconv.Atoi(postID)
stmt, _ := db.Prepare("UPDATE posts SET title = ?, description = ?, body = ?, author = ?, updated_at = NOW() WHERE id = ?")
_, err = stmt.Exec(post.Title, post.Desc, post.Body, post.Author, ID)
if err != nil {
log.Print(err.Error())
json.NewEncoder(w).Encode(HttpResp{Status: 500, Description: "Failed to Update Post in the Database"})
}
json.NewEncoder(w).Encode(HttpResp{Status: 200, Description: "Successfully Update Post in the Database"})
}