Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "html/template"
- "log"
- "net/http"
- "strconv"
- _ "github.com/lib/pq"
- uuid "github.com/satori/go.uuid"
- mgo "gopkg.in/mgo.v2"
- "gopkg.in/mgo.v2/bson"
- )
- var db *mgo.Database
- var books *mgo.Collection
- var tpl *template.Template
- func init() {
- // get a mongo sessions
- //s, err := mgo.Dial("mongodb://krovns:nauda100@localhost/bookstore")
- s, err := mgo.Dial("mongodb://localhost/bookstore")
- if err != nil {
- panic(err)
- }
- if err = s.Ping(); err != nil {
- panic(err)
- }
- db = s.DB("bookstore")
- books = db.C("books")
- fmt.Println("connected to mongodb")
- tpl = template.Must(template.ParseGlob("booktemplate/*.gohtml"))
- }
- type Book struct {
- // add ID and tags if you need them
- // ID bson.ObjectId // `json:"id" bson:"_id"`
- Bid string `json:"Bid" bson:"Bid"`
- Title string `json:"Title" bson:"Title"`
- Author string `json:"Author" bson:"Author"`
- Price float32 `json:"Price" bson:"Price"`
- }
- func main() {
- http.HandleFunc("/", index)
- http.HandleFunc("/show", show)
- http.HandleFunc("/create", create)
- http.HandleFunc("/createp", createp)
- http.HandleFunc("/update", update)
- http.HandleFunc("/updatep", updatep)
- http.HandleFunc("/deletep", deletep)
- http.Handle("/favicon.ico", http.NotFoundHandler())
- http.ListenAndServe(":8080", nil)
- }
- func index(w http.ResponseWriter, req *http.Request) {
- if req.Method != "GET" {
- http.Error(w, http.StatusText(405), 405)
- return
- }
- bks := []Book{}
- err := books.Find(bson.M{}).All(&bks)
- if err != nil {
- http.Error(w, http.StatusText(500), 500)
- return
- }
- tpl.ExecuteTemplate(w, "books.gohtml", bks)
- }
- func show(w http.ResponseWriter, req *http.Request) {
- if req.Method != "GET" {
- http.Error(w, http.StatusText(405), 405)
- return
- }
- bk := Book{}
- bid := req.FormValue("bid")
- if bid == "" {
- http.Error(w, http.StatusText(400), 400)
- return
- }
- err := books.Find(bson.M{"Bid": bid}).One(&bk)
- if err != nil {
- http.Error(w, http.StatusText(500), 500)
- return
- }
- tpl.ExecuteTemplate(w, "show.gohtml", bk)
- }
- func create(w http.ResponseWriter, req *http.Request) {
- tpl.ExecuteTemplate(w, "create.gohtml", nil)
- }
- func createp(w http.ResponseWriter, req *http.Request) {
- if req.Method != "POST" {
- http.Error(w, http.StatusText(405), 405)
- return
- }
- bk := Book{}
- sID, err := uuid.NewV4()
- if err != nil {
- log.Fatalln(err)
- }
- bk.Bid = sID.String()
- bk.Title = req.FormValue("title")
- bk.Author = req.FormValue("author")
- p := req.FormValue("price")
- if bk.Title == "" || bk.Author == "" || p == "" {
- http.Error(w, http.StatusText(400), 400)
- return
- }
- f64, err := strconv.ParseFloat(p, 32)
- if err != nil {
- http.Error(w, http.StatusText(406)+"ievadiet summu", 406)
- return
- }
- bk.Price = float32(f64)
- err = books.Insert(bk)
- if err != nil {
- http.Error(w, http.StatusText(500), 500)
- return
- }
- tpl.ExecuteTemplate(w, "created.gohtml", bk)
- }
- func update(w http.ResponseWriter, req *http.Request) {
- if req.Method != "GET" {
- http.Error(w, http.StatusText(405), 405)
- return
- }
- bk := Book{}
- bid := req.FormValue("bid")
- if bid == "" {
- http.Error(w, http.StatusText(400), 400)
- return
- }
- err := books.Find(bson.M{"Bid": bid}).One(&bk)
- if err != nil {
- http.Error(w, http.StatusText(500), 500)
- return
- }
- tpl.ExecuteTemplate(w, "update.gohtml", bk)
- }
- func updatep(w http.ResponseWriter, req *http.Request) {
- if req.Method != "POST" {
- http.Error(w, http.StatusText(405), 405)
- return
- }
- bk := Book{}
- bk.Bid = req.FormValue("bid")
- bk.Title = req.FormValue("title")
- bk.Author = req.FormValue("author")
- p := req.FormValue("price")
- if bk.Bid == "" || bk.Title == "" || bk.Author == "" || p == "" {
- http.Error(w, http.StatusText(400), 400)
- return
- }
- f64, err := strconv.ParseFloat(p, 32)
- if err != nil {
- http.Error(w, http.StatusText(406)+"ievadiet summu", 406)
- return
- }
- bk.Price = float32(f64)
- err = books.Update(bson.M{"Bid": bk.Bid}, &bk)
- if err != nil {
- http.Error(w, http.StatusText(500), 500)
- fmt.Println(err)
- return
- }
- tpl.ExecuteTemplate(w, "updated.gohtml", bk)
- }
- func deletep(w http.ResponseWriter, req *http.Request) {
- if req.Method != "GET" {
- http.Error(w, http.StatusText(405), 405)
- return
- }
- bid := req.FormValue("bid")
- if bid == "" {
- http.Error(w, http.StatusText(400), 400)
- return
- }
- err := books.Remove(bson.M{"Bid": bid})
- if err != nil {
- http.Error(w, http.StatusText(500), 500)
- return
- }
- http.Redirect(w, req, "/", 303)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement