Added edit page to books

This commit is contained in:
Las Zenow 2012-08-19 15:58:37 +02:00
parent e68f41445e
commit a0d4acdf89
5 changed files with 176 additions and 9 deletions

View file

@ -15,12 +15,13 @@ func deleteHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request
return
}
var id bson.ObjectId = bson.ObjectIdHex(r.URL.Path[len("/delete/"):])
var book Book
if coll.Find(bson.M{"_id": id}).One(&book) != nil {
id := bson.ObjectIdHex(r.URL.Path[len("/delete/"):])
books, err := GetBook(coll, bson.M{"_id": id})
if err != nil {
http.NotFound(w, r)
return
}
book := books[0]
os.RemoveAll(book.Path)
os.RemoveAll(book.Cover[1:])
os.RemoveAll(book.CoverSmall[1:])
@ -30,3 +31,82 @@ func deleteHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request
http.Redirect(w, r, "/", 307)
}
}
func editHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
id := bson.ObjectIdHex(r.URL.Path[len("/edit/"):])
books, err := GetBook(coll, bson.M{"_id": id})
if err != nil {
http.NotFound(w, r)
return
}
var data bookData
data.Book = books[0]
data.S = GetStatus(w, r)
loadTemplate(w, "edit", data)
}
}
func cleanEmptyStr(s []string) []string {
var res []string
for _, v := range s {
if v != "" {
res = append(res, v)
}
}
return res
}
func saveHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.NotFound(w, r)
return
}
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
id := bson.ObjectIdHex(r.URL.Path[len("/save/"):])
title := r.FormValue("title")
publisher := r.FormValue("publisher")
date := r.FormValue("date")
description := r.FormValue("description")
author := cleanEmptyStr(r.Form["author"])
subject := cleanEmptyStr(r.Form["subject"])
lang := cleanEmptyStr(r.Form["lang"])
err := coll.Update(bson.M{"_id": id}, bson.M{"$set": bson.M{"title": title,
"publisher": publisher,
"date": date,
"description": description,
"author": author,
"subject": subject,
"lang": lang}})
if err != nil {
http.NotFound(w, r)
return
}
sess.Notify("Book Modified!", "", "success")
sess.Save(w, r)
http.Redirect(w, r, "/book/"+title, 307)
}
}
func newHandler(coll *mgo.Collection) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
sess := GetSession(r)
if sess.User == "" {
http.NotFound(w, r)
return
}
}
}

View file

@ -35,7 +35,9 @@ func loadTemplate(w http.ResponseWriter, tmpl string, data interface{}) {
TEMPLATE_DIR+"about.html",
TEMPLATE_DIR+"book.html",
TEMPLATE_DIR+"search.html",
TEMPLATE_DIR+"upload.html"))
TEMPLATE_DIR+"upload.html",
TEMPLATE_DIR+"edit.html",
))
err := templates.ExecuteTemplate(w, tmpl+".html", data)
if err != nil {

79
templates/edit.html Normal file
View file

@ -0,0 +1,79 @@
{{template "header.html" .S}}
{{with .Book}}
<div class="row">
{{if .Cover}}
<div class="span4">
<img src="{{.Cover}}" alt="{{.Title}}" class="pull-right" />
</div>
{{end}}
<div class="span8">
<form class="form-horizontal" method="POST" action="/save/{{.Id}}">
<fieldset>
<div class="control-group">
<label class="control-label" for="title">Title</label>
<div class="controls">
<input style="height:28px;" class="input-xlarge" type="text" id="title" value="{{.Title}}" name="title">
</div>
</div>
</fieldset>
<fieldset>
<div class="control-group">
<label class="control-label" for="author">Author</label>
<div class="controls">
{{range .Author}}
<input style="height:28px;" class="input-xlarge" type="text" id="author" value="{{.}}" name="author">
{{end}}
<input style="height:28px;" class="input-xlarge" type="text" id="author" placeholder="Add author" name="author">
</div>
</div>
<div class="control-group">
<label class="control-label" for="publisher">Publisher</label>
<div class="controls">
<input style="height:28px;" class="input-xlarge" type="text" id="publisher" value="{{.Publisher}}" name="publisher">
</div>
</div>
<div class="control-group">
<label class="control-label" for="tags">Tags</label>
<div class="controls">
{{range .Subject}}
<input style="height:28px;" class="input-xlarge" type="text" id="tags" value="{{.}}" name="subject">
{{end}}
<input style="height:28px;" class="input-xlarge" type="text" id="tags" placeholder="Add tags" name="subject">
</div>
</div>
<div class="control-group">
<label class="control-label" for="date">Date</label>
<div class="controls">
<input style="height:28px;" class="input-xlarge" type="text" id="date" value="{{.Date}}" name="date">
</div>
</div>
<div class="control-group">
<label class="control-label" for="langs">Langs</label>
<div class="controls">
{{range .Lang}}
<input style="height:28px;" class="input-xlarge" type="text" id="langs" value="{{.}}" name="lang">
{{end}}
<input style="height:28px;" class="input-xlarge" type="text" id="langs" placeholder="Add langs" name="lang">
</div>
</div>
</fieldset>
<fieldset>
<div class="control-group">
<label class="control-label" for="description">Description</label>
<div class="controls">
<textarea class="input-xlarge" id="description" rows="5" name="description">{{.Description}}</textarea>
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Save</button>
<a href="/book/{{.Title}}" class="btn">Cancel</a>
</div>
</fieldset>
</form>
</div>
</div>
{{end}}
{{template "footer.html"}}

View file

@ -62,6 +62,7 @@
<i class="icon-user icon-white"></i> {{.User}}<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<li><a href="/new/"><i class="icon-book"></i> New books</a></li>
<li class="divider"></li>
<li><a href="/logout/"><i class="icon-off"></i> Log Out</a></li>
</ul>

View file

@ -8,11 +8,12 @@ import (
)
const (
IP = "127.0.0.1"
DB_NAME = "trantor"
BOOKS_COLL = "books"
USERS_COLL = "users"
PASS_SALT = "ImperialLibSalt"
IP = "127.0.0.1"
DB_NAME = "trantor"
BOOKS_COLL = "books"
NEW_BOOKS_COLL = "new"
USERS_COLL = "users"
PASS_SALT = "ImperialLibSalt"
)
type aboutData struct {
@ -103,12 +104,16 @@ func main() {
defer session.Close()
coll := session.DB(DB_NAME).C(BOOKS_COLL)
userColl := session.DB(DB_NAME).C(USERS_COLL)
newColl := session.DB(DB_NAME).C(NEW_BOOKS_COLL)
http.HandleFunc("/book/", bookHandler(coll))
http.HandleFunc("/search/", searchHandler(coll))
http.HandleFunc("/upload/", uploadHandler(coll))
http.HandleFunc("/login/", loginHandler(userColl))
http.HandleFunc("/logout/", logoutHandler)
http.HandleFunc("/new/", newHandler(newColl))
http.HandleFunc("/edit/", editHandler(coll))
http.HandleFunc("/save/", saveHandler(coll))
http.HandleFunc("/delete/", deleteHandler(coll))
http.HandleFunc("/about/", aboutHandler)
fileHandler("/img/")