Refactor: Remove deprecated gRPC service files and implement new API structure

- Deleted old gRPC service definitions in `game_grpc.pb.go` and `public.go`.
- Added new API server implementations for objectstory, player, and shop services.
- Introduced configuration files for new APIs in `etc/*.yaml`.
- Created main entry points for each service in `objectstory.go`, `player.go`, and `shop.go`.
- Removed unused user update handler and user API files.
- Added utility functions for context management and HTTP header parsing.
- Introduced PostgreSQL backup configuration in `backup/postgreSql.yaml`.
This commit is contained in:
wwweww
2026-02-28 18:35:56 +08:00
parent d2f33b4b96
commit 19cc7a778c
349 changed files with 42548 additions and 1453 deletions
@@ -0,0 +1,15 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package config
import (
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/zrpc"
)
type Config struct {
rest.RestConf
CommunityRpcConf zrpc.RpcClientConf
UsercenterRpcConf zrpc.RpcClientConf
}
@@ -0,0 +1,37 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
"github.com/zeromicro/go-zero/rest/httpx"
)
// 发表评论
func CreateCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req struct {
types.PathId
types.CreateCommentReq
}
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
req.CreateCommentReq.PostId = req.PathId.Id
l := community.NewCreateCommentLogic(r.Context(), svcCtx)
resp, err := l.CreateComment(&req.CreateCommentReq)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 发布帖子
func CreatePostHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.CreatePostReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewCreatePostLogic(r.Context(), svcCtx)
resp, err := l.CreatePost(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 获取帖子详情
func GetPostHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PathId
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewGetPostLogic(r.Context(), svcCtx)
resp, err := l.GetPost(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 点赞评论
func LikeCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PathId
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewLikeCommentLogic(r.Context(), svcCtx)
resp, err := l.LikeComment(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 点赞帖子
func LikePostHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PathId
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewLikePostLogic(r.Context(), svcCtx)
resp, err := l.LikePost(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 获取帖子评论
func ListCommentsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ListCommentsReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewListCommentsLogic(r.Context(), svcCtx)
resp, err := l.ListComments(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 获取帖子列表
func ListPostsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PostListReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewListPostsLogic(r.Context(), svcCtx)
resp, err := l.ListPosts(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 获取用户帖子
func ListUserPostsHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.ListCommentsReq
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewListUserPostsLogic(r.Context(), svcCtx)
resp, err := l.ListUserPosts(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 置顶帖子
func PinPostHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PathId
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewPinPostLogic(r.Context(), svcCtx)
resp, err := l.PinPost(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 取消点赞评论
func UnlikeCommentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PathId
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewUnlikeCommentLogic(r.Context(), svcCtx)
resp, err := l.UnlikeComment(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 取消点赞帖子
func UnlikePostHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PathId
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewUnlikePostLogic(r.Context(), svcCtx)
resp, err := l.UnlikePost(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,32 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"juwan-backend/app/community/api/internal/logic/community"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
)
// 取消置顶
func UnpinPostHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.PathId
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := community.NewUnpinPostLogic(r.Context(), svcCtx)
resp, err := l.UnpinPost(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
@@ -0,0 +1,99 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.9.2
package handler
import (
"net/http"
community "juwan-backend/app/community/api/internal/handler/community"
"juwan-backend/app/community/api/internal/svc"
"github.com/zeromicro/go-zero/rest"
)
func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes(
[]rest.Route{
{
// 获取帖子列表
Method: http.MethodGet,
Path: "/posts",
Handler: community.ListPostsHandler(serverCtx),
},
{
// 获取帖子详情
Method: http.MethodGet,
Path: "/posts/:id",
Handler: community.GetPostHandler(serverCtx),
},
{
// 获取帖子评论
Method: http.MethodGet,
Path: "/posts/:id/comments",
Handler: community.ListCommentsHandler(serverCtx),
},
{
// 获取用户帖子
Method: http.MethodGet,
Path: "/users/:id/posts",
Handler: community.ListUserPostsHandler(serverCtx),
},
},
rest.WithPrefix("/api/v1"),
)
server.AddRoutes(
[]rest.Route{
{
// 点赞评论
Method: http.MethodPost,
Path: "/comments/:id/like",
Handler: community.LikeCommentHandler(serverCtx),
},
{
// 取消点赞评论
Method: http.MethodDelete,
Path: "/comments/:id/like",
Handler: community.UnlikeCommentHandler(serverCtx),
},
{
// 发布帖子
Method: http.MethodPost,
Path: "/posts",
Handler: community.CreatePostHandler(serverCtx),
},
{
// 发表评论
Method: http.MethodPost,
Path: "/posts/:id/comments",
Handler: community.CreateCommentHandler(serverCtx),
},
{
// 点赞帖子
Method: http.MethodPost,
Path: "/posts/:id/like",
Handler: community.LikePostHandler(serverCtx),
},
{
// 取消点赞帖子
Method: http.MethodDelete,
Path: "/posts/:id/like",
Handler: community.UnlikePostHandler(serverCtx),
},
{
// 置顶帖子
Method: http.MethodPost,
Path: "/posts/:id/pin",
Handler: community.PinPostHandler(serverCtx),
},
{
// 取消置顶
Method: http.MethodDelete,
Path: "/posts/:id/pin",
Handler: community.UnpinPostHandler(serverCtx),
},
},
rest.WithPrefix("/api/v1"),
)
}
@@ -0,0 +1,57 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type CreateCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 发表评论
func NewCreateCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateCommentLogic {
return &CreateCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CreateCommentLogic) CreateComment(req *types.CreateCommentReq) (resp *types.Comment, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
_, err = l.svcCtx.CommunityRpc.AddComments(l.ctx, &communitypb.AddCommentsReq{
PostId: req.PostId,
AuthorId: uid,
Content: req.Content,
})
if err != nil {
return nil, err
}
comments, err := l.svcCtx.CommunityRpc.SearchComments(l.ctx, &communitypb.SearchCommentsReq{
Page: 0,
Limit: 1,
PostId: req.PostId,
AuthorId: uid,
})
if err != nil || len(comments.GetComments()) == 0 {
return nil, err
}
out := mapComment(l.ctx, l.svcCtx, comments.GetComments()[len(comments.GetComments())-1], false)
return &out, nil
}
@@ -0,0 +1,65 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"strconv"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type CreatePostLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 发布帖子
func NewCreatePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreatePostLogic {
return &CreatePostLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CreatePostLogic) CreatePost(req *types.CreatePostReq) (resp *types.Post, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
linkedOrderID := int64(0)
if req.LinkedOrderId != "" {
linkedOrderID, _ = strconv.ParseInt(req.LinkedOrderId, 10, 64)
}
_, err = l.svcCtx.CommunityRpc.AddPosts(l.ctx, &communitypb.AddPostsReq{
AuthorId: uid,
AuthorRole: "consumer",
Title: req.Title,
Content: req.Content,
Images: req.Images,
Tags: req.Tags,
LinkedOrderId: linkedOrderID,
})
if err != nil {
return nil, err
}
posts, err := l.svcCtx.CommunityRpc.SearchPosts(l.ctx, &communitypb.SearchPostsReq{
Page: 0,
Limit: 1,
AuthorId: &uid,
})
if err != nil || len(posts.GetPosts()) == 0 {
return nil, err
}
out := mapPost(l.ctx, l.svcCtx, posts.GetPosts()[0], false)
return &out, nil
}
@@ -0,0 +1,45 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type GetPostLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取帖子详情
func NewGetPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostLogic {
return &GetPostLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetPostLogic) GetPost(req *types.PathId) (resp *types.Post, err error) {
out, err := l.svcCtx.CommunityRpc.GetPostsById(l.ctx, &communitypb.GetPostsByIdReq{Id: req.Id})
if err != nil {
return nil, err
}
uid := currentUserIDOrZero(l.ctx)
liked := false
if uid > 0 {
likes, e := l.svcCtx.CommunityRpc.SearchPostLikes(l.ctx, &communitypb.SearchPostLikesReq{Page: 0, Limit: 1, PostId: &req.Id, UserId: &uid})
liked = e == nil && len(likes.GetPostLikes()) > 0
}
post := mapPost(l.ctx, l.svcCtx, out.GetPosts(), liked)
return &post, nil
}
@@ -0,0 +1,69 @@
package community
import (
"context"
"strconv"
"time"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"juwan-backend/app/users/rpc/usercenter"
"juwan-backend/common/utils/contextj"
)
func currentUserIDOrZero(ctx context.Context) int64 {
uid, err := contextj.UserIDFrom(ctx)
if err != nil {
return 0
}
return uid
}
func buildUserProfile(ctx context.Context, svcCtx *svc.ServiceContext, userID int64) types.UserProfile {
if userID <= 0 {
return types.UserProfile{}
}
resp, err := svcCtx.UserRpc.GetUsersById(ctx, &usercenter.GetUsersByIdReq{Id: userID})
if err != nil || resp == nil || resp.Users == nil {
id := strconv.FormatInt(userID, 10)
return types.UserProfile{Id: id, Username: id, Nickname: "用户" + id, CreatedAt: time.Now().UTC().Format(time.RFC3339)}
}
u := resp.Users
return types.UserProfile{
Id: strconv.FormatInt(u.GetId(), 10),
Username: u.GetUsername(),
Nickname: u.GetNickname(),
Avatar: u.GetAvatar(),
Role: u.GetCurrentRole(),
Phone: u.GetPhone(),
Bio: u.GetBio(),
CreatedAt: time.Unix(u.GetCreatedAt(), 0).UTC().Format(time.RFC3339),
}
}
func mapPost(ctx context.Context, svcCtx *svc.ServiceContext, p *communitypb.Posts, liked bool) types.Post {
return types.Post{
Id: p.GetId(),
Title: p.GetTitle(),
Content: p.GetContent(),
Images: append([]string(nil), p.GetImages()...),
Tags: append([]string(nil), p.GetTags()...),
LikeCount: p.GetLikeCount(),
CommentCount: p.GetCommentCount(),
Liked: liked,
Author: buildUserProfile(ctx, svcCtx, p.GetAuthorId()),
CreatedAt: time.Unix(p.GetCreatedAt(), 0).UTC().Format(time.RFC3339),
}
}
func mapComment(ctx context.Context, svcCtx *svc.ServiceContext, c *communitypb.Comments, liked bool) types.Comment {
return types.Comment{
Id: c.GetId(),
Content: c.GetContent(),
Author: buildUserProfile(ctx, svcCtx, c.GetAuthorId()),
LikeCount: c.GetLikeCount(),
Liked: liked,
CreatedAt: time.Unix(c.GetCreatedAt(), 0).UTC().Format(time.RFC3339),
}
}
@@ -0,0 +1,43 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type LikeCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 点赞评论
func NewLikeCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeCommentLogic {
return &LikeCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *LikeCommentLogic) LikeComment(req *types.PathId) (resp *types.EmptyResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
_, err = l.svcCtx.CommunityRpc.AddCommentLikes(l.ctx, &communitypb.AddCommentLikesReq{CommentId: req.Id, UserId: uid})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -0,0 +1,43 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type LikePostLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 点赞帖子
func NewLikePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikePostLogic {
return &LikePostLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *LikePostLogic) LikePost(req *types.PathId) (resp *types.EmptyResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
_, err = l.svcCtx.CommunityRpc.AddPostLikes(l.ctx, &communitypb.AddPostLikesReq{PostId: req.Id, UserId: uid})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -0,0 +1,55 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type ListCommentsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取帖子评论
func NewListCommentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListCommentsLogic {
return &ListCommentsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ListCommentsLogic) ListComments(req *types.ListCommentsReq) (resp *types.CommentListResp, err error) {
if req.Limit <= 0 {
req.Limit = 20
}
out, err := l.svcCtx.CommunityRpc.SearchComments(l.ctx, &communitypb.SearchCommentsReq{
Page: req.Offset,
Limit: req.Limit,
PostId: req.Id,
})
if err != nil {
return nil, err
}
uid := currentUserIDOrZero(l.ctx)
items := make([]types.Comment, 0, len(out.GetComments()))
for _, c := range out.GetComments() {
liked := false
if uid > 0 {
likes, e := l.svcCtx.CommunityRpc.SearchCommentLikes(l.ctx, &communitypb.SearchCommentLikesReq{Page: 0, Limit: 1, CommentId: c.Id, UserId: uid})
liked = e == nil && len(likes.GetCommentLikes()) > 0
}
items = append(items, mapComment(l.ctx, l.svcCtx, c, liked))
}
return &types.CommentListResp{Items: items, Meta: types.PageMeta{Total: int64(len(items)), Offset: req.Offset, Limit: req.Limit}}, nil
}
@@ -0,0 +1,56 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"strings"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type ListPostsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取帖子列表
func NewListPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListPostsLogic {
return &ListPostsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ListPostsLogic) ListPosts(req *types.PostListReq) (resp *types.PostListResp, err error) {
if req.Limit <= 0 {
req.Limit = 20
}
in := &communitypb.SearchPostsReq{Page: req.Offset, Limit: req.Limit}
if req.Tags != "" {
in.Tags = strings.Split(req.Tags, ",")
}
posts, err := l.svcCtx.CommunityRpc.SearchPosts(l.ctx, in)
if err != nil {
return nil, err
}
uid := currentUserIDOrZero(l.ctx)
items := make([]types.Post, 0, len(posts.GetPosts()))
for _, p := range posts.GetPosts() {
liked := false
if uid > 0 {
likes, e := l.svcCtx.CommunityRpc.SearchPostLikes(l.ctx, &communitypb.SearchPostLikesReq{Page: 0, Limit: 1, PostId: &p.Id, UserId: &uid})
liked = e == nil && len(likes.GetPostLikes()) > 0
}
items = append(items, mapPost(l.ctx, l.svcCtx, p, liked))
}
return &types.PostListResp{Items: items, Meta: types.PageMeta{Total: int64(len(items)), Offset: req.Offset, Limit: req.Limit}}, nil
}
@@ -0,0 +1,52 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type ListUserPostsLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 获取用户帖子
func NewListUserPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListUserPostsLogic {
return &ListUserPostsLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ListUserPostsLogic) ListUserPosts(req *types.ListCommentsReq) (resp *types.PostListResp, err error) {
if req.Limit <= 0 {
req.Limit = 20
}
authorID := req.Id
out, err := l.svcCtx.CommunityRpc.SearchPosts(l.ctx, &communitypb.SearchPostsReq{Page: req.Offset, Limit: req.Limit, AuthorId: &authorID})
if err != nil {
return nil, err
}
uid := currentUserIDOrZero(l.ctx)
items := make([]types.Post, 0, len(out.GetPosts()))
for _, p := range out.GetPosts() {
liked := false
if uid > 0 {
likes, e := l.svcCtx.CommunityRpc.SearchPostLikes(l.ctx, &communitypb.SearchPostLikesReq{Page: 0, Limit: 1, PostId: &p.Id, UserId: &uid})
liked = e == nil && len(likes.GetPostLikes()) > 0
}
items = append(items, mapPost(l.ctx, l.svcCtx, p, liked))
}
return &types.PostListResp{Items: items, Meta: types.PageMeta{Total: int64(len(items)), Offset: req.Offset, Limit: req.Limit}}, nil
}
@@ -0,0 +1,39 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type PinPostLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 置顶帖子
func NewPinPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PinPostLogic {
return &PinPostLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *PinPostLogic) PinPost(req *types.PathId) (resp *types.EmptyResp, err error) {
t := true
_, err = l.svcCtx.CommunityRpc.UpdatePosts(l.ctx, &communitypb.UpdatePostsReq{Id: req.Id, Pinned: &t})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -0,0 +1,43 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type UnlikeCommentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 取消点赞评论
func NewUnlikeCommentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UnlikeCommentLogic {
return &UnlikeCommentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UnlikeCommentLogic) UnlikeComment(req *types.PathId) (resp *types.EmptyResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
_, err = l.svcCtx.CommunityRpc.DelCommentLikes(l.ctx, &communitypb.DelCommentLikesReq{Id: req.Id, UserId: &uid})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -0,0 +1,43 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"juwan-backend/common/utils/contextj"
"github.com/zeromicro/go-zero/core/logx"
)
type UnlikePostLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 取消点赞帖子
func NewUnlikePostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UnlikePostLogic {
return &UnlikePostLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UnlikePostLogic) UnlikePost(req *types.PathId) (resp *types.EmptyResp, err error) {
uid, err := contextj.UserIDFrom(l.ctx)
if err != nil {
return nil, err
}
_, err = l.svcCtx.CommunityRpc.DelPostLikes(l.ctx, &communitypb.DelPostLikesReq{Id: req.Id, UserId: &uid})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -0,0 +1,39 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package community
import (
"context"
"juwan-backend/app/community/api/internal/svc"
"juwan-backend/app/community/api/internal/types"
communitypb "juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type UnpinPostLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
// 取消置顶
func NewUnpinPostLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UnpinPostLogic {
return &UnpinPostLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *UnpinPostLogic) UnpinPost(req *types.PathId) (resp *types.EmptyResp, err error) {
f := false
_, err = l.svcCtx.CommunityRpc.UpdatePosts(l.ctx, &communitypb.UpdatePostsReq{Id: req.Id, Pinned: &f})
if err != nil {
return nil, err
}
return &types.EmptyResp{}, nil
}
@@ -0,0 +1,26 @@
// Code scaffolded by goctl. Safe to edit.
// goctl 1.9.2
package svc
import (
"juwan-backend/app/community/api/internal/config"
"juwan-backend/app/community/rpc/communityservice"
"juwan-backend/app/users/rpc/usercenter"
"github.com/zeromicro/go-zero/zrpc"
)
type ServiceContext struct {
Config config.Config
CommunityRpc communityservice.CommunityService
UserRpc usercenter.Usercenter
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
CommunityRpc: communityservice.NewCommunityService(zrpc.MustNewClient(c.CommunityRpcConf)),
UserRpc: usercenter.NewUsercenter(zrpc.MustNewClient(c.UsercenterRpcConf)),
}
}
+97
View File
@@ -0,0 +1,97 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.9.2
package types
type Comment struct {
Id int64 `json:"id"`
Content string `json:"content"`
Author UserProfile `json:"author"`
LikeCount int64 `json:"likeCount"`
Liked bool `json:"liked"`
CreatedAt string `json:"createdAt"`
}
type CommentListResp struct {
Items []Comment `json:"items"`
Meta PageMeta `json:"meta"`
}
type CreateCommentReq struct {
PostId int64 `json:"-"`
Content string `json:"content"`
}
type CreatePostReq struct {
Title string `json:"title"`
Content string `json:"content"`
Images []string `json:"images"`
Tags []string `json:"tags"`
LinkedOrderId string `json:"linkedOrderId,optional"`
}
type EmptyResp struct {
}
type ListCommentsReq struct {
PathId
PageReq
}
type PageMeta struct {
Total int64 `json:"total"`
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
}
type PageReq struct {
Offset int64 `form:"offset,default=0"`
Limit int64 `form:"limit,default=20"`
}
type PathId struct {
Id int64 `path:"id"`
}
type Post struct {
Id int64 `json:"id"`
Title string `json:"title"`
Content string `json:"content"`
Images []string `json:"images"`
Tags []string `json:"tags"`
LikeCount int64 `json:"likeCount"`
CommentCount int64 `json:"commentCount"`
Liked bool `json:"liked"`
Author UserProfile `json:"author"`
CreatedAt string `json:"createdAt"`
}
type PostListReq struct {
PageReq
Tags string `form:"tags,optional"`
SortBy string `form:"sortBy,optional"`
}
type PostListResp struct {
Items []Post `json:"items"`
Meta PageMeta `json:"meta"`
}
type SimpleUser struct {
Id string `json:"id"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
}
type UserProfile struct {
Id string `json:"id"`
Username string `json:"username"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Role string `json:"role"` // consumer, player, owner, admin
VerifiedRoles []string `json:"verifiedRoles"`
VerificationStatus map[string]string `json:"verificationStatus"`
Phone string `json:"phone,optional"`
Bio string `json:"bio,optional"`
CreatedAt string `json:"createdAt"`
}