Example #1
0
func DeleteWebsite(siteId int) error {
	var err error
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(deleteSiteToBrand)
	if err != nil {
		return err
	}
	defer stmt.Close()
	_, err = stmt.Exec(siteId)
	if err != nil {
		return err
	}
	stmt, err = db.Prepare(deleteSite)
	if err != nil {
		return err
	}
	defer stmt.Close()
	_, err = stmt.Exec(siteId)
	if err != nil {
		return err
	}
	return err
}
Example #2
0
func (r *SalesRep) Get() error {
	if r.ID == 0 {
		return errors.New("Invalid SalesRep ID")
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(getSalesRep)
	if err != nil {
		return err
	}
	defer stmt.Close()

	var rep SalesRep
	row := stmt.QueryRow(r.ID)
	if err = row.Scan(&rep.ID, &rep.Name, &rep.Code, &rep.CustomerCount); err != nil {
		return err
	}

	r.ID = rep.ID
	r.Name = rep.Name
	r.Code = rep.Code
	r.CustomerCount = rep.CustomerCount

	return nil
}
Example #3
0
//Fetch a great many revisions
func GetAllContentRevisions() (cr ContentRevisions, err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return cr, err
	}
	defer db.Close()
	stmt, err := db.Prepare(getAllContentRevisions)
	if err != nil {
		return cr, err
	}
	defer stmt.Close()

	var rev ContentRevision
	res, err := stmt.Query()
	for res.Next() {
		err = res.Scan(
			&rev.Id,
			&rev.Text,
			&rev.CreatedDate,
			&rev.Active,
		)
		if err != nil {
			return cr, err
		}
		cr = append(cr, rev)
	}
	defer res.Close()
	return cr, err
}
Example #4
0
func (g *Group) Update(dtx *apicontext.DataContext) error {
	if g.ID == 0 {
		return errors.New("Invalid Group ID")
	}

	if len(strings.TrimSpace(g.Name)) == 0 {
		return errors.New("Group must have a name")
	}

	if g.WebsiteID != dtx.WebsiteID {
		g.WebsiteID = dtx.WebsiteID
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(updateForumGroup)
	if err != nil {
		return err
	}
	defer stmt.Close()

	if _, err := stmt.Exec(g.Name, g.Description, g.WebsiteID, g.ID); err != nil {
		return err
	}

	return nil
}
Example #5
0
func (rep *SalesRep) Update() error {
	if rep.ID == 0 {
		return errors.New("Invalid SalesRep ID")
	}

	if len(strings.TrimSpace(rep.Name)) == 0 {
		return errors.New("SalesRep must have a name")
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(updateSalesRep)
	if err != nil {
		return err
	}
	defer stmt.Close()

	if _, err := stmt.Exec(rep.Name, rep.Code, rep.ID); err != nil {
		return err
	}

	return nil
}
Example #6
0
//already have vehicleID (vcdb_vehicle.ID)? get parts
func (v *CurtVehicle) GetPartsFromVehicleConfig(dtx *apicontext.DataContext) (ps []products.Part, err error) {
	//get parts
	var p products.Part
	//get part id
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return ps, err
	}
	defer db.Close()
	stmt, err := db.Prepare(getPartID)
	if err != nil {
		return ps, err
	}
	defer stmt.Close()
	res, err := stmt.Query(v.ID)
	for res.Next() {
		err = res.Scan(&p.ID)
		if err != nil {
			return ps, err
		}
		//get part -- adds some weight
		err = p.FromDatabase(getBrandsFromDTX(dtx))
		if err != nil {
			return ps, err
		}

		ps = append(ps, p)
	}
	defer res.Close()
	return ps, err
}
Example #7
0
//makes warehouse map
func getWarehouseCodes() (map[string]int, error) {
	warehouses := make(map[string]int)
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return warehouses, err
	}
	defer db.Close()

	rows, err := db.Query(warehouseMap)
	if err != nil {
		return warehouses, err
	}
	var id *int
	var code *string
	for rows.Next() {
		err = rows.Scan(&id, &code)
		if err != nil {
			return warehouses, err
		}
		if id != nil && code != nil {
			warehouses[*code] = *id
		}
	}
	return warehouses, nil
}
Example #8
0
func (w *Website) Create() (err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()
	tx, err := db.Begin()
	stmt, err := tx.Prepare(createSite)
	if err != nil {
		return err
	}
	defer stmt.Close()
	res, err := stmt.Exec(w.Url, w.Description)
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	id, err := res.LastInsertId()
	w.ID = int(id)
	if err != nil {
		return err
	}
	err = w.JoinToBrand()
	if err != nil {
		return err
	}
	return err
}
Example #9
0
func (w *Website) Update() error {
	var err error
	for _, brandId := range w.BrandIDs {
		err = w.DeleteBrandJoin(brandId)
		if err != nil {
			return err
		}
	}
	err = w.JoinToBrand()
	if err != nil {
		return err
	}
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()
	tx, err := db.Begin()
	stmt, err := tx.Prepare(updateSite)
	if err != nil {
		return err
	}
	defer stmt.Close()
	_, err = stmt.Exec(w.Url, w.Description, w.ID)
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return err
}
Example #10
0
//updatin' a menu
func (m *Menu) Update() (err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()
	tx, err := db.Begin()
	stmt, err := tx.Prepare(updateMenu)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(m.Name,
		m.IsPrimary,
		m.Active,
		m.DisplayName,
		m.RequireAuthentication,
		m.ShowOnSitemap,
		m.Sort,
		m.WebsiteId,
		m.Id,
	)
	if err != nil {
		tx.Rollback()
		return err
	}
	err = tx.Commit()
	if err != nil {
		return err
	}
	return err
}
Example #11
0
//Fetch up a menu by name
func (m *Menu) GetByName(dtx *apicontext.DataContext) (err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()
	stmt, err := db.Prepare(getMenuByName)
	if err != nil {
		return err
	}
	defer stmt.Close()

	var display *string

	err = stmt.QueryRow(m.Name, dtx.APIKey, dtx.BrandID, dtx.BrandID).Scan(
		&m.Id,
		&m.Name,
		&m.IsPrimary,
		&m.Active,
		&display,
		&m.RequireAuthentication,
		&m.ShowOnSitemap,
		&m.Sort,
		&m.WebsiteId,
	)
	if err != nil {
		return err
	}
	if display != nil {
		m.DisplayName = *display
	}
	return err
}
Example #12
0
func (t *Topic) Delete(dtx *apicontext.DataContext) error {
	if t.ID == 0 {
		return errors.New("Invalid Topic ID")
	}

	if err := t.DeleteThreads(dtx); err != nil {
		return err
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(deleteForumTopic)
	if err != nil {
		return err
	}
	defer stmt.Close()

	if _, err = stmt.Exec(t.ID); err != nil {
		return err
	}

	return nil
}
Example #13
0
func (t *Topic) Update() error {
	if t.ID == 0 {
		return errors.New("Invalid Topic ID")
	}

	if t.GroupID == 0 {
		return errors.New("Topic must have a Group ID")
	}
	if len(strings.TrimSpace(t.Name)) == 0 {
		return errors.New("Topic must have a name")
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(updateForumTopic)
	if err != nil {
		return err
	}
	defer stmt.Close()

	if _, err := stmt.Exec(t.GroupID, t.Name, t.Description, t.Image, t.Active, t.Closed, t.ID); err != nil {
		return err
	}

	return nil
}
Example #14
0
func (t *Topic) Add() error {
	if t.GroupID == 0 {
		return errors.New("Topic must have a Group ID")
	}
	if len(strings.TrimSpace(t.Name)) == 0 {
		return errors.New("Topic must have a name")
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(addForumTopic)
	if err != nil {
		return err
	}
	defer stmt.Close()

	res, err := stmt.Exec(t.GroupID, t.Name, t.Description, t.Image, t.Active, t.Closed)
	if err != nil {
		return err
	}

	if id, err := res.LastInsertId(); err != nil {
		return err
	} else {
		t.ID = int(id)
	}

	return nil
}
Example #15
0
func GetAllApiKeyTypes() (as []ApiKeyType, err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return
	}
	defer db.Close()
	stmt, err := db.Prepare(getAllApiKeyTypes)
	if err != nil {
		return
	}
	defer stmt.Close()
	res, err := stmt.Query() //returns *sql.Rows
	if err != nil {
		return
	}

	for res.Next() {
		a, err := ScanKey(res)
		if err != nil {
			return as, err
		}
		as = append(as, *a)
	}
	defer res.Close()
	return as, err
}
Example #16
0
func (w *Website) Delete() (err error) {
	for _, brandId := range w.BrandIDs {
		err = w.DeleteBrandJoin(brandId)
		if err != nil {
			return err
		}
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()
	tx, err := db.Begin()
	stmt, err := tx.Prepare(deleteSite)
	if err != nil {
		return err
	}
	defer stmt.Close()
	_, err = stmt.Exec(w.ID)
	if err != nil {
		tx.Rollback()
		return err
	}
	err = tx.Commit()
	if err != nil {
		return err
	}

	return err
}
Example #17
0
func (a *ApiKeyType) Create() (err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return
	}
	defer db.Close()
	stmt, err := db.Prepare(createApiKeyType)
	if err != nil {
		return
	}
	defer stmt.Close()
	added := time.Now().Format(timeFormat)
	_, err = stmt.Exec(a.Type, added)
	if err != nil {
		return
	}

	stmt, err = db.Prepare(getKeyByDateType)
	if err != nil {
		return err
	}

	defer stmt.Close()

	err = stmt.QueryRow(a.Type, added).Scan(&a.ID)
	if err != nil {
		return err
	}
	return
}
Example #18
0
func GetAllWebsites() (ws Websites, err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return ws, err
	}
	defer db.Close()
	stmt, err := db.Prepare(getAllSites)
	if err != nil {
		return ws, err
	}
	defer stmt.Close()
	res, err := stmt.Query()
	var w Website
	var url, desc *string
	for res.Next() {
		err = res.Scan(
			&w.ID,
			&url,
			&desc,
		)
		if err != nil {
			return ws, err
		}
		if url != nil {
			w.Url = *url
		}
		if desc != nil {
			w.Description = *desc
		}
		ws = append(ws, w)
	}
	defer res.Close()
	return ws, err
}
Example #19
0
func (i *VehicleInquiry) Push() error {

	if i.Name == "" {
		return fmt.Errorf("%s", "name is required")
	}
	if i.Category == 0 {
		return fmt.Errorf("%s", "category is required")
	}
	if i.Phone == "" && i.Email == "" {
		return fmt.Errorf("%s", "a form of contact is required")
	}
	if i.Vehicle == "" {
		return fmt.Errorf("%s", "the vehicle of inquiry is required")
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(insertStmt)
	if err != nil {
		return err
	}
	defer stmt.Close()

	_, err = stmt.Exec(i.Name, i.Category, i.Phone, i.Email, i.Vehicle, i.Message)
	return err
}
Example #20
0
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
}
Example #21
0
func (g *Group) Add(dtx *apicontext.DataContext) error {
	if len(strings.TrimSpace(g.Name)) == 0 {
		return errors.New("Group must have a name")
	}

	if g.WebsiteID != dtx.WebsiteID {
		g.WebsiteID = dtx.WebsiteID
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(addForumGroup)
	if err != nil {
		return err
	}
	defer stmt.Close()

	res, err := stmt.Exec(g.Name, g.Description, g.WebsiteID)
	if err != nil {
		return err
	}

	if id, err := res.LastInsertId(); err != nil {
		return err
	} else {
		g.ID = int(id)
	}

	return nil
}
Example #22
0
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
}
Example #23
0
func (g *Group) Delete(dtx *apicontext.DataContext) error {
	if g.ID == 0 {
		return errors.New("Invalid Group ID")
	}

	if err := g.DeleteTopics(dtx); err != nil {
		return err
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(deleteForumGroup)
	if err != nil {
		return err
	}
	defer stmt.Close()

	if _, err = stmt.Exec(g.ID); err != nil {
		return err
	}

	return nil
}
Example #24
0
func Search(title, lead, content, publishStart, publishEnd, active, slug, pageStr, resultsStr string, dtx *apicontext.DataContext) (pagination.Objects, error) {
	var err error
	var l pagination.Objects
	var fs []interface{}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return l, err
	}
	defer db.Close()

	stmt, err := db.Prepare(search)
	if err != nil {
		return l, err
	}
	defer stmt.Close()

	res, err := stmt.Query("%"+title+"%", "%"+lead+"%", "%"+content+"%", "%"+publishStart+"%", "%"+publishEnd+"%", "%"+active+"%", "%"+slug+"%", dtx.APIKey, dtx.BrandID, dtx.BrandID)
	for res.Next() {
		n, err := scanItem(res)
		if err == nil {
			fs = append(fs, n)
		}
	}
	defer res.Close()
	l = pagination.Paginate(pageStr, resultsStr, fs)
	return l, err
}
Example #25
0
func GetAllSalesReps() (reps SalesReps, err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return
	}
	defer db.Close()

	stmt, err := db.Prepare(getAllSalesReps)
	if err != nil {
		return
	}
	defer stmt.Close()

	rows, err := stmt.Query()
	if err != nil {
		return
	}

	for rows.Next() {
		var rep SalesRep
		if err = rows.Scan(&rep.ID, &rep.Name, &rep.Code, &rep.CustomerCount); err == nil {
			reps = append(reps, rep)
		}
	}
	defer rows.Close()

	return
}
Example #26
0
func (n *News) Delete(dtx *apicontext.DataContext) 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(deleteNews)
	_, err = stmt.Exec(n.ID)
	if err != nil {
		tx.Rollback()
		return err
	}

	err = n.DeleteJoinBrand()
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return nil
}
Example #27
0
func (rep *SalesRep) Add() error {
	if len(strings.TrimSpace(rep.Name)) == 0 {
		return errors.New("SalesRep must have a name")
	}

	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()

	stmt, err := db.Prepare(addSalesRep)
	if err != nil {
		return err
	}
	defer stmt.Close()

	res, err := stmt.Exec(rep.Name, rep.Code)
	if err != nil {
		return err
	}

	if id, err := res.LastInsertId(); err != nil {
		return err
	} else {
		rep.ID = int(id)
	}

	return nil
}
Example #28
0
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
}
Example #29
0
//creatin' a revision, requires content to exist
func (rev *ContentRevision) Create() (err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return err
	}
	defer db.Close()
	tx, err := db.Begin()
	stmt, err := tx.Prepare(createRevision)
	if err != nil {
		return err
	}
	defer stmt.Close()
	rev.CreatedDate = time.Now()
	res, err := stmt.Exec(rev.ContentId, rev.Text, rev.CreatedDate, rev.Active)
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	id, err := res.LastInsertId()
	if err != nil {
		return err
	}
	rev.Id = int(id)
	return err
}
Example #30
0
func CreateWebsite(url, desc string) (webID int, err error) {
	db, err := sql.Open("mysql", database.ConnectionString())
	if err != nil {
		return webID, err
	}
	defer db.Close()
	tx, err := db.Begin()
	stmt, err := tx.Prepare(createSite)
	if err != nil {
		return webID, err
	}
	defer stmt.Close()
	res, err := stmt.Exec(url, desc)
	if err != nil {
		tx.Rollback()
		return webID, err
	}
	tx.Commit()
	id, err := res.LastInsertId()
	webID = int(id)
	if err != nil {
		return webID, err
	}
	return webID, nil
}