Beispiel #1
0
func tagExist(db *gorp.DbMap, name string) bool {
	id, _ := db.SelectInt("select Id from Tag where Title = ?", name)
	if id != 0 {
		return true
	}
	return false
}
func PostUser(r render.Render, dbmap *gorp.DbMap, res http.ResponseWriter, u User, e binding.Errors) {
	if e != nil {
		r.JSON(http.StatusBadRequest, map[string]string{"message": e[0].Message})
		return
	}
	//check user exists yet or not?
	var count int64
	count, err := dbmap.SelectInt("SELECT count(*) FROM CUSTOMER WHERE CUS_TEL=?", u.PhoneNumber)
	if err != nil {
		glog.V(1).Infof("[DEBUG:] Search customer by tel:%v fail:%v", u.PhoneNumber, err)
		r.JSON(http.StatusConflict, map[string]string{"message": "DB ERROR"})
		return
	}

	if count > 0 {
		glog.V(1).Infof("Customer with tel:%v exists yet", u.PhoneNumber)
		r.JSON(http.StatusConflict, map[string]string{"message": "User with same Tel exists"})
		return
	}

	//insert new user info to db;
	err = dbmap.Insert(&u)
	if err != nil {
		glog.V(1).Infof("[DEBUG:] Insert customer %v fail:%v", u, err)
		r.JSON(http.StatusConflict, map[string]string{"message": "DB ERROR"})
		return
	}
	r.JSON(200, map[string]string{"message": "SUCCESS"})
}
Beispiel #3
0
func getNeoId(dbMap *gorp.DbMap, code string) (int, error) {
	id, err := dbMap.SelectInt("SELECT id FROM neo_map WHERE code ='" + code + "'")
	if err != nil {
		fmt.Println(err)
		return 0, err
	}
	return int(id), nil
}
Beispiel #4
0
func getSlug(db *gorp.DbMap, title string) string {
	title = chpu.Chpu(title)
	for {
		id, _ := db.SelectInt("select Id from New where Slug = ?", title)
		if id != 0 {
			title = strinc.Inc(title)
		} else {
			break
		}
	}
	return title
}
Beispiel #5
0
func IsVisitedPage(Db *gorp.DbMap, fullurl string) bool {
	fullurl = n(fullurl)
	u, _ := url.Parse(fullurl)
	visited := false

	vis, err := Db.SelectInt("select Visited from SitePage"+
		" where SiteId in (select Id from Site where Domain = ?"+
		") and Url = ?", u.Host, u.RequestURI())
	if err != nil {
		return false
	}
	if vis > 0 {
		visited = true
	}

	return visited
}
Beispiel #6
0
func Create(db *gorp.DbMap, c ChangeLog) (ChangeLog, error) {
	c.Slug = chpu.Chpu(c.Title)
	for {
		id, _ := db.SelectInt("select Id from ChangeLog where Slug = ?", c.Slug)
		if id != 0 {
			c.Slug = strinc.Inc(c.Slug)
		} else {
			break
		}
	}
	r := rate.Rate{}

	t := time.Now().UTC()
	c.Created = t.UnixNano()
	err := db.Insert(&c)
	r.Create(db, 6, c.Id)
	return c, err
}
Beispiel #7
0
func (r Rate) Vote(Db *gorp.DbMap, v string, u int64) (Rate, error) {
	var el int64
	if r.Id == 0 {
		return Rate{}, errors.New("Rate not found")
	}
	r = r.GetRate(Db, r.ItemType, r.ItemId)
	id, err := Db.SelectInt("select RateId from Vote where RateId = ? and"+
		" UserId = ?", r.Id, u)
	if err != nil {
		return Rate{}, err
	}
	if id != 0 {
		return Rate{}, errors.New("You have already voted")
	}
	switch v {
	case "a":
		el = -1
		r.Against++
		Db.Exec("update Rate set Against = Against+1 where Id = ?", r)
		break
	case "b":
		el = 1
		r.Behind++
		Db.Exec("update Rate set Behind = Behind+1 where Id = ?", r)
		break
	default:
		return Rate{}, errors.New("Vote election undefined")
	}

	r.Rate = WilsonSum(r.Behind-r.Against, r.Against+r.Behind)

	vote := Vote{
		RateId: r.Id,

		Value:  el,
		UserId: u,
	}

	Db.Update(&r)
	Db.Insert(&vote)
	return r, nil
}
Beispiel #8
0
// Returns 0 if not found
func FindArticleIdByKey(dbmap *gorp.DbMap, accountId int64, key string) (id int64, err error) {
	return dbmap.SelectInt(`select id from articles where account_id = $1 and key = $2`, accountId, key)
}