Advertisement
krovn

Untitled

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