This repository has been archived on 2025-03-01. You can view files and clone it, but cannot push or open issues or pull requests.
trantor/lib/list.go

87 lines
2.1 KiB
Go
Raw Normal View History

package trantor
import (
"net/http"
"strings"
"gitlab.com/trantor/trantor/lib/database"
log "github.com/cihub/seelog"
"github.com/gorilla/mux"
)
func listHandler(h handler) {
listID := mux.Vars(h.r)["listID"]
list, err := h.db.GetBookList(listID)
if err != nil {
log.Error("Error loading list ", listID, ": ", err)
h.sess.Notify("Something went wrong!", "Could not load the list list", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
if list == nil {
notFound(h)
return
}
var data listData
data.S = GetStatus(h)
data.S.Title = list.Title + " -- " + data.S.Title
data.List = list
h.load("list", data)
}
type listData struct {
S Status
List *database.BookList
}
func listPostHandler(h handler) {
if h.sess.User == "" {
notFound(h)
return
}
listTitle := h.r.FormValue("list")
bookID := h.r.FormValue("book_id")
userLists, err := h.db.GetListsByUser(h.sess.User)
if err != nil {
log.Error("Error loading user (", h.sess.User, ") lists: ", err)
h.sess.Notify("Something went wrong!", "Could not add book to the list", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
var list *database.BookList
for _, l := range userLists {
if strings.ToLower(l.Title) == strings.ToLower(listTitle) {
list = &l
break
}
}
var listID string
if list == nil {
listID = genID()
err = h.db.NewBookList(listID, listTitle, h.sess.User, []string{})
if err != nil {
log.Error("Error creating list ", listTitle, " by user ", h.sess.User, ": ", err)
h.sess.Notify("Something went wrong!", "Could not add book to the list", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
} else {
listID = list.ListID
}
err = h.db.AddBookToList(listID, bookID)
if err != nil {
log.Error("Error adding book ", bookID, " to list ", listTitle, "(", listID, "): ", err)
h.sess.Notify("Something went wrong!", "Could not add book to the list", "error")
http.Redirect(h.w, h.r, h.r.Referer(), http.StatusFound)
return
}
http.Redirect(h.w, h.r, "/list/"+listID, http.StatusFound)
}