Skip to content

Commit

Permalink
Merge pull request #25 from Baihhh/mvp
Browse files Browse the repository at this point in the history
feat: Optimize the front and back end paging logic of homepage
  • Loading branch information
IRONICBo authored Jan 25, 2024
2 parents 00ee4bd + ff449c2 commit 8dd6c4a
Show file tree
Hide file tree
Showing 8 changed files with 579 additions and 366 deletions.
113 changes: 80 additions & 33 deletions cmd/gopcomm/community_yap.gox
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"fmt"
"strconv"

"github.com/goplus/community/internal/core"
"github.com/goplus/community/markdown"
Expand All @@ -20,6 +21,10 @@ var (
trans *translation.Engine
)

const (
layoutUS = "January 2, 2006"
)

todo := context.TODO()
endpoint := os.Getenv("GOP_COMMUNITY_ENDPOINT")
domain := os.Getenv("GOP_COMMUNITY_DOMAIN")
Expand All @@ -42,27 +47,28 @@ get "/failed", ctx => {
}

get "/p/:id", ctx => {
// todo middleware
// Get User Info
var userClaim *casdoorsdk.Claims
var user *core.User
token, err := ctx.Request.Cookie("token")
if err == nil {
userClaim, err = community.GetUserClaim(token.Value)
user, err = community.getUser(token.Value)
if err != nil {
zlog.Error("get user claim error:", err)
zlog.Error("get user error:", err)
}
}

id := ctx.param("id")
article, _ := community.article(todo, id)
ctx.yap "article", {
"User": userClaim,

"User": user,
"ID": id,
"Title": article.Title,
"Content": article.HtmlUrl,
"Tags": article.Tags,
"Cover": article.Cover,
"Ctime": article.Ctime,
"Mtime": article.Mtime.Format(layoutUS),
"Author": article.User,
// "User": article.User,
}
}
Expand All @@ -78,23 +84,28 @@ get "/getArticle/:id", ctx => {

get "/user/:id", ctx => {
id := ctx.param("id")
// Default is the current user info

// Get User Info
var userClaim *casdoorsdk.Claims
// Get current User Info by id
userClaim, err := community.getUserClaim(id)
if err != nil {
zlog.Error("get current user error:", err)
}
// todo middleware
// get user by token
var user *core.User
token, err := ctx.Request.Cookie("token")
if err == nil {
userClaim, err = community.GetUserClaim(token.Value)
user, err = community.getUser(token.Value)
if err != nil {
zlog.Error("get user claim error:", err)
zlog.Error("get user error:", err)
}
}

// TODO: Get User Info
// get article list published by uid
items, _ :=community.GetArticlesByUid(todo, id)
ctx.yap "user", {
"Id": id,
"CurrentUser": userClaim,
"User": userClaim,
"User": user,
"Items": items,
}
}

Expand All @@ -103,38 +114,77 @@ get "/add", ctx => {
}

get "/", ctx => {
// TODO: Add page list

// from := ctx.param("from")
from := ctx.param("page")
limit := ctx.param("limit")
// todo middleware
// Get User Info
var userClaim *casdoorsdk.Claims
var user *core.User
token, err := ctx.Request.Cookie("token")
if err == nil {
userClaim, err = community.GetUserClaim(token.Value)
user, err = community.getUser(token.Value)
if err != nil {
zlog.Error("get user claim error:", err)
zlog.Error("get user error:", err)
}
}

limitInt, err := strconv.Atoi(limit)
if err != nil {
limitInt = 10
}
page, err := strconv.Atoi(from)
if err != nil {
page = 1
}
// Get Article Info
articles, _, _ := community.listArticle(todo, core.MarkBegin, 20)
// articles, next, _ := community.listArticle(todo, from, limitInt)
articles, total, _ := community.articles(todo, page, limitInt, "")
ctx.yap "home", {
"User": userClaim,
"User": user,
"Items": articles,
// "Next": next,
"Page": page,
"TotalPage": (total + limitInt -1)/limitInt,
"Total": total,
}
}

post "/search", ctx => {
searchValue := ctx.param("search")
zlog.Infof("SearchValue: %+v", searchValue)
get "/search", ctx => {
searchValue := ctx.param("value")
if searchValue == "" {
ctx.json {
"code": 400,
"err": "value can not be ''.",
}
}
articles, _ := community.searchArticle(todo, searchValue)
from := ctx.param("page")
limit := ctx.param("limit")
limitInt, err := strconv.Atoi(limit)
if err != nil {
limitInt = 10
}
page, err := strconv.Atoi(from)
if err != nil {
page = 1
}

// todo middleware
var user *core.User
token, err := ctx.Request.Cookie("token")
if err == nil {
user, err = community.getUser(token.Value)
if err != nil {
zlog.Error("get user error:", err)
}
}
articles, total, _ := community.articles(todo, page, limitInt, searchValue)
ctx.yap "home", {
"User": user,
"Items": articles,
"Value": searchValue,
"Page": page,
"TotalPage": (total + limitInt -1)/limitInt,
"Total": total,
}
}

Expand All @@ -147,7 +197,7 @@ get "/edit/:id", ctx => {
}
}

uid, err := community.GetUserId(token.Value)
uid, err := community.ParseJwtToken(token.Value)
if err != nil {
ctx.json {
"code": 500,
Expand Down Expand Up @@ -196,7 +246,7 @@ post "/commit", ctx => {
"err": "no token",
}
}
uid, err := community.GetUserId(token.Value)
uid, err := community.ParseJwtToken(token.Value)
if err != nil {
ctx.json {
"code": 500,
Expand Down Expand Up @@ -234,7 +284,7 @@ post "/translate", ctx => {
"err": "no token",
}
}
uid, err := community.GetUserId(token.Value)
uid, err := community.ParseJwtToken(token.Value)
if err != nil {
ctx.json {
"code": 500,
Expand All @@ -245,7 +295,6 @@ post "/translate", ctx => {
mdData := ctx.param("content")
htmlData := ctx.param("html")
id := ctx.param("id")
zlog.Info(mdData)
// get translation markdown
transData, err := trans.translateMarkdownText(mdData, language.Chinese, language.English)
if err != nil {
Expand Down Expand Up @@ -335,15 +384,13 @@ post "/upload", ctx => {
"err": "no token",
}
}
uid, err := community.GetUserId(token.Value)
uid, err := community.ParseJwtToken(token.Value)
if err != nil {
ctx.json {
"code": 500,
"err": err.Error(),
}
}
// uid := "70f6a615-c0d5-4315-a5ac-34ca845450ed"

id,err:=community.SaveMedia(context.Background(), uid, bytes)
if err!=nil {
zlog.Error("save file",err.Error())
Expand Down
Loading

0 comments on commit 8dd6c4a

Please sign in to comment.