-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
211 lines (178 loc) · 5.32 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
// main.go
package main
import (
"database/sql"
"encoding/json"
"errors"
"github.com/gorilla/mux"
"github.com/lib/pq"
"log"
"net/http"
"os"
)
// Book represents the model for a book
type Book struct {
ID int `json:"id"`
Title string `json:"title"`
Author string `json:"author"`
ISBN string `json:"isbn"`
}
var db *sql.DB
func main() {
// Initialize database connection
connStr := "postgres://postgres:postgres@postgres:5432/bookshop?sslmode=disable"
var err error
db, err = sql.Open("postgres", connStr)
if err != nil {
log.Fatal(err)
}
defer db.Close()
// Create the books table if it doesn't exist
createTableSQL := `
create table if not exists books (
id SERIAL primary key,
title varchar(100) not null,
author varchar(100) not null,
isbn varchar(14) unique not null
);`
_, err = db.Exec(createTableSQL)
if err != nil {
log.Fatal(err)
}
// Initialize router
router := mux.NewRouter()
// Define routes
router.HandleFunc("/books", getBooks).Methods("GET")
router.HandleFunc("/books", createBook).Methods("POST")
router.HandleFunc("/books/{id}", getBookByIdentifier).Methods("GET")
router.HandleFunc("/books/isbn/{isbn}", getBookByIdentifier).Methods("GET")
router.HandleFunc("/books/{id}", updateBookByIdentifier).Methods("PUT")
router.HandleFunc("/books/isbn/{isbn}", updateBookByIdentifier).Methods("PUT")
router.HandleFunc("/books/{id}", deleteBook).Methods("DELETE")
// Start server
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
log.Printf("Server starting on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, router))
}
// Handler functions
func getBooks(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
var books []Book
rows, err := db.Query("select id, title, author, isbn from books")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
defer rows.Close()
for rows.Next() {
var book Book
if err := rows.Scan(&book.ID, &book.Title, &book.Author, &book.ISBN); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
books = append(books, book)
}
json.NewEncoder(w).Encode(books)
}
func getBookByIdentifier(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
var book Book
var err error
if isbn, ok := params["isbn"]; ok {
err = db.QueryRow("select id, title, author, isbn from books where isbn = $1",
isbn).Scan(&book.ID, &book.Title, &book.Author, &book.ISBN)
} else if id, ok := params["id"]; ok {
err = db.QueryRow("select id, title, author, isbn from books where id = $1",
id).Scan(&book.ID, &book.Title, &book.Author, &book.ISBN)
} else {
http.Error(w, "No identifier provided", http.StatusBadRequest)
return
}
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, "Book not found", http.StatusNotFound)
return
} else if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(book)
}
func createBook(w http.ResponseWriter, r *http.Request) {
var book Book
if err := json.NewDecoder(r.Body).Decode(&book); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err := db.QueryRow(
"insert into books (title, author, isbn) values ($1, $2, $3) returning id",
book.Title, book.Author, book.ISBN,
).Scan(&book.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(book)
}
func updateBookByIdentifier(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
// Decode request body
var book Book
if err := json.NewDecoder(r.Body).Decode(&book); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
params := mux.Vars(r)
var err error
// Determine whether to update by ISBN or ID
if isbn, ok := params["isbn"]; ok {
err = db.QueryRow(
"update books set title = $1, author = $2, isbn = $3 where isbn = $4 returning id",
book.Title, book.Author, book.ISBN, isbn,
).Scan(&book.ID)
} else if id, ok := params["id"]; ok {
err = db.QueryRow(
"update books set title = $1, author = $2, isbn = $3 where id = $4 returning id",
book.Title, book.Author, book.ISBN, id,
).Scan(&book.ID)
} else {
http.Error(w, "No identifier provided", http.StatusBadRequest)
return
}
if err != nil {
// Check for unique constraint violation on ISBN
if pqErr, ok := err.(*pq.Error); ok && pqErr.Code == "23505" {
http.Error(w, "Book with this ISBN already exists", http.StatusConflict)
return
}
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, "Book not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(book)
}
func deleteBook(w http.ResponseWriter, r *http.Request) {
params := mux.Vars(r)
result, err := db.Exec("delete from books where id = $1", params["id"])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
rowsAffected, err := result.RowsAffected()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if rowsAffected == 0 {
http.Error(w, "Book not found", http.StatusNotFound)
return
}
w.WriteHeader(http.StatusNoContent)
}