basket.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. s.Info(db, currency_id)
  25. return s.String()
  26. } else {
  27. return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
  28. }
  29. } else {
  30. return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
  31. }
  32. }
  33. func (this *Basket) Plus(host, session_id string, db *sqlw.DB, product_id int) string {
  34. if host == "" || session_id == "" {
  35. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  36. }
  37. this.Lock()
  38. defer this.Unlock()
  39. if h, ok := this.hosts[host]; ok == true {
  40. if s, ok := h.sessions[session_id]; ok == true {
  41. s.Plus(db, product_id)
  42. }
  43. } else {
  44. s := &session{}
  45. s.Products = map[int]*product{}
  46. s.Plus(db, product_id)
  47. h := &onehost{}
  48. h.sessions = map[string]*session{}
  49. h.sessions[session_id] = s
  50. this.hosts[host] = h
  51. }
  52. return (&dResponse{IsError: false, Msg: "basket_product_plus", Message: ""}).String()
  53. }
  54. func (this *Basket) Minus(host, session_id string, db *sqlw.DB, product_id int) string {
  55. if host == "" || session_id == "" {
  56. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  57. }
  58. this.Lock()
  59. defer this.Unlock()
  60. if h, ok := this.hosts[host]; ok == true {
  61. if s, ok := h.sessions[session_id]; ok == true {
  62. s.Minus(db, product_id)
  63. }
  64. }
  65. return (&dResponse{IsError: false, Msg: "basket_product_minus", Message: ""}).String()
  66. }
  67. func (this *Basket) Remove(host, session_id string, db *sqlw.DB, product_id int) string {
  68. if host == "" || session_id == "" {
  69. return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
  70. }
  71. this.Lock()
  72. defer this.Unlock()
  73. if h, ok := this.hosts[host]; ok == true {
  74. if s, ok := h.sessions[session_id]; ok == true {
  75. s.Remove(db, product_id)
  76. }
  77. }
  78. return (&dResponse{IsError: false, Msg: "basket_product_remove", Message: ""}).String()
  79. }