Add comments to submissions
This commit is contained in:
parent
8d126fbe7a
commit
8bcff3c826
8 changed files with 97 additions and 30 deletions
|
@ -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 {
|
||||
|
|
|
@ -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 (
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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").
|
||||
|
|
|
@ -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))
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -57,6 +57,16 @@
|
|||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{if .Comment}}
|
||||
<div class="row">
|
||||
<div class="span1">
|
||||
<b>Comment:</b>
|
||||
</div>
|
||||
<div class="span10 well">
|
||||
<p>{{.Comment}}</p>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
<ul class="pager">
|
||||
{{if .Prev}}
|
||||
|
|
|
@ -33,40 +33,45 @@
|
|||
<p class="pull-right"><a href="/book/{{.ID}}">{{if .Cover}}<img class="img-rounded" src="/cover/{{.ID}}/small/{{.Title}}.jpg" alt="{{.Title}}" />{{end}}</a></p>
|
||||
</div>
|
||||
<div class="span10 well">
|
||||
<div class="row">
|
||||
<div class="span7">
|
||||
<p>
|
||||
<span class="muted">[{{if .Lang}}{{.Lang}}{{end}}]</span>
|
||||
<a href="/search/?q=title:{{.Title}}"><strong>{{.Title}}</strong></a><br />
|
||||
{{if .Authors}}<strong>Authors:</strong> {{range .Authors}}<a href="/search/?q=author:{{.}}">{{.}}</a>, {{end}}<br />{{end}}
|
||||
{{if .Publisher}}<strong>Publisher:</strong> <a href="/search/?q=publisher:{{.Publisher}}">{{.Publisher}}</a><br />{{end}}
|
||||
{{if .Tags}}<strong>Tags:</strong> {{range .Tags}}<a href="/search/?q=tag:{{.}}">{{.}}</a>, {{end}}<br />{{end}}
|
||||
{{if .Isbn}}<strong>ISBN:</strong> {{.Isbn}}<br />{{end}}
|
||||
{{if .Date}}<strong>Date:</strong> {{.Date}}<br />{{end}}
|
||||
{{.Description}}
|
||||
</p>
|
||||
</div>
|
||||
<div class="span3">
|
||||
{{if and .ID (not .Active)}}
|
||||
<div class="row btn-group pull-right">
|
||||
{{if eq $role "admin" "moderator"}}
|
||||
<a href="/store/{{.ID}}/" class="btn btn-success"><i class="icon-ok"></i> Save</a>
|
||||
{{end}}
|
||||
<a href="/submission/{{$submissionID}}/edit/{{.ID}}" class="btn btn-primary"><i class="icon-pencil"></i> Edit</a>
|
||||
<a href="/submission/{{$submissionID}}/delete/{{.ID}}/" class="btn btn-danger"><i class="icon-remove"></i> Delete</a>
|
||||
</div>
|
||||
<div class="row"><p></p></div>
|
||||
{{end}}
|
||||
<div class="row btn-group pull-right">
|
||||
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
|
||||
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="span7">
|
||||
<p>
|
||||
<span class="muted">[{{if .Lang}}{{.Lang}}{{end}}]</span>
|
||||
<a href="/search/?q=title:{{.Title}}"><strong>{{.Title}}</strong></a><br />
|
||||
{{if .Authors}}<strong>Authors:</strong> {{range .Authors}}<a href="/search/?q=author:{{.}}">{{.}}</a>, {{end}}<br />{{end}}
|
||||
{{if .Publisher}}<strong>Publisher:</strong> <a href="/search/?q=publisher:{{.Publisher}}">{{.Publisher}}</a><br />{{end}}
|
||||
{{if .Tags}}<strong>Tags:</strong> {{range .Tags}}<a href="/search/?q=tag:{{.}}">{{.}}</a>, {{end}}<br />{{end}}
|
||||
{{if .Isbn}}<strong>ISBN:</strong> {{.Isbn}}<br />{{end}}
|
||||
{{if .Date}}<strong>Date:</strong> {{.Date}}<br />{{end}}
|
||||
{{.Description}}
|
||||
</p>
|
||||
</div>
|
||||
<div class="span3">
|
||||
{{if and .ID (not .Active)}}
|
||||
<div class="row btn-group pull-right">
|
||||
{{if eq $role "admin" "moderator"}}
|
||||
<a href="/store/{{.ID}}/" class="btn btn-success"><i class="icon-ok"></i> Save</a>
|
||||
{{end}}
|
||||
<a href="/submission/{{$submissionID}}/edit/{{.ID}}" class="btn btn-primary"><i class="icon-pencil"></i> Edit</a>
|
||||
<a href="/submission/{{$submissionID}}/delete/{{.ID}}/" class="btn btn-danger"><i class="icon-remove"></i> Delete</a>
|
||||
</div>
|
||||
<div class="row"><p></p></div>
|
||||
{{end}}
|
||||
<div class="row btn-group pull-right">
|
||||
<a href="/download/{{.ID}}/{{.Title}}.epub" class="btn btn-inverse"><i class="icon-download-alt icon-white"></i> download</a>
|
||||
<a href="/read/{{.ID}}" class="btn btn-warning"><i class="icon-eye-open icon-white"></i> read it!</a>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
<form class="row form-inline" method="POST" action="/submission/{{$submissionID}}/comment/{{.Book.ID}}">
|
||||
<textarea class="span11" id="comment" rows="2" name="comment" placeholder="Comments about this book submission. Is it a better version than an existing one in the library?">{{.Comment}}</textarea>
|
||||
<button type="submit" class="btn btn-primary">Send comment</button>
|
||||
</form>
|
||||
{{end}}
|
||||
|
||||
{{template "footer.html"}}
|
||||
|
|
Reference in a new issue