Advertisement
krovn

Untitled

Nov 9th, 2019
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 5.27 KB | None | 0 0
  1. Use bookstore
  2.  
  3. [{"Bid":"8123kd28123 ","Title":"Pirma gramata","Author":"Martins","Price":8.99},{"Bid":"e75061b1-76d7-4480-9d24-9922744f80c5 ","Title":"The Princes","Author":"Janiss","Price":9.94},{"Bid":"94e75979-2e2b-47c2-b72b-90a27a385488 ","Title":"PoterisTresais","Author":"Autors2","Price":19},{"Bid":"3dd67062-c532-4d8b-808a-d18c62d87763 ","Title":"The Princes","Author":"Edgarins","Price":9.44},{"Bid":"15dd57c8-6795-4722-962d-feb8fb871219 ","Title":"Ziemassvetki","Author":"Zigmars","Price":5.89}]
  4.  
  5. db.createUser({user:"krovns",pwd:"nauda100",roles:[{role:"readWrite",db:"bookstore"}]})
  6.  
  7. mongo -u "krovns" -p "nauda100" --authenticationDatabase "bookstore"
  8.  
  9. Test if you can do something in bookstore
  10. ==============================================================
  11. package main
  12.  
  13. import (
  14.     "fmt"
  15.     "html/template"
  16.     "log"
  17.     "net/http"
  18.     "strconv"
  19.  
  20.     _ "github.com/lib/pq"
  21.     uuid "github.com/satori/go.uuid"
  22.     mgo "gopkg.in/mgo.v2"
  23.     "gopkg.in/mgo.v2/bson"
  24. )
  25.  
  26. var db *mgo.Database
  27. var books *mgo.Collection
  28. var tpl *template.Template
  29.  
  30. func init() {
  31.     // get a mongo sessions
  32.     //s, err := mgo.Dial("mongodb://krovns:nauda100@localhost/bookstore")
  33.     s, err := mgo.Dial("mongodb://localhost/bookstore")
  34.     if err != nil {
  35.         panic(err)
  36.     }
  37.     if err = s.Ping(); err != nil {
  38.         panic(err)
  39.     }
  40.     db = s.DB("bookstore")
  41.     books = db.C("books")
  42.     fmt.Println("connected to mongodb")
  43.     tpl = template.Must(template.ParseGlob("booktemplate/*.gohtml"))
  44. }
  45.  
  46. type Book struct {
  47.     // add ID and tags if you need them
  48.     // ID bson.ObjectId // `json:"id" bson:"_id"`
  49.     Bid    string  `json:"Bid" bson:"Bid"`
  50.     Title  string  `json:"Title" bson:"Title"`
  51.     Author string  `json:"Author" bson:"Author"`
  52.     Price  float32 `json:"Price" bson:"Price"`
  53. }
  54.  
  55. func main() {
  56.     http.HandleFunc("/", index)
  57.     http.HandleFunc("/show", show)
  58.     http.HandleFunc("/create", create)
  59.     http.HandleFunc("/createp", createp)
  60.     http.HandleFunc("/update", update)
  61.     http.HandleFunc("/updatep", updatep)
  62.     http.HandleFunc("/deletep", deletep)
  63.     http.Handle("/favicon.ico", http.NotFoundHandler())
  64.     http.ListenAndServe(":8080", nil)
  65. }
  66.  
  67. func index(w http.ResponseWriter, req *http.Request) {
  68.     if req.Method != "GET" {
  69.         http.Error(w, http.StatusText(405), 405)
  70.         return
  71.     }
  72.     bks := []Book{}
  73.     err := books.Find(bson.M{}).All(&bks)
  74.     if err != nil {
  75.         http.Error(w, http.StatusText(500), 500)
  76.         return
  77.     }
  78.     tpl.ExecuteTemplate(w, "books.gohtml", bks)
  79. }
  80.  
  81. func show(w http.ResponseWriter, req *http.Request) {
  82.     if req.Method != "GET" {
  83.         http.Error(w, http.StatusText(405), 405)
  84.         return
  85.     }
  86.     bk := Book{}
  87.     bid := req.FormValue("bid")
  88.     if bid == "" {
  89.         http.Error(w, http.StatusText(400), 400)
  90.         return
  91.     }
  92.     err := books.Find(bson.M{"Bid": bid}).One(&bk)
  93.     if err != nil {
  94.         http.Error(w, http.StatusText(500), 500)
  95.         return
  96.     }
  97.     tpl.ExecuteTemplate(w, "show.gohtml", bk)
  98. }
  99.  
  100. func create(w http.ResponseWriter, req *http.Request) {
  101.     tpl.ExecuteTemplate(w, "create.gohtml", nil)
  102. }
  103.  
  104. func createp(w http.ResponseWriter, req *http.Request) {
  105.     if req.Method != "POST" {
  106.         http.Error(w, http.StatusText(405), 405)
  107.         return
  108.     }
  109.     bk := Book{}
  110.     sID, err := uuid.NewV4()
  111.     if err != nil {
  112.         log.Fatalln(err)
  113.     }
  114.     bk.Bid = sID.String()
  115.     bk.Title = req.FormValue("title")
  116.     bk.Author = req.FormValue("author")
  117.     p := req.FormValue("price")
  118.     if bk.Title == "" || bk.Author == "" || p == "" {
  119.         http.Error(w, http.StatusText(400), 400)
  120.         return
  121.     }
  122.     f64, err := strconv.ParseFloat(p, 32)
  123.     if err != nil {
  124.         http.Error(w, http.StatusText(406)+"ievadiet summu", 406)
  125.         return
  126.     }
  127.     bk.Price = float32(f64)
  128.     err = books.Insert(bk)
  129.     if err != nil {
  130.         http.Error(w, http.StatusText(500), 500)
  131.         return
  132.     }
  133.     tpl.ExecuteTemplate(w, "created.gohtml", bk)
  134. }
  135.  
  136. func update(w http.ResponseWriter, req *http.Request) {
  137.     if req.Method != "GET" {
  138.         http.Error(w, http.StatusText(405), 405)
  139.         return
  140.     }
  141.     bk := Book{}
  142.     bid := req.FormValue("bid")
  143.     if bid == "" {
  144.         http.Error(w, http.StatusText(400), 400)
  145.         return
  146.     }
  147.     err := books.Find(bson.M{"Bid": bid}).One(&bk)
  148.     if err != nil {
  149.         http.Error(w, http.StatusText(500), 500)
  150.         return
  151.     }
  152.     tpl.ExecuteTemplate(w, "update.gohtml", bk)
  153. }
  154.  
  155. func updatep(w http.ResponseWriter, req *http.Request) {
  156.     if req.Method != "POST" {
  157.         http.Error(w, http.StatusText(405), 405)
  158.         return
  159.     }
  160.     bk := Book{}
  161.     bk.Bid = req.FormValue("bid")
  162.     bk.Title = req.FormValue("title")
  163.     bk.Author = req.FormValue("author")
  164.     p := req.FormValue("price")
  165.     if bk.Bid == "" || bk.Title == "" || bk.Author == "" || p == "" {
  166.         http.Error(w, http.StatusText(400), 400)
  167.         return
  168.     }
  169.     f64, err := strconv.ParseFloat(p, 32)
  170.     if err != nil {
  171.         http.Error(w, http.StatusText(406)+"ievadiet summu", 406)
  172.         return
  173.     }
  174.     bk.Price = float32(f64)
  175.     err = books.Update(bson.M{"Bid": bk.Bid}, &bk)
  176.     if err != nil {
  177.         http.Error(w, http.StatusText(500), 500)
  178.         fmt.Println(err)
  179.         return
  180.     }
  181.     tpl.ExecuteTemplate(w, "updated.gohtml", bk)
  182. }
  183.  
  184. func deletep(w http.ResponseWriter, req *http.Request) {
  185.     if req.Method != "GET" {
  186.         http.Error(w, http.StatusText(405), 405)
  187.         return
  188.     }
  189.     bid := req.FormValue("bid")
  190.     if bid == "" {
  191.         http.Error(w, http.StatusText(400), 400)
  192.         return
  193.     }
  194.     err := books.Remove(bson.M{"Bid": bid})
  195.     if err != nil {
  196.         http.Error(w, http.StatusText(500), 500)
  197.         return
  198.     }
  199.     http.Redirect(w, req, "/", 303)
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement