123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package basket
- import (
- "net/http"
- "sync"
- "golang-fave/engine/sqlw"
- )
- type Basket struct {
- sync.RWMutex
- hosts map[string]*onehost
- }
- func New() *Basket {
- b := Basket{}
- b.hosts = map[string]*onehost{}
- return &b
- }
- func (this *Basket) Info(r *http.Request, host, session_id string, db *sqlw.DB, currency_id int) string {
- if host == "" || session_id == "" {
- return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
- }
- // Load currency here
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[host]; ok == true {
- if s, ok := h.sessions[session_id]; ok == true {
- s.Preload(r, db)
- return s.String(db)
- } else {
- return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
- }
- } else {
- return (&dResponse{IsError: false, Msg: "basket_is_empty", Message: ""}).String()
- }
- }
- func (this *Basket) Plus(r *http.Request, host, session_id string, db *sqlw.DB, product_id int) string {
- if host == "" || session_id == "" {
- return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
- }
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[host]; ok == true {
- if s, ok := h.sessions[session_id]; ok == true {
- s.Preload(r, db)
- s.Plus(db, product_id)
- }
- } else {
- s := &session{}
- s.listCurrencies = map[int]*currency{}
- s.Products = map[int]*product{}
- s.Preload(r, db)
- s.Plus(db, product_id)
- h := &onehost{}
- h.sessions = map[string]*session{}
- h.sessions[session_id] = s
- this.hosts[host] = h
- }
- return (&dResponse{IsError: false, Msg: "basket_product_plus", Message: ""}).String()
- }
- func (this *Basket) Minus(r *http.Request, host, session_id string, db *sqlw.DB, product_id int) string {
- if host == "" || session_id == "" {
- return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
- }
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[host]; ok == true {
- if s, ok := h.sessions[session_id]; ok == true {
- s.Preload(r, db)
- s.Minus(db, product_id)
- }
- }
- return (&dResponse{IsError: false, Msg: "basket_product_minus", Message: ""}).String()
- }
- func (this *Basket) Remove(r *http.Request, host, session_id string, db *sqlw.DB, product_id int) string {
- if host == "" || session_id == "" {
- return (&dResponse{IsError: true, Msg: "basket_host_or_session_not_set", Message: ""}).String()
- }
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[host]; ok == true {
- if s, ok := h.sessions[session_id]; ok == true {
- s.Preload(r, db)
- s.Remove(db, product_id)
- }
- }
- return (&dResponse{IsError: false, Msg: "basket_product_remove", Message: ""}).String()
- }
- func (this *Basket) ProductsCount(r *http.Request, host, session_id string) int {
- if host != "" && session_id != "" {
- this.Lock()
- defer this.Unlock()
- if h, ok := this.hosts[host]; ok == true {
- if s, ok := h.sessions[session_id]; ok == true {
- return s.ProductsCount()
- }
- }
- }
- return 0
- }
|