basket.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package basket
  2. import (
  3. "net/http"
  4. "sync"
  5. "golang-fave/engine/sqlw"
  6. )
  7. type Basket struct {
  8. sync.RWMutex
  9. hosts map[string]*onehost
  10. }
  11. func New() *Basket {
  12. b := Basket{}
  13. b.hosts = map[string]*onehost{}
  14. return &b
  15. }
  16. func (this *Basket) Info(r *http.Request, host, session_id string, db *sqlw.DB, currency_id int) string {
  17. if host == "" || session_id == "" {
  18. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  19. }
  20. // Load currency here
  21. this.Lock()
  22. defer this.Unlock()
  23. if h, ok := this.hosts[host]; ok == true {
  24. if s, ok := h.sessions[session_id]; ok == true {
  25. s.Preload(r, db)
  26. return s.String(db)
  27. } else {
  28. return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
  29. }
  30. } else {
  31. return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
  32. }
  33. }
  34. func (this *Basket) Plus(r *http.Request, host, session_id string, db *sqlw.DB, product_id int) string {
  35. if host == "" || session_id == "" {
  36. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  37. }
  38. this.Lock()
  39. defer this.Unlock()
  40. if h, ok := this.hosts[host]; ok == true {
  41. if s, ok := h.sessions[session_id]; ok == true {
  42. s.Preload(r, db)
  43. s.Plus(db, product_id)
  44. }
  45. } else {
  46. s := &session{}
  47. s.listCurrencies = map[int]*currency{}
  48. s.Products = map[int]*product{}
  49. s.Preload(r, db)
  50. s.Plus(db, product_id)
  51. h := &onehost{}
  52. h.sessions = map[string]*session{}
  53. h.sessions[session_id] = s
  54. this.hosts[host] = h
  55. }
  56. return (&dResponse{IsError: false, Msg: "basket_product_plus", Message: ""}).String()
  57. }
  58. func (this *Basket) Minus(r *http.Request, host, session_id string, db *sqlw.DB, product_id int) string {
  59. if host == "" || session_id == "" {
  60. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  61. }
  62. this.Lock()
  63. defer this.Unlock()
  64. if h, ok := this.hosts[host]; ok == true {
  65. if s, ok := h.sessions[session_id]; ok == true {
  66. s.Preload(r, db)
  67. s.Minus(db, product_id)
  68. }
  69. }
  70. return (&dResponse{IsError: false, Msg: "basket_product_minus", Message: ""}).String()
  71. }
  72. func (this *Basket) Remove(r *http.Request, host, session_id string, db *sqlw.DB, product_id int) string {
  73. if host == "" || session_id == "" {
  74. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  75. }
  76. this.Lock()
  77. defer this.Unlock()
  78. if h, ok := this.hosts[host]; ok == true {
  79. if s, ok := h.sessions[session_id]; ok == true {
  80. s.Preload(r, db)
  81. s.Remove(db, product_id)
  82. }
  83. }
  84. return (&dResponse{IsError: false, Msg: "basket_product_remove", Message: ""}).String()
  85. }
  86. func (this *Basket) ProductsCount(r *http.Request, host, session_id string) int {
  87. if host != "" && session_id != "" {
  88. this.Lock()
  89. defer this.Unlock()
  90. if h, ok := this.hosts[host]; ok == true {
  91. if s, ok := h.sessions[session_id]; ok == true {
  92. return s.ProductsCount()
  93. }
  94. }
  95. }
  96. return 0
  97. }