-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
218 lines (175 loc) · 4.6 KB
/
main.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
package main
import (
"database/sql"
"fmt"
"log"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/gofor-little/env"
_ "github.com/lib/pq"
)
// Not in use, just for reference
type User struct {
Id string `json:"id"`
Name string `json:"name"`
Email string `json:"email"`
Password string `json:"password"`
CreatedAt string `json:"created_at"`
UpdatedAt *string `json:"updated_at"`
}
type Article struct {
Id string `json:"id"`
Slug string `json:"slug"`
Title string `json:"title"`
Body string `json:"body"`
UserId string `json:"user_id"`
CreatedAt string `json:"created_at"`
UpdatedAt *string `json:"updated_at"`
}
func main() {
if err := env.Load(".env"); err != nil {
panic(err)
}
db, err := getDatabaseConnection()
if err != nil {
log.Fatalln(err)
}
app := fiber.New()
app.Get("/articles", func(c *fiber.Ctx) error {
return getArticles(c, db)
})
app.Post("/articles", func(c *fiber.Ctx) error {
return storeArticle(c, db)
})
app.Get("/articles/:id", func(c *fiber.Ctx) error {
return getArticle(c, db)
})
app.Put("/articles/:id", func(c *fiber.Ctx) error {
return updateArticle(c, db)
})
app.Delete("/articles/:id", func(c *fiber.Ctx) error {
return deleteArticle(c, db)
})
port := env.Get("APP_PORT", "8000")
log.Fatalln(app.Listen(fmt.Sprintf(":%v", port)))
}
func getArticles(c *fiber.Ctx, db *sql.DB) error {
rows, err := db.Query("SELECT * FROM articles")
if err != nil {
log.Fatalln(err)
return c.JSON("An error occurred.")
}
//Docs: This releases any resources held by the rows no matter how the function returns.
//Looping all the way through the rows also closes it implicitly, but it is better
//to use defer to make sure rows is closed no matter what.
defer rows.Close()
var articles []Article
for rows.Next() {
var article Article
rows.Scan(
&article.Id,
&article.Slug,
&article.Title,
&article.Body,
&article.UserId,
&article.CreatedAt,
&article.UpdatedAt,
)
articles = append(articles, article)
}
return c.JSON(fiber.Map{
"articles": articles,
})
}
func storeArticle(c *fiber.Ctx, db *sql.DB) error {
var article Article
c.BodyParser(&article)
article.Slug = strings.ReplaceAll(strings.ToLower(article.Title), " ", "-")
err := db.QueryRow(
"INSERT INTO articles (slug, title, body, user_id) VALUES ($1, $2, $3, $4) returning id, created_at",
article.Slug,
article.Title,
article.Body,
article.UserId,
).Scan(&article.Id, &article.CreatedAt)
if err != nil {
log.Fatalln(err)
return c.JSON("An error occurred.")
}
return c.JSON(fiber.Map{
"message": "Created a new article.",
"article": article,
})
}
func getArticle(c *fiber.Ctx, db *sql.DB) error {
id := c.Params("id")
var article Article
err := db.QueryRow("SELECT * FROM articles WHERE id = $1", id).Scan(
&article.Id,
&article.Slug,
&article.Title,
&article.Body,
&article.UserId,
&article.CreatedAt,
&article.UpdatedAt,
)
if err != nil {
log.Fatalln(err)
return c.JSON("An error occurred.")
}
fmt.Println(article.Id, err)
return c.JSON(fiber.Map{
"article": article,
})
}
func updateArticle(c *fiber.Ctx, db *sql.DB) error {
id := c.Params("id")
var article Article
c.BodyParser(&article)
article.Slug = strings.ReplaceAll(strings.ToLower(article.Title), " ", "-")
err := db.QueryRow(
"UPDATE articles SET slug = $1, title = $2, body = $3, updated_at = now() WHERE id = $4 returning id, user_id, created_at, updated_at",
article.Slug,
article.Title,
article.Body,
id,
).Scan(&article.Id, &article.UserId, &article.CreatedAt, &article.UpdatedAt)
if err != nil {
log.Fatalln(err)
return c.JSON("An error occurred.")
}
return c.JSON(fiber.Map{
"message": "Selected article has been updated.",
"article": article,
})
}
func deleteArticle(c *fiber.Ctx, db *sql.DB) error {
id := c.Params("id")
_, err := db.Exec("DELETE FROM articles WHERE id = $1", id)
if err != nil {
log.Fatalln(err)
return c.JSON("An error occurred.")
}
return c.JSON(fiber.Map{
"message": "Selected article has been deleted.",
})
}
func getDatabaseConnection() (*sql.DB, error) {
dbHost := env.Get("DB_HOST", "localhost")
dbPort := env.Get("DB_PORT", "5432")
dbDatabase := env.Get("DB_DATABASE", "articles_app")
dbUsername := env.Get("DB_USERNAME", "postgres")
dbPassword := env.Get("DB_PASSWORD", "password")
dbSSLMode := env.Get("DB_SSLMODE", "disable")
connectionURL := fmt.Sprintf(
"postgresql://%s:%s@%s:%s/%s?sslmode=%s",
dbUsername,
dbPassword,
dbHost,
dbPort,
dbDatabase,
dbSSLMode,
)
db, err := sql.Open("postgres", connectionURL)
return db, err
}