Example #1
0
func FindArticles(offset, size int) ([]entity.Article, int64, error) {
	dbm := dao.GetDBM(conf.DATABASE_NAME)
	defer dbm.Close()
	sql := "SELECT * FROM article WHERE delFlg=0 AND status=0 LIMIT ?,?"
	rows, err := dbm.Query(sql, offset, size)
	if err != nil {
		return nil, 0, err
	}
	defer rows.Close()
	var items []entity.Article = make([]entity.Article, 0)
	count := -1
	for rows.Next() {
		count++
		var item entity.Article
		rows.Scan(&item.Id, &item.Title, &item.Content, &item.Keywords, &item.Description, &item.Lang, &item.Tag, &item.Timestamp, &item.Status, &item.DelFlg)
		tmp, err := strconv.ParseInt(item.Timestamp, 10, 0)
		if err != nil {
			tmp = time.Now().Unix()
		}
		item.Timestamp = time.Unix(tmp, 0).Format("2006-01-02")
		items = append(items, item)
	}
	if count == -1 {
		return nil, 0, errors.New("no data")
	}
	return items, GetArticleCount(), nil
}
Example #2
0
func FindArticleById(id int) (entity.Article, error) {
	dbm := dao.GetDBM(conf.DATABASE_NAME)
	defer dbm.Close()
	sql := "SELECT * FROM article WHERE id=? AND delFlg=0"
	rows, err := dbm.Query(sql, strconv.Itoa(id))
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()
	var item entity.Article
	count := -1
	for rows.Next() {
		count++
		rows.Scan(&item.Id, &item.Title, &item.Content, &item.Keywords, &item.Description, &item.Lang, &item.Tag, &item.Timestamp, &item.Status, &item.DelFlg)
		tmp, err := strconv.ParseInt(item.Timestamp, 10, 0)
		if err != nil {
			tmp = time.Now().Unix()
		}
		// item.Timestamp = time.Unix(tmp, 0).Format("2006-01-02 15:04:05")
		item.Timestamp = time.Unix(tmp, 0).Format("2006-01-02")
	}
	if count == -1 {
		return item, errors.New("no data")
	}
	return item, nil
}