session.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package basket
  2. import (
  3. "encoding/json"
  4. "golang-fave/engine/sqlw"
  5. )
  6. type session struct {
  7. Products map[int]*product `json:"products"`
  8. Currency *currency `json:"currency"`
  9. Total float64 `json:"total"`
  10. }
  11. func (this *session) String() string {
  12. json, err := json.Marshal(this)
  13. if err != nil {
  14. return `{"msg":"basket_engine_error","message":"` + err.Error() + `"}`
  15. }
  16. return string(json)
  17. }
  18. func (this *session) Info(db *sqlw.DB, currency_id int) {
  19. // Update prices
  20. // Update total
  21. }
  22. func (this *session) Plus(db *sqlw.DB, product_id int) {
  23. if p, ok := this.Products[product_id]; ok == true {
  24. p.Quantity++
  25. return
  26. }
  27. // Check and insert
  28. // Load from DB
  29. }
  30. func (this *session) Minus(db *sqlw.DB, product_id int) {
  31. if p, ok := this.Products[product_id]; ok == true {
  32. if p.Quantity > 1 {
  33. p.Quantity--
  34. } else {
  35. delete(this.Products, product_id)
  36. }
  37. }
  38. }
  39. func (this *session) Remove(db *sqlw.DB, product_id int) {
  40. if _, ok := this.Products[product_id]; ok == true {
  41. delete(this.Products, product_id)
  42. }
  43. }