Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 3
0
func ArticlePostHandler(w http.ResponseWriter, r *http.Request, action string) {
	if r.Method == "GET" {
		model := make(map[string]interface{})
		model["title"] = "Post"
		model["action"] = "post"
		ExecuteTemplate(w, "post", model)
		return
	}

	if r.Method != "POST" {
		return
	}

	defer r.Body.Close()
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.NotFound(w, r)
		return
	}

	var dat map[string]interface{}
	err = json.Unmarshal(body, &dat)
	if err != nil {
		http.NotFound(w, r)
		return
	}

	title := fmt.Sprint(dat["title"])
	content := fmt.Sprint(dat["content"])
	keywords := fmt.Sprint(dat["keywords"])
	description := fmt.Sprint(dat["description"])
	lang := fmt.Sprint(dat["lang"])
	tag := fmt.Sprint(dat["tag"])
	status := fmt.Sprint(dat["status"])

	var article entity.Article
	article.Title = title
	article.Content = content
	article.Keywords = keywords
	article.Description = description
	article.Lang = lang
	article.Status = status
	article.Tag = tag

	service.SaveArticle(article)

	tmp := time.Unix(time.Now().Unix(), 0).Format("20060102")
	tmp2 := strings.Replace(title, " ", "-", -1)
	filename := "posted/" + tmp + "-" + tmp2 + ".txt"
	ioutil.WriteFile(filename, []byte(content), 0600)

	http.Redirect(w, r, "/articles", http.StatusFound)
}
Exemplo n.º 4
0
func ArticleSaveHandler(w http.ResponseWriter, r *http.Request, action string) {
	if action != "post" {
		http.NotFound(w, r)
		return
	}

	title := r.FormValue("title")
	content := r.FormValue("content")
	keywords := r.FormValue("keywords")
	description := r.FormValue("description")
	lang := r.FormValue("lang")
	tag := r.FormValue("tag")
	status := r.FormValue("status")

	if utils.IsEmpty(title) {
		OnResponse(w, 201, "标题不能为空", nil)
		return
	}
	if utils.IsEmpty(content) {
		OnResponse(w, 202, "内容不能为空", nil)
		return
	}

	var article entity.Article
	article.Title = title
	article.Content = content
	article.Keywords = keywords
	article.Description = description
	article.Lang = lang
	article.Status = status
	article.Tag = tag

	service.SaveArticle(article)

	tmp := time.Unix(time.Now().Unix(), 0).Format("20060102")
	tmp2 := strings.Replace(title, " ", "-", -1)
	filename := "posted/" + tmp + "-" + tmp2 + ".txt"
	ioutil.WriteFile(filename, []byte(content), 0600)

	http.Redirect(w, r, "/articles", http.StatusFound)
}