diff --git a/lib/admin.go b/lib/admin.go index 1319d8d..6b2791d 100644 --- a/lib/admin.go +++ b/lib/admin.go @@ -148,6 +148,7 @@ func saveHandler(h handler) { type newBook struct { TitleFound int AuthorFound int + Comment string B database.Book } type newData struct { @@ -194,6 +195,10 @@ func newHandler(h handler) { data.Books[i].B = b _, data.Books[i].TitleFound, _ = h.db.GetBooks("title:"+b.Title, 1, 0) _, data.Books[i].AuthorFound, _ = h.db.GetBooks("author:"+strings.Join(b.Authors, " author:"), 1, 0) + data.Books[i].Comment, err = h.db.GetComment(b.ID) + if err != nil { + log.Error("Error getting comment for ", b.Title, " (", b.ID, "): ", err) + } } data.Page = page + 1 if num > (page+1)*newItemsPage { diff --git a/lib/database/database.go b/lib/database/database.go index 649a965..a39b5d1 100644 --- a/lib/database/database.go +++ b/lib/database/database.go @@ -31,7 +31,9 @@ type DB interface { AddSubmission(submission Submission) (id int, err error) UpdateSubmission(id int, status string, book *Book) error UpdateSubmissionByBook(bookID string, status string, book *Book) error + UpdateSubmissionComment(submissionID, bookID, comment string) error GetSubmission(submissionID string) (submission []Submission, err error) + GetComment(bookID string) (string, error) } const ( diff --git a/lib/database/ro.go b/lib/database/ro.go index 00d4be4..c75e63e 100644 --- a/lib/database/ro.go +++ b/lib/database/ro.go @@ -97,10 +97,18 @@ func (db *roDB) UpdateSubmission(id int, status string, book *Book) error { return errors.New("RO database") } +func (db *roDB) UpdateSubmissionComment(submissionID, bookdID, comment string) error { + return errors.New("RO database") +} + func (db *roDB) UpdateSubmissionByBook(bookID string, status string, book *Book) error { return errors.New("RO database") } +func (db *roDB) GetComment(bookID string) (string, error) { + return db.db.GetComment(bookID) +} + func (db *roDB) GetSubmission(submissionID string) (submission []Submission, err error) { return db.db.GetSubmission(submissionID) } diff --git a/lib/database/submissions.go b/lib/database/submissions.go index 87dcff7..91d8439 100644 --- a/lib/database/submissions.go +++ b/lib/database/submissions.go @@ -5,6 +5,7 @@ type Submission struct { SubmissionID string `sql:"type:varchar(16)"` Filename string Status string + Comment string BookID string `sql:"type:varchar(16),unique"` Book *Book } @@ -32,6 +33,26 @@ func (db *pgDB) UpdateSubmissionByBook(bookID string, status string, book *Book) return err } +func (db *pgDB) UpdateSubmissionComment(submissionID, bookID, comment string) error { + _, err := db.sql.Model(&Submission{}). + Set("comment = ?", comment). + Where("submission_id = ?", submissionID). + Where("book_id = ?", bookID). + Update() + return err +} + +func (db *pgDB) GetComment(bookID string) (string, error) { + var submission Submission + err := db.sql.Model(&submission). + Where("book_id = ?", bookID). + Select() + if err != nil { + return "", err + } + return submission.Comment, nil +} + func (db *pgDB) GetSubmission(submissionID string) (submission []Submission, err error) { err = db.sql.Model(&submission). Column("Book"). diff --git a/lib/trantor.go b/lib/trantor.go index 480a672..76493c6 100644 --- a/lib/trantor.go +++ b/lib/trantor.go @@ -171,6 +171,7 @@ func InitRouter(db database.DB, sg *StatsGatherer, assetsPath string) http.Handl r.HandleFunc("/upload/", sg.Gather(uploadHandler)).Methods("GET") r.HandleFunc("/upload/", sg.Gather(uploadPostHandler)).Methods("POST") r.HandleFunc("/submission/{submissionID:"+idPattern+"}", sg.Gather(submissionHandler)) + r.HandleFunc("/submission/{submissionID:"+idPattern+"}/comment/{id:"+idPattern+"}", sg.Gather(submissionCommentHandler)).Methods("POST") r.HandleFunc("/submission/{submissionID:"+idPattern+"}/edit/{id:"+idPattern+"}", sg.Gather(editHandler)) r.HandleFunc("/submission/{submissionID:"+idPattern+"}/save/{id:"+idPattern+"}", sg.Gather(saveHandler)).Methods("POST") r.HandleFunc("/submission/{submissionID:"+idPattern+"}/delete/{ids:(?:"+idPattern+"/)+}", sg.Gather(deleteHandler)) diff --git a/lib/upload.go b/lib/upload.go index cb940de..3766ee0 100644 --- a/lib/upload.go +++ b/lib/upload.go @@ -144,6 +144,21 @@ type submissionData struct { Submissions []database.Submission } +func submissionCommentHandler(h handler) { + submissionID := mux.Vars(h.r)["submissionID"] + bookID := mux.Vars(h.r)["id"] + comment := h.r.FormValue("comment") + + err := h.db.UpdateSubmissionComment(submissionID, bookID, comment) + if err != nil { + log.Error("Adding comment (submission: ", submissionID, ", book: ", bookID, ") <", comment, ">: ", err) + h.sess.Notify("Error adding a comment!", "Can't add the comment rigt now. Try again later or report it to the site admins", "error") + } else { + h.sess.Notify("Comment added!", "", "success") + } + http.Redirect(h.w, h.r, "/submission/"+submissionID, http.StatusFound) +} + func genID() string { b := make([]byte, 12) rand.Read(b) diff --git a/templates/new.html b/templates/new.html index 0977e42..39f7db3 100644 --- a/templates/new.html +++ b/templates/new.html @@ -57,6 +57,16 @@ {{end}} + {{if .Comment}} +
+
+ Comment: +
+
+

{{.Comment}}

+
+
+ {{end}} {{end}}