From 60379ba1ef569ed726599747b03e477e83d11e43 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Tue, 5 Mar 2019 19:54:21 +0000 Subject: [PATCH] Be able to list users submisions - Closes: #45 --- lib/database/database.go | 8 +++- lib/database/ro.go | 6 ++- lib/database/submissions.go | 21 +++++++++- lib/trantor.go | 1 + lib/upload.go | 4 +- lib/user.go | 23 +++++++++++ templates/dashboard.html | 1 + templates/header.html | 1 + templates/submission.html | 10 +++-- templates/submissions.html | 77 +++++++++++++++++++++++++++++++++++++ 10 files changed, 143 insertions(+), 9 deletions(-) create mode 100644 templates/submissions.html diff --git a/lib/database/database.go b/lib/database/database.go index 13daf9e..e1eb2aa 100644 --- a/lib/database/database.go +++ b/lib/database/database.go @@ -31,11 +31,12 @@ type DB interface { IncDownloads(ID string) error GetDownloadCounter(ID string) (int, error) GetFrontPage() FrontPage - AddSubmission(submission Submission) (id int, err error) + AddSubmission(submission Submission, userName string) (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) + GetUserSubmissions(userName string) (submission []Submission, err error) GetComment(bookID string) (string, error) BookLister @@ -228,12 +229,17 @@ CREATE INDEX IF NOT EXISTS visits_book_id_idx on visits(book_id); -- Submissions indexes CREATE INDEX IF NOT EXISTS submissions_id_idx on submissions(submission_id); CREATE INDEX IF NOT EXISTS submissions_book_idx on submissions(book_id); +CREATE INDEX IF NOT EXISTS submissions_user_idx on submissions(user_id); -- Submissions constraint ALTER TABLE ONLY submissions DROP CONSTRAINT IF EXISTS submissions_book_id_fkey; ALTER TABLE ONLY submissions ADD CONSTRAINT submissions_book_id_fkey FOREIGN KEY (book_id) REFERENCES books(id) ON DELETE SET NULL; +ALTER TABLE ONLY submissions + DROP CONSTRAINT IF EXISTS submissions_user_id_fkey; +ALTER TABLE ONLY submissions + ADD CONSTRAINT submissions_user_id_fkey FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE SET NULL; -- BookLists indexes CREATE INDEX IF NOT EXISTS book_lists_list_id_idx on book_lists(list_id); diff --git a/lib/database/ro.go b/lib/database/ro.go index 4deb60e..6010a0f 100644 --- a/lib/database/ro.go +++ b/lib/database/ro.go @@ -101,7 +101,7 @@ func (db *roDB) GetFrontPage() FrontPage { return db.db.GetFrontPage() } -func (db *roDB) AddSubmission(submission Submission) (id int, err error) { +func (db *roDB) AddSubmission(submission Submission, userName string) (id int, err error) { return 0, errors.New("RO database") } @@ -125,6 +125,10 @@ func (db *roDB) GetSubmission(submissionID string) (submission []Submission, err return db.db.GetSubmission(submissionID) } +func (db *roDB) GetUserSubmissions(userName string) (submission []Submission, err error) { + return db.db.GetUserSubmissions(userName) +} + func (db *roDB) NewBookList(listID, title, username string, description []string) error { return errors.New("RO database") } diff --git a/lib/database/submissions.go b/lib/database/submissions.go index 91d8439..9a26771 100644 --- a/lib/database/submissions.go +++ b/lib/database/submissions.go @@ -1,16 +1,26 @@ package database +// TODO: clean up old submissions + type Submission struct { ID int `sql:"type:serial"` SubmissionID string `sql:"type:varchar(16)"` Filename string Status string Comment string + UserID int `sql:"type:integer,unique"` + User *User BookID string `sql:"type:varchar(16),unique"` Book *Book } -func (db *pgDB) AddSubmission(submission Submission) (id int, err error) { +func (db *pgDB) AddSubmission(submission Submission, userName string) (id int, err error) { + if userName != "" { + user, err := db.getUser(userName) + if err == nil { + submission.UserID = user.ID + } + } err = db.sql.Insert(&submission) return submission.ID, err } @@ -61,6 +71,15 @@ func (db *pgDB) GetSubmission(submissionID string) (submission []Submission, err return } +func (db *pgDB) GetUserSubmissions(userName string) (submission []Submission, err error) { + err = db.sql.Model(&submission). + Column("Book", "User"). + Where("username = ?", userName). + Order("id DESC"). + Select() + return +} + func extractID(book *Book) interface{} { if book == nil { return nil diff --git a/lib/trantor.go b/lib/trantor.go index a0f016b..bcefd00 100644 --- a/lib/trantor.go +++ b/lib/trantor.go @@ -193,6 +193,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/", sg.Gather(userSubmissionsHandler)) 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)) diff --git a/lib/upload.go b/lib/upload.go index 844ef33..c74e087 100644 --- a/lib/upload.go +++ b/lib/upload.go @@ -102,11 +102,11 @@ func uploadPostHandler(h handler) { log.Error("Can not open uploaded file ", f.Filename, ": ", err) h.sess.Notify("Upload problem!", "There was a problem with book "+f.Filename, "error") submission.Status = "It was not possible to read the book" - h.db.AddSubmission(submission) + h.db.AddSubmission(submission, h.sess.User) continue } - id, err := h.db.AddSubmission(submission) + id, err := h.db.AddSubmission(submission, h.sess.User) if err != nil { log.Error("Can add submission to db for ", f.Filename, ": ", err) } diff --git a/lib/user.go b/lib/user.go index 9de1715..5a29d15 100644 --- a/lib/user.go +++ b/lib/user.go @@ -112,6 +112,29 @@ func settingsHandler(h handler) { h.load("settings", data) } +func userSubmissionsHandler(h handler) { + if h.sess.User == "" { + notFound(h) + return + } + var data submissionsData + data.S = GetStatus(h) + data.S.Title = "Submissions" + + var err error + data.Submissions, err = h.db.GetUserSubmissions(h.sess.User) + if err != nil { + log.Warn("Can't get submissions for user ", h.sess.User, ": ", err) + } + + h.load("submissions", data) +} + +type submissionsData struct { + S Status + Submissions []database.Submission +} + func userAdminHandler(h handler) { if !h.sess.IsAdmin() { notFound(h) diff --git a/templates/dashboard.html b/templates/dashboard.html index d5fb3a9..a37da19 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -12,6 +12,7 @@ {{end}}
  • {{end}} +
  • Submissions
  • Settings
  • Log Out
  • diff --git a/templates/header.html b/templates/header.html index e5204b3..dea88d2 100644 --- a/templates/header.html +++ b/templates/header.html @@ -76,6 +76,7 @@ {{end}}
  • {{end}} +
  • Submissions
  • Settings
  • Log Out
  • diff --git a/templates/submission.html b/templates/submission.html index b9b3c75..2951adf 100644 --- a/templates/submission.html +++ b/templates/submission.html @@ -16,10 +16,10 @@

    {{.Filename}} - {{if .Book.Active}} - - {{else if not .Book.ID}} + {{if not .Book}} + {{else if .Book.Active}} + {{else}} {{end}} @@ -28,7 +28,7 @@

    {{with .Book}} - {{if .ID}} + {{if .}} @@ -68,10 +68,12 @@ {{end}}
    + {{if .Book}}
    + {{end}} {{end}} {{template "footer.html"}} diff --git a/templates/submissions.html b/templates/submissions.html new file mode 100644 index 0000000..cd1dcb6 --- /dev/null +++ b/templates/submissions.html @@ -0,0 +1,77 @@ +{{template "header.html" .S}} + +
    +

    All your submissions:

    +
    + +{{$role := .S.Role}} +{{range .Submissions}} +
    +
    +
    +

    {{.Filename}} + {{if not .Book}} + + {{else if .Book.Active}} + + {{else}} + + {{end}} + {{.Status}}

    +
    + +
    + {{$submissionID := .SubmissionID}} + {{with .Book}} + + {{if .}} + +
    +
    +
    +

    + [{{if .Lang}}{{.Lang}}{{end}}] + {{.Title}}
    + {{if .Authors}}Authors: {{range .Authors}}{{.}}, {{end}}
    {{end}} + {{if .Publisher}}Publisher: {{.Publisher}}
    {{end}} + {{if .Tags}}Tags: {{range .Tags}}{{.}}, {{end}}
    {{end}} + {{if .Isbn}}ISBN: {{.Isbn}}
    {{end}} + {{if .Date}}Date: {{.Date}}
    {{end}} + {{.Description}} +

    +
    +
    + {{if and .ID (not .Active)}} +
    + {{if eq $role "admin" "moderator"}} + Save + {{end}} + Edit + Delete +
    +

    + {{end}} + +
    +
    +
    + {{end}} + {{end}} +
    + + {{if .Book}} +
    + + +
    + {{end}} +{{end}} + +{{template "footer.html"}}