Example #1
0
// 我的(某人的)收藏
// uri: /favorites/{username}
func SomeoneFavoritesHandler(rw http.ResponseWriter, req *http.Request) {
	vars := mux.Vars(req)
	username := vars["username"]
	user := service.FindUserByUsername(username)
	if user == nil {
		util.Redirect(rw, req, "/")
		return
	}

	objtype, err := strconv.Atoi(req.FormValue("objtype"))
	if err != nil {
		objtype = model.TYPE_ARTICLE
	}

	p, err := strconv.Atoi(req.FormValue("p"))
	if err != nil {
		p = 1
	}

	data := map[string]interface{}{"objtype": objtype, "user": user}

	rows := 20
	favorites, total := service.FindUserFavorites(user.Uid, objtype, (p-1)*rows, rows)
	if total > 0 {
		objids := util.Models2Intslice(favorites, "Objid")

		switch objtype {
		case model.TYPE_TOPIC:
			data["topics"] = service.FindTopicsByIds(objids)
		case model.TYPE_ARTICLE:
			data["articles"] = service.FindArticlesByIds(objids)
		case model.TYPE_RESOURCE:
			data["resources"] = service.FindResourcesByIds(objids)
		case model.TYPE_WIKI:
			// data["wikis"] = service.FindArticlesByIds(objids)
		case model.TYPE_PROJECT:
			data["projects"] = service.FindProjectsByIds(objids)
		}

	}

	uri := fmt.Sprintf("/favorites/%s?objtype=%d&p=%d", user.Username, objtype, p)
	data["pageHtml"] = service.GenPageHtml(p, rows, total, uri)

	// 设置模板数据
	filter.SetData(req, data)
	req.Form.Set(filter.CONTENT_TPL_KEY, "/template/favorite.html")
}
Example #2
0
// search
// uri: /search
func SearchHandler(rw http.ResponseWriter, req *http.Request) {
	q := req.FormValue("q")
	field := req.FormValue("f")
	p, err := strconv.Atoi(req.FormValue("p"))
	if err != nil {
		p = 1
	}

	rows := 20

	pageHtml := ""
	respBody, err := service.DoSearch(q, field, (p-1)*rows, rows)
	if err == nil && respBody != nil {
		pageHtml = service.GenPageHtml(p, rows, respBody.NumFound, "/search?q="+q+"&f="+field)
	}

	req.Form.Set(filter.CONTENT_TPL_KEY, "/template/search.html")
	// 设置模板数据
	filter.SetData(req, map[string]interface{}{"respBody": respBody, "q": q, "f": field, "pageHtml": pageHtml})
}