// GetAllCdnFiles Retrieves all CdnFiles func GetAllCdnFiles() (cs CdnFiles, err error) { data, err := redis.Get(AllCdnFilesRedisKey) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &cs) return } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return } defer db.Close() stmt, err := db.Prepare(getAllCdnFiles) if err != nil { return } defer stmt.Close() rows, err := stmt.Query() if err != nil { return } ch := make(chan CdnFiles) go populateCdns(rows, ch) cs = <-ch if len(cs) == 0 { err = sql.ErrNoRows return } go redis.Setex(AllCdnFilesRedisKey, cs, 86400) return }
// Retrieves a base video file. This does not grab all the associated channels or CDN files. func (v *Video) Get() error { redis_key := "video:" + strconv.Itoa(v.ID) data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &v) return err } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return err } defer db.Close() stmt, err := db.Prepare(getVideo) if err != nil { return err } defer stmt.Close() row := stmt.QueryRow(v.ID) ch := make(chan Video) go populateVideo(row, ch) *v = <-ch if v != nil { go redis.Setex(redis_key, *v, redis.CacheTimeout) } return err }
func (p *Price) Update() error { db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return err } defer db.Close() tx, err := db.Begin() if err != nil { return err } stmt, err := tx.Prepare(updatePrice) _, err = stmt.Exec(p.CustID, p.PartID, p.Price, p.IsSale, p.SaleStart, p.SaleEnd, p.ID) if err != nil { tx.Rollback() return err } err = tx.Commit() if err != nil { return err } go redis.Setex("price:"+strconv.Itoa(p.ID), p, 86400) go redis.Delete(fmt.Sprintf("prices:part:%d", strconv.Itoa(p.PartID))) go redis.Delete(fmt.Sprintf("customers:prices:%d", strconv.Itoa(p.CustID))) return nil }
func GetLeads(pageStr, resultsStr string, dtx *apicontext.DataContext) (pagination.Objects, error) { var err error var fs []interface{} var l pagination.Objects redis_key := "news:leads" + ":" + dtx.BrandString data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &l) return l, err } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return l, err } defer db.Close() stmt, err := db.Prepare(getLeads) if err != nil { return l, err } defer stmt.Close() res, err := stmt.Query(dtx.APIKey, dtx.BrandID, dtx.BrandID) for res.Next() { var f News res.Scan(&f.Lead) fs = append(fs, f) } defer res.Close() l = pagination.Paginate(pageStr, resultsStr, fs) go redis.Setex(redis_key, l, 86400) return l, err }
func GetAll(dtx *apicontext.DataContext) (Newses, error) { var fs Newses var err error redis_key := "news" + ":" + dtx.BrandString data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &fs) return fs, err } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return fs, err } defer db.Close() stmt, err := db.Prepare(getAll) if err != nil { return fs, err } defer stmt.Close() res, err := stmt.Query(dtx.APIKey, dtx.BrandID, dtx.BrandID) for res.Next() { n, err := scanItem(res) if err == nil { fs = append(fs, n) } } defer res.Close() go redis.Setex(redis_key, fs, 86400) return fs, nil }
// gets all of the web property notes - rarely called. func GetAllWebPropertyNotes(dtx *apicontext.DataContext) (WebPropertyNotes, error) { var ws WebPropertyNotes var err error redis_key := "webpropertynotes:" + dtx.BrandString data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &ws) if err == nil { return ws, err } } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return ws, err } defer db.Close() stmt, err := db.Prepare(getAllWebPropertyNotes) if err != nil { return ws, err } defer stmt.Close() res, err := stmt.Query(dtx.APIKey, dtx.BrandID, dtx.BrandID) for res.Next() { var w WebPropertyNote res.Scan(&w.ID, &w.WebPropID, &w.Text, &w.DateAdded) ws = append(ws, w) } go redis.Setex(redis_key, ws, 86400) return ws, err }
func GetAllPrices() (Prices, error) { var ps Prices var err error data, err := redis.Get(allPricesRedisKey) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &ps) return ps, err } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return ps, err } defer db.Close() stmt, err := db.Prepare(getPrices) if err != nil { return ps, err } defer stmt.Close() res, err := stmt.Query() for res.Next() { var p Price err = res.Scan(&p.ID, &p.CustID, &p.PartID, &p.Price, &p.IsSale, &p.SaleStart, &p.SaleEnd) ps = append(ps, p) } defer res.Close() go redis.Setex(allPricesRedisKey, ps, 86400) return ps, nil }
func (n *News) Get(dtx *apicontext.DataContext) error { var err error redis_key := "news:" + strconv.Itoa(n.ID) + ":" + dtx.BrandString data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &n) return err } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return err } defer db.Close() stmt, err := db.Prepare(getNews) if err != nil { return err } defer stmt.Close() row := stmt.QueryRow(n.ID, dtx.APIKey, dtx.BrandID, dtx.BrandID) item, err := scanItem(row) if err != nil { return err } n.copy(item) go redis.Setex(redis_key, n, 86400) return nil }
func (p *Price) Get() error { redis_key := "price:" + strconv.Itoa(p.ID) data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &p) return err } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return err } defer db.Close() stmt, err := db.Prepare(getPrice) if err != nil { return err } defer stmt.Close() err = stmt.QueryRow(p.ID).Scan(&p.ID, &p.CustID, &p.PartID, &p.Price, &p.IsSale, &p.SaleStart, &p.SaleEnd) if err != nil { return err } go redis.Setex(redis_key, p, 86400) return nil }
func GetPricesByPart(partID int) (Prices, error) { var ps Prices redis_key := "prices:part:" + strconv.Itoa(partID) data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &ps) return ps, err } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return ps, err } defer db.Close() stmt, err := db.Prepare(getPricesByPart) if err != nil { return ps, err } defer stmt.Close() res, err := stmt.Query(partID) for res.Next() { var p Price res.Scan(&p.ID, &p.CustID, &p.PartID, &p.Price, &p.IsSale, &p.SaleStart, &p.SaleEnd) ps = append(ps, p) } go redis.Setex(redis_key, ps, 86400) return ps, nil }
func (c *Customer) GetPricesByCustomer() (CustomerPrices, error) { var cps CustomerPrices redis_key := "customers:prices:" + strconv.Itoa(c.Id) data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &cps) return cps, err } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return cps, err } defer db.Close() stmt, err := db.Prepare(getPricesByCustomer) if err != nil { return cps, err } defer stmt.Close() res, err := stmt.Query(c.Id) for res.Next() { var p Price res.Scan(&p.ID, &p.CustID, &p.PartID, &p.Price, &p.IsSale, &p.SaleStart, &p.SaleEnd) cps.Prices = append(cps.Prices, p) } go redis.Setex(redis_key, cps, 86400) return cps, err }
// GetCdnFiles Gets all of the CdnFiles for the specific video. func (v *Video) GetCdnFiles() (cdns CdnFiles, err error) { redis_key := "video:cdnFiles:" + strconv.Itoa(v.ID) data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &cdns) return } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return } defer db.Close() stmt, err := db.Prepare(getVideoCdns) if err != nil { return } defer stmt.Close() rows, err := stmt.Query(v.ID) if err != nil { return } ch := make(chan CdnFiles) go populateCdns(rows, ch) cdns = <-ch if cdns != nil { go redis.Setex(redis_key, cdns, redis.CacheTimeout) } return }
func (p *Price) Create() error { db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return err } defer db.Close() tx, err := db.Begin() if err != nil { return err } stmt, err := tx.Prepare(createPrice) res, err := stmt.Exec(p.CustID, p.PartID, p.Price, p.IsSale, p.SaleStart, p.SaleEnd) if err != nil { tx.Rollback() return err } id, err := res.LastInsertId() p.ID = int(id) if err != nil { tx.Rollback() return err } err = tx.Commit() if err != nil { return err } go redis.Delete(allPricesRedisKey) go redis.Setex("price:"+strconv.Itoa(p.ID), p, 86400) return nil }
// Gets All Web Property Requirements func GetAllWebPropertyRequirements(dtx *apicontext.DataContext) (WebPropertyRequirements, error) { var ws WebPropertyRequirements var err error redis_key := "webpropertyrequirements:" + dtx.BrandString data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &ws) if err == nil { return ws, err } } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return ws, err } defer db.Close() stmt, err := db.Prepare(getAllWebPropertyRequirements) if err != nil { return ws, err } defer stmt.Close() res, err := stmt.Query(dtx.APIKey, dtx.BrandID, dtx.BrandID) for res.Next() { var w WebPropertyRequirement var reqType, req *string var comp *bool var wpid *int err = res.Scan( &w.ID, &w.RequirementID, &reqType, &req, &comp, &wpid, ) if err != nil { return ws, err } if reqType != nil { w.ReqType = *reqType } if req != nil { w.Requirement = *req } if comp != nil { w.Compliance = *comp } if wpid != nil { w.WebPropID = *wpid } ws = append(ws, w) } go redis.Setex(redis_key, ws, 86400) return ws, err }
func (p *Part) GetVideos(dtx *apicontext.DataContext) error { redis_key := fmt.Sprintf("part:%d:videos:%s", p.ID, dtx.BrandString) data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { if err = json.Unmarshal(data, &p.Videos); err != nil { return nil } } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return err } defer db.Close() qry, err := db.Prepare(partVideoStmt) if err != nil { return err } defer qry.Close() rows, err := qry.Query(p.ID) if err != nil { return err } var videos []PartVideo for rows.Next() { var v PartVideo var icon *string err = rows.Scan( &v.YouTubeVideoId, &v.Type, &v.IsPrimary, &icon) if err != nil { continue } v.TypeIcon, _ = url.Parse(*icon) videos = append(videos, v) } defer rows.Close() go redis.Setex(redis_key, p.Videos, redis.CacheTimeout) return nil }
// GetAllCdnFileTypes Retrieves all CdnFileTypes func GetAllCdnFileTypes() (cts []CdnFileType, err error) { data, err := redis.Get(AllCdnFileTypeRedisKey) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &cts) return } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return } defer db.Close() stmt, err := db.Prepare(getAllCdnTypes) if err != nil { return } defer stmt.Close() res, err := stmt.Query() if err != nil { return } var c CdnFileType var desc *string for res.Next() { err = res.Scan( &c.ID, &c.MimeType, &c.Title, &desc, ) if err != nil { return } if desc != nil { c.Description = *desc } cts = append(cts, c) } defer res.Close() go redis.Setex(AllCdnFileTypeRedisKey, cts, 86400) return }
// GetAllVideoTypes Retrieves all VideoTypes func GetAllVideoTypes() (vts []VideoType, err error) { data, err := redis.Get(AllVideoTypesRedisKey) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &vts) return } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return } defer db.Close() stmt, err := db.Prepare(getAllVideoTypes) if err != nil { return } defer stmt.Close() rows, err := stmt.Query() if err != nil { return } var vt VideoType var vName, vIcon *string for rows.Next() { err = rows.Scan(&vt.ID, &vName, &vIcon) if err != nil { return } if vName != nil { vt.Name = *vName } if vIcon != nil { vt.Icon = *vIcon } vts = append(vts, vt) } defer rows.Close() go redis.Setex(AllVideoTypesRedisKey, vts, 86400) return }
// GetAllChannelTypes Retrieves all ChannelType func GetAllChannelTypes() (cts []ChannelType, err error) { data, err := redis.Get(AllChannelTypesRedisKey) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &cts) return } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return } defer db.Close() stmt, err := db.Prepare(getAllChannelTypes) if err != nil { return } defer stmt.Close() var c ChannelType res, err := stmt.Query() if err != nil { return } for res.Next() { err = res.Scan( &c.ID, &c.Name, &c.Description, ) if err != nil { return } cts = append(cts, c) } defer res.Close() go redis.Setex(AllChannelTypesRedisKey, cts, 86400) return cts, err }
// GetAllVideos This grabs all the videos given a certain Brand. Videos are Base Videos and do not have advanced information. func GetAllVideos(dtx *apicontext.DataContext) (vs Videos, err error) { data, err := redis.Get(AllVideosRedisKey + ":" + dtx.BrandString) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &vs) return } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return } defer db.Close() stmt, err := db.Prepare(getAllVideos) if err != nil { return } defer stmt.Close() rows, err := stmt.Query(dtx.APIKey, dtx.BrandID, dtx.BrandID) if err != nil { return } ch := make(chan Videos) go populateVideos(rows, ch) vs = <-ch close(ch) if len(vs) == 0 { err = sql.ErrNoRows return } go redis.Setex(AllVideosRedisKey+":"+dtx.BrandString, vs, 86400) return }
// GetParts Gets all the Video's associated products. func (v *Video) GetParts() (err error) { redis_key := "video:parts:" + strconv.Itoa(v.ID) data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &v.PartIds) return } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return } defer db.Close() stmt, err := db.Prepare(getVideoParts) if err != nil { return } defer stmt.Close() rows, err := stmt.Query(v.ID) if err != nil { return } var i *int for rows.Next() { err = rows.Scan(&i) if err != nil { return err } if i != nil { v.PartIds = append(v.PartIds, *i) } } if len(v.PartIds) > 0 { go redis.Setex(redis_key, v.PartIds, redis.CacheTimeout) } return }
// GetVideoDetails grabs a video's more advance information such as, Brands, CDN files, associated channels(youtube videos), and any products associated with the video. func (v *Video) GetVideoDetails() error { redis_key := "video:details:" + strconv.Itoa(v.ID) data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &v) return err } brandChan := make(chan error) chanChan := make(chan error) cdnChan := make(chan error) partChan := make(chan error) err = v.Get() if err != nil { return err } go func() { err = v.GetBrands() if err != nil { brandChan <- err } brandChan <- nil }() go func() { chs, err := v.GetChannels() if err != nil { chanChan <- err } v.Channels = chs chanChan <- nil }() go func() { cdns, err := v.GetCdnFiles() if err != nil { cdnChan <- err } v.Files = cdns cdnChan <- nil }() go func() { err := v.GetParts() if err != nil { partChan <- err } partChan <- nil }() err = <-brandChan if err != nil { return err } err = <-chanChan if err != nil { return err } err = <-cdnChan if err != nil { return err } err = <-partChan if err != nil { return err } close(brandChan) close(chanChan) close(cdnChan) close(partChan) if v != nil { go redis.Setex(redis_key, v, redis.CacheTimeout) } return nil }
func ReverseLookup(partId int) (vehicles []Vehicle, err error) { redis_key := fmt.Sprintf("part:%d:vehicles", partId) data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { if err = json.Unmarshal(data, &vehicles); err == nil { return } } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return } defer db.Close() stmt, err := db.Prepare(reverseLookupStmt) if err != nil { return } defer stmt.Close() rows, err := stmt.Query(partId) if err != nil { return } vehicleArray := make(map[int]Vehicle, 0) for rows.Next() { var id int var year int var ma string var mo string var sm string var configVal string var configType string if err = rows.Scan(&id, &year, &ma, &mo, &sm, &configVal, &configType); err != nil { break } v, ok := vehicleArray[id] if ok && configType != "" && configVal != "" { // Vehicle Record exists for this ID // so we'll simply append this configuration variable config := Config{Type: configType, Value: configVal} v.Configuration = append(v.Configuration, config) } else { var config Config if configType != "" && configVal != "" { config = Config{ Type: configType, Value: configVal, } } v = Vehicle{ ID: id, Year: year, Make: ma, Model: mo, Submodel: sm, } if config.Type != "" && config.Value != "" { v.Configuration = append(v.Configuration, config) } } vehicleArray[v.ID] = v } defer rows.Close() vehicles = make([]Vehicle, 0) for _, v := range vehicleArray { vehicles = append(vehicles, v) } go redis.Setex(redis_key, vehicles, redis.CacheTimeout) return }
// Gets All Web Properties func GetAll(dtx *apicontext.DataContext) (WebProperties, error) { var ws WebProperties var err error redis_key := "webproperties:" + dtx.BrandString data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &ws) if err == nil { return ws, err } } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return ws, err } defer db.Close() stmt, err := db.Prepare(getAllWebProperties) if err != nil { return ws, err } defer stmt.Close() webPropTypes, err := GetAllWebPropertyTypes(dtx) webPropNotes, err := GetAllWebPropertyNotes(dtx) WebPropertyRequirements, err := GetAllWebPropertyRequirements(dtx) if err != nil { return ws, err } typesMap := webPropTypes.ToMap() notesMap := webPropNotes.ToMap() requirementsMap := WebPropertyRequirements.ToMap() var url, sid *string var tid *int res, err := stmt.Query(dtx.APIKey, dtx.BrandID, dtx.BrandID) for res.Next() { var w WebProperty res.Scan( &w.ID, &w.Name, &w.CustID, &w.BadgeID, &url, &w.IsEnabled, &sid, &tid, &w.IsFinalApproved, &w.IsEnabledDate, &w.IsDenied, &w.RequestedDate, &w.AddedDate, ) if err != nil { return ws, err } if url != nil { w.Url = *url } if sid != nil { w.SellerID = *sid } if tid != nil { w.WebPropertyType.ID = *tid } typeChan := make(chan int) notesChan := make(chan int) requirementsChan := make(chan int) go func() error { if _, ok := typesMap[w.WebPropertyType.ID]; ok { w.WebPropertyType = typesMap[w.WebPropertyType.ID] } typeChan <- 1 return nil }() go func() error { if _, ok := notesMap[w.ID]; ok { w.WebPropertyNotes = append(w.WebPropertyNotes, notesMap[w.ID]) } notesChan <- 1 return nil }() go func() error { if _, ok := requirementsMap[w.ID]; ok { w.WebPropertyRequirements = append(w.WebPropertyRequirements, requirementsMap[w.ID]) } requirementsChan <- 1 return nil }() <-typeChan <-notesChan <-requirementsChan ws = append(ws, w) } go redis.Setex(redis_key, ws, 86400) return ws, err }
func (l *Lookup) GetYears(dtx *apicontext.DataContext) error { //hit redis first redis_key := fmt.Sprintf("lookup:years:%s", dtx.BrandString) data, err := redis.Get(redis_key) if err == nil { err = json.Unmarshal(data, &l.Years) return nil } stmtBeginning := ` select distinct y.YearID from vcdb_Year as y join BaseVehicle as bv on y.YearID = bv.YearID join vcdb_Vehicle as v on bv.ID = v.BaseVehicleID join vcdb_VehiclePart as vp on v.ID = vp.VehicleID join Part as p on vp.PartNumber = p.partID where (p.status = 800 || p.status = 900) ` stmtEnd := ` order by y.YearID desc` brandStmt := " && p.brandID in (" for _, b := range l.Brands { brandStmt += strconv.Itoa(b) + "," } brandStmt = strings.TrimRight(brandStmt, ",") + ")" wholeStmt := stmtBeginning + brandStmt + stmtEnd db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return err } defer db.Close() stmt, err := db.Prepare(wholeStmt) if err != nil { return err } defer stmt.Close() res, err := stmt.Query() if err != nil { return err } l.Years = make([]int, 0) for res.Next() { var year int err = res.Scan(&year) if err == nil { l.Years = append(l.Years, year) } } defer res.Close() l.Pagination = Pagination{ TotalItems: len(l.Years), ReturnedCount: len(l.Years), Page: 1, PerPage: len(l.Years), TotalPages: 1, } if dtx.BrandString != "" { go redis.Setex(redis_key, l.Years, 86400) } return nil }
func GetAllLocations(dtx *apicontext.DataContext) (CustomerLocations, error) { var ls CustomerLocations var err error redis_key := "customers:locations:" + dtx.BrandString data, err := redis.Get(redis_key) if err == nil && len(data) > 0 { err = json.Unmarshal(data, &ls) return ls, err } db, err := sql.Open("mysql", database.ConnectionString()) if err != nil { return ls, err } defer db.Close() stmt, err := db.Prepare(getLocations) if err != nil { return ls, err } defer stmt.Close() res, err := stmt.Query(dtx.APIKey, dtx.BrandID, dtx.BrandID) var name, address, city, email, phone, fax, contactPerson, postal []byte for res.Next() { var l CustomerLocation err = res.Scan( &l.Id, &name, &address, &city, &l.State.Id, &email, &phone, &fax, &l.Coordinates.Latitude, &l.Coordinates.Longitude, &l.CustomerId, &contactPerson, &l.IsPrimary, &postal, &l.ShippingDefault, ) if err != nil { return ls, err } l.Name, err = conversions.ByteToString(name) l.Address, err = conversions.ByteToString(address) l.City, err = conversions.ByteToString(city) l.Email, err = conversions.ByteToString(email) l.Phone, err = conversions.ByteToString(phone) l.Fax, err = conversions.ByteToString(fax) l.ContactPerson, err = conversions.ByteToString(contactPerson) l.PostalCode, err = conversions.ByteToString(postal) if err != nil { return ls, err } ls = append(ls, l) } defer res.Close() go redis.Setex(redis_key, ls, 86400) return ls, err }