// Index 首页 func (IndexController) Index(ctx echo.Context) error { num := 10 paginator := logic.NewPaginatorWithPerPage(1, num) topicsList := make([]map[string]interface{}, num) // 置顶的topic topTopics := logic.DefaultTopic.FindAll(ctx, paginator, "ctime DESC", "top=1") if len(topTopics) < num { // 获取最新帖子 paginator.SetPerPage(num - len(topTopics)) newTopics := logic.DefaultTopic.FindAll(ctx, paginator, "ctime DESC", "top=0") topicsList = append(topTopics, newTopics...) } // 获得最新博文 recentArticles := logic.DefaultArticle.FindBy(ctx, 10) // 获取当前用户喜欢对象信息 var likeFlags map[int]int if len(recentArticles) > 0 { curUser, ok := ctx.Get("user").(*model.Me) if ok { likeFlags, _ = logic.DefaultLike.FindUserLikeObjects(ctx, curUser.Uid, model.TypeArticle, recentArticles[0].Id, recentArticles[len(recentArticles)-1].Id) } } // Golang 资源 resources := logic.DefaultResource.FindBy(ctx, 10) return render(ctx, "index.html", map[string]interface{}{"topics": topicsList, "articles": recentArticles, "likeflags": likeFlags, "resources": resources}) }
// ReadList 我的(某人的)收藏 func (FavoriteController) ReadList(ctx echo.Context) error { username := ctx.Param("username") user := logic.DefaultUser.FindOne(ctx, "username", username) if user == nil || user.Uid == 0 { return ctx.Redirect(http.StatusSeeOther, "/") } objtype := goutils.MustInt(ctx.QueryParam("objtype"), model.TypeArticle) p := goutils.MustInt(ctx.QueryParam("p"), 1) data := map[string]interface{}{"objtype": objtype, "user": user} rows := goutils.MustInt(ctx.QueryParam("rows"), 20) if rows > 20 { rows = 20 } favorites, total := logic.DefaultFavorite.FindUserFavorites(ctx, user.Uid, objtype, (p-1)*rows, rows) if total > 0 { objids := slices.StructsIntSlice(favorites, "Objid") switch objtype { case model.TypeTopic: data["topics"] = logic.DefaultTopic.FindByTids(objids) case model.TypeArticle: data["articles"] = logic.DefaultArticle.FindByIds(objids) case model.TypeResource: data["resources"] = logic.DefaultResource.FindByIds(objids) case model.TypeWiki: // data["wikis"] = logic.DefaultWiki.FindWikisByIds(objids) case model.TypeProject: data["projects"] = logic.DefaultProject.FindByIds(objids) } } uri := fmt.Sprintf("/favorites/%s?objtype=%d&rows=%d&", user.Username, objtype, rows) paginator := logic.NewPaginatorWithPerPage(p, rows) data["pageHtml"] = paginator.SetTotal(total).GetPageHtml(uri) return render(ctx, "favorite.html", data) }
// Search func (SearchController) Search(ctx echo.Context) error { q := ctx.QueryParam("q") field := ctx.QueryParam("f") p := goutils.MustInt(ctx.QueryParam("p"), 1) rows := 20 respBody, err := logic.DefaultSearcher.DoSearch(q, field, (p-1)*rows, rows) data := map[string]interface{}{ "respBody": respBody, "q": q, "f": field, } if err == nil { uri := "/search?q=" + q + "&f=" + field + "&" paginator := logic.NewPaginatorWithPerPage(p, rows) data["pageHtml"] = paginator.SetTotal(int64(respBody.NumFound)).GetPageHtml(uri) } return render(ctx, "search.html", data) }