basket.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. }