-
Notifications
You must be signed in to change notification settings - Fork 0
/
story.query.go
232 lines (183 loc) · 4.9 KB
/
story.query.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package queries
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"time"
fiber "github.com/gofiber/fiber/v2"
_ "github.com/lib/pq"
models "github.com/tolubydesign/angular-story-backend/app/models"
utils "github.com/tolubydesign/angular-story-backend/app/utils"
fasthttp "github.com/valyala/fasthttp"
)
// TODO: require jwt, user verification, user login and user related story objects
/*
Get all Story objects related to the user.
db: PostgreSQL Database.
An error will be returned if issues occur. Otherwise all stories will be returned.
Return story array and possible error.
*/
func GetAllStories(db *sql.DB) ([]models.Story, error) {
request := `select * from Story`
rows, err := db.Query(request)
if err != nil {
return nil, err
}
defer rows.Close()
var stories []models.Story
for rows.Next() {
var story models.Story
err := rows.Scan(&story.Id, &story.Title, &story.Description, &story.Content)
if err != nil {
return stories, err
}
stories = append(stories, story)
}
if err = rows.Err(); err != nil {
return stories, err
}
return stories, nil
}
/*
Get a single Story Object from the database.
id: The story object in question.
con: fasthttp Request Context (Pointer).
db: PostgreSQL Database.
An error will be returned if issues occur. Otherwise the relevant story will be returned.
*/
func GetSingleStory(id string, con *fasthttp.RequestCtx, db *sql.DB) (models.Story, error) {
ctx, cancel := context.WithTimeout(con, 6*time.Second)
defer cancel()
query := fmt.Sprintf(`
SELECT * FROM story
WHERE story_id = '%s';
`, id)
var story models.Story
err := db.QueryRowContext(ctx, query).Scan(&story.Id, &story.Title, &story.Description, &story.Content)
// TODO: handle error responses
switch {
case err == sql.ErrNoRows:
log.Printf("Get Single Story. Error no story with id of %s\n", id)
case err != nil:
log.Fatalf("Get Single Story. query error: %v\n", err)
default:
log.Printf("Get Single Story. story with id of %s found.\n", id)
}
if err != nil {
return story, err
}
return story, nil
}
/*
POST Request.
Add a new story to the database. This includes content a description and a title.
c: Fiber Context object (Pointer).
db: PostgreSQL Database.
Returning possible error.
*/
func AddStory(c *fiber.Ctx, db *sql.DB) error {
fiberContext := c.Context()
ctx, cancel := context.WithTimeout(fiberContext, 3*time.Second)
defer cancel()
var body models.Story
byteBody := c.Body()
// Convert Struct to JSON
json.Unmarshal(byteBody, &body)
bodyContentJson, err := json.Marshal(body.Content)
if err != nil {
return err
}
model := models.Story{
Title: body.Title,
Description: body.Description,
Content: bodyContentJson,
}
execution := "INSERT INTO story(title, description, content) VALUES($1, $2, $3);"
result, err := db.ExecContext(ctx, execution, model.Title, model.Description, model.Content)
if err != nil {
return err
}
rows, err := result.RowsAffected()
if err != nil {
return err
}
if rows != 1 {
return err
}
return nil
}
/*
DELETE Request.
Remove a single story from the database.
id: The story object in question.
ctx: Fiber Context object (Pointer).
db: *sql.DB
Returning possible error.
*/
func DeleteSingleStory(id string, ctx *fasthttp.RequestCtx, db *sql.DB) error {
basicContext, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
deleteStmt := `DELETE FROM story WHERE story_id=$1`
_, err := db.ExecContext(basicContext, deleteStmt, id)
if err != nil {
return err
}
return nil
}
/*
PUT Request.
Update the contents and information of a single story row.
ctx: Fiber Context object (Pointer).
db: *sql.DB
Returning possible error.
*/
func UpdateStory(ctx *fiber.Ctx, db *sql.DB) error {
fiberContext := ctx.Context()
headers := ctx.GetReqHeaders()
headerId := headers["Id"]
headerDescription := headers["Description"]
headerTitle := headers["Title"]
basicContext, cancel := context.WithTimeout(fiberContext, 2*time.Second)
defer cancel()
var body models.Story
byteBody := ctx.Body()
json.Unmarshal(byteBody, &body)
content, err := json.Marshal(body.Content)
if err != nil {
return err
}
headerIdError := utils.ValidateLimitedStringVariable(headerId)
if headerIdError != nil {
return headerIdError
}
headerTitleError := utils.ValidateLimitedStringVariable(headerTitle)
if headerTitleError != nil {
return headerTitleError
}
headerDescriptionError := utils.ValidateLimitedStringVariable(headerDescription)
if headerDescriptionError != nil {
return headerDescriptionError
}
var updateStmt string
updateStmt = fmt.Sprintf(`
UPDATE story
SET title = $1,
description = $2,
content = $3
WHERE story_id = $4;
`)
result, err := db.ExecContext(basicContext, updateStmt, headerTitle, headerDescription, content, headerId)
if err != nil {
return err
}
rows, err := result.RowsAffected()
if err != nil {
return err
}
if rows != 1 {
return err
}
return nil
}