diff --git a/database.go b/database.go index 679efc4..f6a732b 100644 --- a/database.go +++ b/database.go @@ -75,8 +75,16 @@ func (d *DB) RemoveBook(id bson.ObjectId) error { return d.books.Remove(bson.M{"_id": id}) } -func (d *DB) UpdateBook(id bson.ObjectId, book interface{}) error { - return d.books.Update(bson.M{"_id": id}, bson.M{"$set": book}) +func (d *DB) UpdateBook(id bson.ObjectId, data interface{}) error { + return d.books.Update(bson.M{"_id": id}, bson.M{"$set": data}) +} + +func (d *DB) IncVisit(id bson.ObjectId) error { + return d.books.Update(bson.M{"_id": id}, bson.M{"$inc": bson.M{"VisitsCount": 1}}) +} + +func (d *DB) IncDownload(path string) error { + return d.books.Update(bson.M{"path": path}, bson.M{"$inc": bson.M{"DownloadCount": 1}}) } /* optional parameters: length and start index diff --git a/trantor.go b/trantor.go index 1be19e6..48faf93 100644 --- a/trantor.go +++ b/trantor.go @@ -55,10 +55,17 @@ func bookHandler(w http.ResponseWriter, r *http.Request) { http.NotFound(w, r) return } + db.IncVisit(id) data.Book = books[0] loadTemplate(w, "book", data) } +func downloadHandler(w http.ResponseWriter, r *http.Request) { + file := r.URL.Path[1:] + db.IncDownload(file) + http.ServeFile(w, r, file) +} + func fileHandler(path string) { h := http.FileServer(http.Dir(path[1:])) http.Handle(path, http.StripPrefix(path, h)) @@ -131,9 +138,9 @@ func main() { http.HandleFunc("/save/", saveHandler) http.HandleFunc("/delete/", deleteHandler) http.HandleFunc("/about/", aboutHandler) + http.HandleFunc("/books/", downloadHandler) fileHandler("/img/") fileHandler("/cover/") - fileHandler("/books/") fileHandler("/css/") fileHandler("/js/") http.HandleFunc("/", indexHandler)