2012-08-18 02:06:43 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-08-30 13:17:50 -05:00
|
|
|
log "github.com/cihub/seelog"
|
|
|
|
|
2012-08-18 02:06:43 +02:00
|
|
|
"net/http"
|
2012-10-31 10:24:17 +01:00
|
|
|
"strconv"
|
2012-12-05 15:23:49 +01:00
|
|
|
"strings"
|
2014-08-30 01:25:16 -05:00
|
|
|
|
|
|
|
"git.gitorious.org/trantor/trantor.git/database"
|
|
|
|
"github.com/gorilla/mux"
|
2012-08-18 02:06:43 +02:00
|
|
|
)
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func deleteHandler(h handler) {
|
|
|
|
if !h.sess.IsAdmin() {
|
|
|
|
notFound(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
return
|
|
|
|
}
|
2012-08-18 02:06:43 +02:00
|
|
|
|
2012-09-13 00:05:21 +02:00
|
|
|
var titles []string
|
|
|
|
var isNew bool
|
2013-09-23 16:27:31 +02:00
|
|
|
ids := strings.Split(mux.Vars(h.r)["ids"], "/")
|
2014-07-02 20:40:24 -05:00
|
|
|
for _, id := range ids {
|
2014-08-31 17:19:48 -05:00
|
|
|
if id == "" {
|
|
|
|
continue
|
|
|
|
}
|
2014-07-02 20:40:24 -05:00
|
|
|
book, err := h.db.GetBookId(id)
|
2012-09-13 00:05:21 +02:00
|
|
|
if err != nil {
|
2014-07-02 20:40:24 -05:00
|
|
|
h.sess.Notify("Book not found!", "The book with id '"+id+"' is not there", "error")
|
2012-10-29 19:17:38 +01:00
|
|
|
continue
|
2012-09-13 00:05:21 +02:00
|
|
|
}
|
2014-08-21 19:24:23 -05:00
|
|
|
h.store.Delete(id)
|
2014-07-02 21:09:41 -05:00
|
|
|
h.db.DeleteBook(id)
|
2012-09-13 00:05:21 +02:00
|
|
|
|
2012-09-14 00:34:13 +02:00
|
|
|
if !book.Active {
|
2012-09-13 00:05:21 +02:00
|
|
|
isNew = true
|
|
|
|
}
|
|
|
|
titles = append(titles, book.Title)
|
2012-09-12 00:19:19 +02:00
|
|
|
}
|
2012-10-29 19:17:38 +01:00
|
|
|
if titles != nil {
|
2013-09-23 16:27:31 +02:00
|
|
|
h.sess.Notify("Removed books!", "The books "+strings.Join(titles, ", ")+" are completly removed", "success")
|
2012-10-29 19:17:38 +01:00
|
|
|
}
|
2013-09-23 16:27:31 +02:00
|
|
|
h.sess.Save(h.w, h.r)
|
2012-09-13 00:05:21 +02:00
|
|
|
if isNew {
|
2013-09-23 16:27:31 +02:00
|
|
|
http.Redirect(h.w, h.r, "/new/", http.StatusFound)
|
2012-09-13 00:05:21 +02:00
|
|
|
} else {
|
2013-09-23 16:27:31 +02:00
|
|
|
http.Redirect(h.w, h.r, "/", http.StatusFound)
|
2012-09-12 21:47:43 +02:00
|
|
|
}
|
2012-08-18 02:06:43 +02:00
|
|
|
}
|
2012-08-19 15:58:37 +02:00
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func editHandler(h handler) {
|
2014-07-02 20:40:24 -05:00
|
|
|
id := mux.Vars(h.r)["id"]
|
|
|
|
if !h.sess.IsAdmin() {
|
2013-09-23 16:27:31 +02:00
|
|
|
notFound(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
return
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
2014-07-02 20:40:24 -05:00
|
|
|
book, err := h.db.GetBookId(id)
|
2012-09-12 00:19:19 +02:00
|
|
|
if err != nil {
|
2013-09-23 16:27:31 +02:00
|
|
|
notFound(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var data bookData
|
2014-07-02 20:40:24 -05:00
|
|
|
data.Book = book
|
2013-09-23 16:27:31 +02:00
|
|
|
data.S = GetStatus(h)
|
2014-08-30 01:25:16 -05:00
|
|
|
loadTemplate(h, "edit", data)
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func cleanEmptyStr(s []string) []string {
|
|
|
|
var res []string
|
|
|
|
for _, v := range s {
|
|
|
|
if v != "" {
|
|
|
|
res = append(res, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func saveHandler(h handler) {
|
2014-07-02 21:09:41 -05:00
|
|
|
id := mux.Vars(h.r)["id"]
|
|
|
|
if !h.sess.IsAdmin() {
|
2013-09-23 16:27:31 +02:00
|
|
|
notFound(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
return
|
|
|
|
}
|
2012-08-19 15:58:37 +02:00
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
title := h.r.FormValue("title")
|
|
|
|
publisher := h.r.FormValue("publisher")
|
|
|
|
date := h.r.FormValue("date")
|
|
|
|
description := h.r.FormValue("description")
|
|
|
|
author := cleanEmptyStr(h.r.Form["author"])
|
|
|
|
subject := cleanEmptyStr(h.r.Form["subject"])
|
|
|
|
lang := cleanEmptyStr(h.r.Form["lang"])
|
2012-09-12 00:19:19 +02:00
|
|
|
book := map[string]interface{}{"title": title,
|
|
|
|
"publisher": publisher,
|
|
|
|
"date": date,
|
|
|
|
"description": description,
|
|
|
|
"author": author,
|
|
|
|
"subject": subject,
|
|
|
|
"lang": lang}
|
2013-09-23 16:27:31 +02:00
|
|
|
err := h.db.UpdateBook(id, book)
|
2012-09-12 00:19:19 +02:00
|
|
|
if err != nil {
|
2013-09-23 16:27:31 +02:00
|
|
|
notFound(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
return
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
2012-09-12 00:19:19 +02:00
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
h.sess.Notify("Book Modified!", "", "success")
|
|
|
|
h.sess.Save(h.w, h.r)
|
2014-08-21 19:24:23 -05:00
|
|
|
if h.db.IsBookActive(id) {
|
2014-07-02 21:09:41 -05:00
|
|
|
http.Redirect(h.w, h.r, "/book/"+id, http.StatusFound)
|
2012-09-12 21:47:43 +02:00
|
|
|
} else {
|
2013-09-23 16:27:31 +02:00
|
|
|
http.Redirect(h.w, h.r, "/new/", http.StatusFound)
|
2012-09-12 21:47:43 +02:00
|
|
|
}
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|
|
|
|
|
2012-09-09 22:42:03 +02:00
|
|
|
type newBook struct {
|
2012-09-14 00:34:13 +02:00
|
|
|
TitleFound int
|
2012-09-09 22:42:03 +02:00
|
|
|
AuthorFound int
|
2014-06-29 19:41:29 -05:00
|
|
|
B database.Book
|
2012-09-09 22:42:03 +02:00
|
|
|
}
|
2012-08-20 14:25:18 +02:00
|
|
|
type newData struct {
|
|
|
|
S Status
|
|
|
|
Found int
|
2012-09-09 22:42:03 +02:00
|
|
|
Books []newBook
|
2012-10-31 10:24:17 +01:00
|
|
|
Page int
|
|
|
|
Next string
|
|
|
|
Prev string
|
2012-08-20 14:25:18 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func newHandler(h handler) {
|
|
|
|
if !h.sess.IsAdmin() {
|
|
|
|
notFound(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
return
|
|
|
|
}
|
2012-08-25 16:41:54 +02:00
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
err := h.r.ParseForm()
|
2012-10-31 10:37:21 +01:00
|
|
|
if err != nil {
|
2013-09-23 16:27:31 +02:00
|
|
|
http.Error(h.w, err.Error(), http.StatusInternalServerError)
|
2012-10-31 10:37:21 +01:00
|
|
|
return
|
|
|
|
}
|
2012-10-31 10:24:17 +01:00
|
|
|
page := 0
|
2013-09-23 16:27:31 +02:00
|
|
|
if len(h.r.Form["p"]) != 0 {
|
|
|
|
page, err = strconv.Atoi(h.r.Form["p"][0])
|
2012-10-31 10:24:17 +01:00
|
|
|
if err != nil {
|
|
|
|
page = 0
|
|
|
|
}
|
|
|
|
}
|
2013-09-23 16:27:31 +02:00
|
|
|
res, num, _ := h.db.GetNewBooks(NEW_ITEMS_PAGE, page*NEW_ITEMS_PAGE)
|
2012-10-31 10:24:17 +01:00
|
|
|
|
2012-09-12 00:19:19 +02:00
|
|
|
var data newData
|
2013-09-23 16:27:31 +02:00
|
|
|
data.S = GetStatus(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
data.Found = num
|
2012-10-31 10:37:21 +01:00
|
|
|
if num-NEW_ITEMS_PAGE*page < NEW_ITEMS_PAGE {
|
|
|
|
data.Books = make([]newBook, num-NEW_ITEMS_PAGE*page)
|
|
|
|
} else {
|
|
|
|
data.Books = make([]newBook, NEW_ITEMS_PAGE)
|
|
|
|
}
|
2012-09-12 00:19:19 +02:00
|
|
|
for i, b := range res {
|
|
|
|
data.Books[i].B = b
|
2014-07-02 20:58:00 -05:00
|
|
|
_, data.Books[i].TitleFound, _ = h.db.GetBooks("title:"+b.Title, 1, 0)
|
|
|
|
_, data.Books[i].AuthorFound, _ = h.db.GetBooks("author:"+strings.Join(b.Author, " author:"), 1, 0)
|
2012-08-20 14:25:18 +02:00
|
|
|
}
|
2012-12-05 15:23:49 +01:00
|
|
|
data.Page = page + 1
|
2012-10-31 10:24:17 +01:00
|
|
|
if num > (page+1)*NEW_ITEMS_PAGE {
|
|
|
|
data.Next = "/new/?p=" + strconv.Itoa(page+1)
|
|
|
|
}
|
|
|
|
if page > 0 {
|
|
|
|
data.Prev = "/new/?p=" + strconv.Itoa(page-1)
|
|
|
|
}
|
2014-08-30 01:25:16 -05:00
|
|
|
loadTemplate(h, "new", data)
|
2012-08-20 14:25:18 +02:00
|
|
|
}
|
|
|
|
|
2013-09-23 16:27:31 +02:00
|
|
|
func storeHandler(h handler) {
|
|
|
|
if !h.sess.IsAdmin() {
|
|
|
|
notFound(h)
|
2012-09-12 00:19:19 +02:00
|
|
|
return
|
|
|
|
}
|
2012-08-20 14:25:18 +02:00
|
|
|
|
2012-09-13 00:05:21 +02:00
|
|
|
var titles []string
|
2013-09-23 16:27:31 +02:00
|
|
|
ids := strings.Split(mux.Vars(h.r)["ids"], "/")
|
2014-07-02 20:40:24 -05:00
|
|
|
for _, id := range ids {
|
2014-08-21 19:24:23 -05:00
|
|
|
if id == "" {
|
|
|
|
continue
|
|
|
|
}
|
2014-07-02 20:40:24 -05:00
|
|
|
book, err := h.db.GetBookId(id)
|
2012-09-13 00:05:21 +02:00
|
|
|
if err != nil {
|
2014-07-02 20:40:24 -05:00
|
|
|
h.sess.Notify("Book not found!", "The book with id '"+id+"' is not there", "error")
|
2012-10-29 19:17:38 +01:00
|
|
|
continue
|
2012-09-13 00:05:21 +02:00
|
|
|
}
|
2014-08-21 19:24:23 -05:00
|
|
|
if err != nil {
|
|
|
|
h.sess.Notify("An error ocurred!", err.Error(), "error")
|
|
|
|
log.Error("Error getting book for storing '", book.Title, "': ", err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
err = h.db.ActiveBook(id)
|
2012-10-28 17:22:23 +01:00
|
|
|
if err != nil {
|
2013-09-23 16:27:31 +02:00
|
|
|
h.sess.Notify("An error ocurred!", err.Error(), "error")
|
2014-02-11 11:20:38 +01:00
|
|
|
log.Error("Error storing book '", book.Title, "': ", err.Error())
|
2012-10-29 19:17:38 +01:00
|
|
|
continue
|
2012-10-28 17:22:23 +01:00
|
|
|
}
|
2012-09-13 00:05:21 +02:00
|
|
|
titles = append(titles, book.Title)
|
|
|
|
}
|
2012-10-29 19:17:38 +01:00
|
|
|
if titles != nil {
|
2013-09-23 16:27:31 +02:00
|
|
|
h.sess.Notify("Store books!", "The books '"+strings.Join(titles, ", ")+"' are stored for public download", "success")
|
2012-10-29 19:17:38 +01:00
|
|
|
}
|
2013-09-23 16:27:31 +02:00
|
|
|
h.sess.Save(h.w, h.r)
|
|
|
|
http.Redirect(h.w, h.r, "/new/", http.StatusFound)
|
2012-08-19 15:58:37 +02:00
|
|
|
}
|