basket.go 2.7 KB

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