Skip to content

Commit abc8776

Browse files
committed
end of section 3
1 parent 2cc7a98 commit abc8776

File tree

5 files changed

+582
-21
lines changed

5 files changed

+582
-21
lines changed

WebApp/main.go

+22-7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"net/url"
1212

13+
gmux "github.com/gorilla/mux"
1314
_ "github.com/mattn/go-sqlite3"
1415
"github.com/urfave/negroni"
1516
"github.com/yosssi/ace"
@@ -78,7 +79,8 @@ func main() {
7879
return
7980
}
8081

81-
mux := http.NewServeMux()
82+
//mux := http.NewServeMux()
83+
mux := gmux.NewRouter()
8284

8385
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
8486
p := Page{
@@ -103,7 +105,7 @@ func main() {
103105
http.Error(w, err.Error(), http.StatusInternalServerError)
104106
return
105107
}
106-
})
108+
}).Methods("GET")
107109

108110
mux.HandleFunc("/search", func(w http.ResponseWriter, r *http.Request) {
109111
qs := r.FormValue("queryString")
@@ -119,11 +121,11 @@ func main() {
119121
http.Error(w, err.Error(), http.StatusInternalServerError)
120122
return
121123
}
122-
})
124+
}).Methods("POST")
123125

124-
mux.HandleFunc("/books/add", func(w http.ResponseWriter, r *http.Request) {
125-
qs := r.FormValue("id")
126-
log.Println("/book/add => qs = ", qs)
126+
mux.HandleFunc("/books/{id}", func(w http.ResponseWriter, r *http.Request) {
127+
qs := gmux.Vars(r)["id"]
128+
log.Println("/books/add => qs = ", qs)
127129

128130
book, err := find(qs)
129131
if err != nil {
@@ -167,7 +169,20 @@ func main() {
167169
http.Error(w, err.Error(), http.StatusInternalServerError)
168170
return
169171
}
170-
})
172+
}).Methods("POST", "PUT")
173+
174+
mux.HandleFunc("/books/{pk}", func(w http.ResponseWriter, r *http.Request) {
175+
pk := gmux.Vars(r)["pk"]
176+
log.Println("/books/delete => pk = ", pk)
177+
178+
_, err := db.Exec("delete from books where pk = ?", pk)
179+
if err != nil {
180+
log.Println("/boks/delete pk = ", pk, " Error while deleting the book, error = ", err.Error())
181+
http.Error(w, err.Error(), http.StatusInternalServerError)
182+
return
183+
}
184+
w.WriteHeader(http.StatusOK)
185+
}).Methods("DELETE")
171186

172187
n := negroni.Classic()
173188
n.Use(negroni.HandlerFunc(verifyDBConnection))

WebApp/templates/index.ace

+34-14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
html
33
head
44
title Search Books
5+
link rel="icon" type="image/ico" href="https://cdn1.vectorstock.com/i/1000x1000/09/15/horizontal-stack-of-colored-books-flat-icon-vector-7320915.jpg"
56
script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"
67
script src="https://code.jquery.com/jquery-3.4.1.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"
78
link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"
@@ -65,16 +66,24 @@ html
6566
th scope="col" Author
6667
th scope="col" Classification
6768
th scope="col" ID
69+
th scope="col" Action
6870
tbody id="collection-results"
6971
{{range .Books}}
70-
tr
72+
tr id="table-row-{{.PK}}"
7173
td {{.PK}}
7274
td {{.Title}}
7375
td {{.Author}}
7476
td {{.Classification}}
7577
td {{.ID}}
78+
td
79+
button class="btn btn-outline-danger" title="Delete this book" type="button" onclick="return deleteBook({{.PK}})"
80+
i class="fas fa-trash-alt"
7681
{{end}}
77-
82+
83+
li
84+
85+
li
86+
ul class="list-group"
7887
table class="table table-hover table-bordered table-dark" id="table-search"
7988
thead
8089
tr
@@ -132,15 +141,12 @@ html
132141
showId("table-search");
133142

134143
parsed.forEach(function (result) {
135-
var actionForm = `
136-
<form id="${'add-book-form' + result.ID}" class="form-inline" onsubmit="return false">
137-
<input class="form-control mr-sm-2" type="text" name="id" value="${result.ID}" hidden>
138-
<button class="btn btn-outline-success" title="Add this book" type="submit" onclick="return addBook(${result.ID})">
139-
<i class="fas fa-plus-square"></i>
140-
</button>
141-
</form>
142-
`
143-
var row = $("<tr><td>" + result.Title + "</td><td>" + result.Author + "</td><td>" + result.Year + "</td><td>" + result.ID + "</td><td>" + actionForm + "</td></tr>");
144+
var addButton = `
145+
<button class="btn btn-outline-success" title="Add this book" type="submit" onclick="return addBook(${result.ID})">
146+
<i class="fas fa-plus-square"></i>
147+
</button>
148+
`
149+
var row = $("<tr><td>" + result.Title + "</td><td>" + result.Author + "</td><td>" + result.Year + "</td><td>" + result.ID + "</td><td>" + addButton + "</td></tr>");
144150
searchResults.append(row);
145151
});
146152
}
@@ -150,16 +156,20 @@ html
150156

151157
function addBook(id) {
152158
$.ajax({
153-
url: "/books/add",
159+
url: `/books/${id}`,
154160
method: "POST",
155-
data: $("#add-book-form" + id).serialize(),
156161
success: function (rawData) {
157162
hideId("table-search");
158163
if(rawData){
159164
var parsed = JSON.parse(rawData);
160165
if (parsed && parsed.Title) {
161166
var collectionResults = $("#collection-results");
162-
var row = $("<tr><td>" + parsed.PK + "</td><td>" + parsed.Title + "</td><td>" + parsed.Author + "</td><td>" + parsed.Classification + "</td><td>" + parsed.ID + "</td></tr>");
167+
var deleteButton = `
168+
<button class="btn btn-outline-danger" title="Delete this book" type="button" onclick="return deleteBook(${parsed.PK})">
169+
<i class="fas fa-trash-alt"></i>
170+
</button>
171+
`
172+
var row = $("<tr id=\"table-row-" + parsed.PK + "\"><td>" + parsed.PK + "</td><td>" + parsed.Title + "</td><td>" + parsed.Author + "</td><td>" + parsed.Classification + "</td><td>" + parsed.ID + "</td><td>" + deleteButton + "</td></tr>");
163173
collectionResults.append(row);
164174
$('#addBookResultModalHead').html(`Success <i class="far fa-smile"></i>`);
165175
$('#addBookResultModalBody').text(`${parsed.Title} has been added to database`);
@@ -176,6 +186,16 @@ html
176186
});
177187
return false;
178188
}
189+
190+
function deleteBook(pk){
191+
$.ajax({
192+
url: `/books/${pk}`,
193+
method: "DELETE",
194+
success: function (rawData) {
195+
$(`#table-row-${pk}`).remove();
196+
}
197+
});
198+
}
179199

180200

181201

0 commit comments

Comments
 (0)