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,17 @@
package config
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/zrpc"
)
type Config struct {
zrpc.RpcServerConf
SnowflakeRpcConf zrpc.RpcClientConf
DB struct {
Master string
Slaves string
}
CacheConf cache.CacheConf
}
@@ -0,0 +1,55 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type AddCommentLikesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewAddCommentLikesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCommentLikesLogic {
return &AddCommentLikesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// -----------------------commentLikes-----------------------
func (l *AddCommentLikesLogic) AddCommentLikes(in *pb.AddCommentLikesReq) (*pb.AddCommentLikesResp, error) {
if in.GetCommentId() <= 0 || in.GetUserId() <= 0 {
return nil, errors.New("commentId and userId are required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
comment, ok := store.Comments[in.GetCommentId()]
if !ok || comment.GetDeletedAt() > 0 {
return nil, errors.New("comment not found")
}
key := commentLikeKey(in.GetCommentId(), in.GetUserId())
if _, exists := store.CommentLikes[key]; exists {
return &pb.AddCommentLikesResp{}, nil
}
store.CommentLikes[key] = &pb.CommentLikes{
CommentId: in.GetCommentId(),
UserId: in.GetUserId(),
CreatedAt: nowUnix(in.GetCreatedAt()),
}
comment.LikeCount++
return &pb.AddCommentLikesResp{}, nil
}
@@ -0,0 +1,62 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type AddCommentsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewAddCommentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddCommentsLogic {
return &AddCommentsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// -----------------------comments-----------------------
func (l *AddCommentsLogic) AddComments(in *pb.AddCommentsReq) (*pb.AddCommentsResp, error) {
if in.GetPostId() <= 0 {
return nil, errors.New("postId is required")
}
if in.GetAuthorId() <= 0 {
return nil, errors.New("authorId is required")
}
if in.GetContent() == "" {
return nil, errors.New("content is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
post, ok := store.Posts[in.GetPostId()]
if !ok || post.GetDeletedAt() > 0 {
return nil, errors.New("post not found")
}
now := nowUnix(in.GetCreatedAt())
comment := &pb.Comments{
Id: store.NextComment(),
PostId: in.GetPostId(),
AuthorId: in.GetAuthorId(),
Content: in.GetContent(),
LikeCount: 0,
CreatedAt: now,
}
store.Comments[comment.Id] = comment
post.CommentCount++
post.UpdatedAt = now
return &pb.AddCommentsResp{}, nil
}
@@ -0,0 +1,56 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type AddPostLikesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewAddPostLikesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddPostLikesLogic {
return &AddPostLikesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// -----------------------postLikes-----------------------
func (l *AddPostLikesLogic) AddPostLikes(in *pb.AddPostLikesReq) (*pb.AddPostLikesResp, error) {
// todo: add your logic here and delete this line
if in.GetPostId() <= 0 || in.GetUserId() <= 0 {
return nil, errors.New("postId and userId are required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
post, ok := store.Posts[in.GetPostId()]
if !ok || post.GetDeletedAt() > 0 {
return nil, errors.New("post not found")
}
key := postLikeKey(in.GetPostId(), in.GetUserId())
if _, exists := store.PostLikes[key]; exists {
return &pb.AddPostLikesResp{}, nil
}
store.PostLikes[key] = &pb.PostLikes{
PostId: in.GetPostId(),
UserId: in.GetUserId(),
CreatedAt: nowUnix(in.GetCreatedAt()),
}
post.LikeCount++
return &pb.AddPostLikesResp{}, nil
}
@@ -0,0 +1,67 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type AddPostsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewAddPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddPostsLogic {
return &AddPostsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
// -----------------------posts-----------------------
func (l *AddPostsLogic) AddPosts(in *pb.AddPostsReq) (*pb.AddPostsResp, error) {
if in.GetAuthorId() <= 0 {
return nil, errors.New("authorId is required")
}
if in.GetTitle() == "" {
return nil, errors.New("title is required")
}
if in.GetContent() == "" {
return nil, errors.New("content is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
now := nowUnix(in.GetCreatedAt())
post := &pb.Posts{
Id: store.NextPost(),
AuthorId: in.GetAuthorId(),
AuthorRole: in.GetAuthorRole(),
Title: in.GetTitle(),
Content: in.GetContent(),
Images: append([]string(nil), in.GetImages()...),
Tags: append([]string(nil), in.GetTags()...),
LinkedOrderId: in.GetLinkedOrderId(),
QuotedPostId: in.GetQuotedPostId(),
LikeCount: 0,
CommentCount: 0,
Pinned: in.GetPinned(),
SearchText: in.GetTitle() + " " + in.GetContent(),
CreatedAt: now,
UpdatedAt: now,
}
if post.AuthorRole == "" {
post.AuthorRole = "consumer"
}
store.Posts[post.Id] = post
return &pb.AddPostsResp{}, nil
}
@@ -0,0 +1,65 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type DelCommentLikesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewDelCommentLikesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelCommentLikesLogic {
return &DelCommentLikesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *DelCommentLikesLogic) DelCommentLikes(in *pb.DelCommentLikesReq) (*pb.DelCommentLikesResp, error) {
if in.GetId() <= 0 {
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
if in.UserId != nil && in.GetUserId() > 0 {
key := commentLikeKey(in.GetId(), in.GetUserId())
if _, ok := store.CommentLikes[key]; ok {
delete(store.CommentLikes, key)
if comment, ok := store.Comments[in.GetId()]; ok && comment.LikeCount > 0 {
comment.LikeCount--
}
}
return &pb.DelCommentLikesResp{}, nil
}
removed := int64(0)
for key, like := range store.CommentLikes {
if like.GetCommentId() == in.GetId() {
delete(store.CommentLikes, key)
removed++
}
}
if removed > 0 {
if comment, ok := store.Comments[in.GetId()]; ok {
if comment.LikeCount >= removed {
comment.LikeCount -= removed
} else {
comment.LikeCount = 0
}
}
}
return &pb.DelCommentLikesResp{}, nil
}
@@ -0,0 +1,50 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type DelCommentsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewDelCommentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelCommentsLogic {
return &DelCommentsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *DelCommentsLogic) DelComments(in *pb.DelCommentsReq) (*pb.DelCommentsResp, error) {
if in.GetId() <= 0 {
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
comment, ok := store.Comments[in.GetId()]
if !ok {
return &pb.DelCommentsResp{}, nil
}
if comment.GetDeletedAt() == 0 {
now := nowUnix(0)
comment.DeletedAt = now
if post, ok := store.Posts[comment.GetPostId()]; ok && post.GetDeletedAt() == 0 && post.CommentCount > 0 {
post.CommentCount--
post.UpdatedAt = now
}
}
return &pb.DelCommentsResp{}, nil
}
@@ -0,0 +1,65 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type DelPostLikesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewDelPostLikesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelPostLikesLogic {
return &DelPostLikesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *DelPostLikesLogic) DelPostLikes(in *pb.DelPostLikesReq) (*pb.DelPostLikesResp, error) {
if in.GetId() <= 0 {
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
if in.UserId != nil && in.GetUserId() > 0 {
key := postLikeKey(in.GetId(), in.GetUserId())
if _, ok := store.PostLikes[key]; ok {
delete(store.PostLikes, key)
if post, ok := store.Posts[in.GetId()]; ok && post.LikeCount > 0 {
post.LikeCount--
}
}
return &pb.DelPostLikesResp{}, nil
}
removed := int64(0)
for key, like := range store.PostLikes {
if like.GetPostId() == in.GetId() {
delete(store.PostLikes, key)
removed++
}
}
if removed > 0 {
if post, ok := store.Posts[in.GetId()]; ok {
if post.LikeCount >= removed {
post.LikeCount -= removed
} else {
post.LikeCount = 0
}
}
}
return &pb.DelPostLikesResp{}, nil
}
@@ -0,0 +1,45 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type DelPostsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewDelPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DelPostsLogic {
return &DelPostsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *DelPostsLogic) DelPosts(in *pb.DelPostsReq) (*pb.DelPostsResp, error) {
if in.GetId() <= 0 {
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
post, ok := store.Posts[in.GetId()]
if !ok {
return &pb.DelPostsResp{}, nil
}
now := nowUnix(0)
post.DeletedAt = now
post.UpdatedAt = now
return &pb.DelPostsResp{}, nil
}
@@ -0,0 +1,44 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCommentLikesByIdLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetCommentLikesByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCommentLikesByIdLogic {
return &GetCommentLikesByIdLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetCommentLikesByIdLogic) GetCommentLikesById(in *pb.GetCommentLikesByIdReq) (*pb.GetCommentLikesByIdResp, error) {
if in.GetId() <= 0 {
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.RLock()
defer store.Mu.RUnlock()
for _, like := range store.CommentLikes {
if like.GetCommentId() == in.GetId() {
cp := *like
return &pb.GetCommentLikesByIdResp{CommentLikes: &cp}, nil
}
}
return nil, errors.New("comment like not found")
}
@@ -0,0 +1,43 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type GetCommentsByIdLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetCommentsByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetCommentsByIdLogic {
return &GetCommentsByIdLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetCommentsByIdLogic) GetCommentsById(in *pb.GetCommentsByIdReq) (*pb.GetCommentsByIdResp, error) {
if in.GetId() <= 0 {
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.RLock()
defer store.Mu.RUnlock()
comment, ok := store.Comments[in.GetId()]
if !ok || comment.GetDeletedAt() > 0 {
return nil, errors.New("comment not found")
}
out := *comment
return &pb.GetCommentsByIdResp{Comments: &out}, nil
}
@@ -0,0 +1,44 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type GetPostLikesByIdLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetPostLikesByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostLikesByIdLogic {
return &GetPostLikesByIdLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetPostLikesByIdLogic) GetPostLikesById(in *pb.GetPostLikesByIdReq) (*pb.GetPostLikesByIdResp, error) {
if in.GetId() <= 0 {
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.RLock()
defer store.Mu.RUnlock()
for _, like := range store.PostLikes {
if like.GetPostId() == in.GetId() {
cp := *like
return &pb.GetPostLikesByIdResp{PostLikes: &cp}, nil
}
}
return nil, errors.New("post like not found")
}
@@ -0,0 +1,46 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type GetPostsByIdLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewGetPostsByIdLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPostsByIdLogic {
return &GetPostsByIdLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetPostsByIdLogic) GetPostsById(in *pb.GetPostsByIdReq) (*pb.GetPostsByIdResp, error) {
if in.GetId() <= 0 {
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.RLock()
defer store.Mu.RUnlock()
post, ok := store.Posts[in.GetId()]
if !ok || post.GetDeletedAt() > 0 {
return nil, errors.New("post not found")
}
out := *post
out.Images = append([]string(nil), post.Images...)
out.Tags = append([]string(nil), post.Tags...)
return &pb.GetPostsByIdResp{Posts: &out}, nil
}
@@ -0,0 +1,45 @@
package logic
import (
"sort"
"strconv"
"time"
"juwan-backend/app/community/rpc/pb"
)
func nowUnix(in int64) int64 {
if in > 0 {
return in
}
return time.Now().Unix()
}
func postLikeKey(postID, userID int64) string {
return strconv.FormatInt(postID, 10) + ":" + strconv.FormatInt(userID, 10)
}
func commentLikeKey(commentID, userID int64) string {
return strconv.FormatInt(commentID, 10) + ":" + strconv.FormatInt(userID, 10)
}
func sortPostsDesc(posts []*pb.Posts) {
sort.Slice(posts, func(i, j int) bool {
if posts[i].Pinned != posts[j].Pinned {
return posts[i].Pinned
}
if posts[i].CreatedAt == posts[j].CreatedAt {
return posts[i].Id > posts[j].Id
}
return posts[i].CreatedAt > posts[j].CreatedAt
})
}
func sortCommentsAsc(comments []*pb.Comments) {
sort.Slice(comments, func(i, j int) bool {
if comments[i].CreatedAt == comments[j].CreatedAt {
return comments[i].Id < comments[j].Id
}
return comments[i].CreatedAt < comments[j].CreatedAt
})
}
@@ -0,0 +1,61 @@
package logic
import (
"context"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type SearchCommentLikesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSearchCommentLikesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchCommentLikesLogic {
return &SearchCommentLikesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SearchCommentLikesLogic) SearchCommentLikes(in *pb.SearchCommentLikesReq) (*pb.SearchCommentLikesResp, error) {
limit := in.GetLimit()
if limit <= 0 {
limit = 20
}
offset := in.GetPage()
if offset < 0 {
offset = 0
}
store := l.svcCtx.Store
store.Mu.RLock()
defer store.Mu.RUnlock()
filtered := make([]*pb.CommentLikes, 0, len(store.CommentLikes))
for _, like := range store.CommentLikes {
if in.GetCommentId() > 0 && like.GetCommentId() != in.GetCommentId() {
continue
}
if in.GetUserId() > 0 && like.GetUserId() != in.GetUserId() {
continue
}
cp := *like
filtered = append(filtered, &cp)
}
if offset >= int64(len(filtered)) {
return &pb.SearchCommentLikesResp{CommentLikes: []*pb.CommentLikes{}}, nil
}
end := offset + limit
if end > int64(len(filtered)) {
end = int64(len(filtered))
}
return &pb.SearchCommentLikesResp{CommentLikes: filtered[offset:end]}, nil
}
@@ -0,0 +1,76 @@
package logic
import (
"context"
"strings"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type SearchCommentsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSearchCommentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchCommentsLogic {
return &SearchCommentsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SearchCommentsLogic) SearchComments(in *pb.SearchCommentsReq) (*pb.SearchCommentsResp, error) {
limit := in.GetLimit()
if limit <= 0 {
limit = 20
}
offset := in.GetPage()
if offset < 0 {
offset = 0
}
store := l.svcCtx.Store
store.Mu.RLock()
defer store.Mu.RUnlock()
filtered := make([]*pb.Comments, 0, len(store.Comments))
for _, c := range store.Comments {
if c.GetDeletedAt() > 0 {
continue
}
if in.GetId() > 0 && c.GetId() != in.GetId() {
continue
}
if in.GetPostId() > 0 && c.GetPostId() != in.GetPostId() {
continue
}
if in.GetAuthorId() > 0 && c.GetAuthorId() != in.GetAuthorId() {
continue
}
if in.Content != nil && !strings.Contains(strings.ToLower(c.GetContent()), strings.ToLower(in.GetContent())) {
continue
}
if in.LikeCount != nil && c.GetLikeCount() != in.GetLikeCount() {
continue
}
cc := *c
filtered = append(filtered, &cc)
}
sortCommentsAsc(filtered)
if offset >= int64(len(filtered)) {
return &pb.SearchCommentsResp{Comments: []*pb.Comments{}}, nil
}
end := offset + limit
if end > int64(len(filtered)) {
end = int64(len(filtered))
}
return &pb.SearchCommentsResp{Comments: filtered[offset:end]}, nil
}
@@ -0,0 +1,61 @@
package logic
import (
"context"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type SearchPostLikesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSearchPostLikesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchPostLikesLogic {
return &SearchPostLikesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SearchPostLikesLogic) SearchPostLikes(in *pb.SearchPostLikesReq) (*pb.SearchPostLikesResp, error) {
limit := in.GetLimit()
if limit <= 0 {
limit = 20
}
offset := in.GetPage()
if offset < 0 {
offset = 0
}
store := l.svcCtx.Store
store.Mu.RLock()
defer store.Mu.RUnlock()
filtered := make([]*pb.PostLikes, 0, len(store.PostLikes))
for _, like := range store.PostLikes {
if in.PostId != nil && like.GetPostId() != in.GetPostId() {
continue
}
if in.UserId != nil && like.GetUserId() != in.GetUserId() {
continue
}
cp := *like
filtered = append(filtered, &cp)
}
if offset >= int64(len(filtered)) {
return &pb.SearchPostLikesResp{PostLikes: []*pb.PostLikes{}}, nil
}
end := offset + limit
if end > int64(len(filtered)) {
end = int64(len(filtered))
}
return &pb.SearchPostLikesResp{PostLikes: filtered[offset:end]}, nil
}
@@ -0,0 +1,96 @@
package logic
import (
"context"
"strings"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type SearchPostsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewSearchPostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SearchPostsLogic {
return &SearchPostsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *SearchPostsLogic) SearchPosts(in *pb.SearchPostsReq) (*pb.SearchPostsResp, error) {
limit := in.GetLimit()
if limit <= 0 {
limit = 20
}
offset := in.GetPage()
if offset < 0 {
offset = 0
}
store := l.svcCtx.Store
store.Mu.RLock()
defer store.Mu.RUnlock()
filtered := make([]*pb.Posts, 0, len(store.Posts))
for _, p := range store.Posts {
if p.GetDeletedAt() > 0 {
continue
}
if in.GetId() > 0 && p.GetId() != in.GetId() {
continue
}
if in.AuthorId != nil && p.GetAuthorId() != in.GetAuthorId() {
continue
}
if in.AuthorRole != nil && p.GetAuthorRole() != in.GetAuthorRole() {
continue
}
if in.Title != nil && !strings.Contains(strings.ToLower(p.GetTitle()), strings.ToLower(in.GetTitle())) {
continue
}
if in.Content != nil && !strings.Contains(strings.ToLower(p.GetContent()), strings.ToLower(in.GetContent())) {
continue
}
if len(in.GetTags()) > 0 {
match := false
for _, t := range in.GetTags() {
for _, pt := range p.GetTags() {
if t == pt {
match = true
break
}
}
if match {
break
}
}
if !match {
continue
}
}
cp := *p
cp.Images = append([]string(nil), p.Images...)
cp.Tags = append([]string(nil), p.Tags...)
filtered = append(filtered, &cp)
}
sortPostsDesc(filtered)
if offset >= int64(len(filtered)) {
return &pb.SearchPostsResp{Posts: []*pb.Posts{}}, nil
}
end := offset + limit
if end > int64(len(filtered)) {
end = int64(len(filtered))
}
return &pb.SearchPostsResp{Posts: filtered[offset:end]}, nil
}
@@ -0,0 +1,47 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateCommentLikesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateCommentLikesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCommentLikesLogic {
return &UpdateCommentLikesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdateCommentLikesLogic) UpdateCommentLikes(in *pb.UpdateCommentLikesReq) (*pb.UpdateCommentLikesResp, error) {
if in.GetCommentId() <= 0 || in.GetUserId() <= 0 {
return nil, errors.New("commentId and userId are required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
if _, ok := store.Comments[in.GetCommentId()]; !ok {
return nil, errors.New("comment not found")
}
key := commentLikeKey(in.GetCommentId(), in.GetUserId())
store.CommentLikes[key] = &pb.CommentLikes{
CommentId: in.GetCommentId(),
UserId: in.GetUserId(),
CreatedAt: nowUnix(in.GetCreatedAt()),
}
return &pb.UpdateCommentLikesResp{}, nil
}
@@ -0,0 +1,61 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateCommentsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateCommentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateCommentsLogic {
return &UpdateCommentsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdateCommentsLogic) UpdateComments(in *pb.UpdateCommentsReq) (*pb.UpdateCommentsResp, error) {
if in.GetId() <= 0 {
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
comment, ok := store.Comments[in.GetId()]
if !ok || comment.GetDeletedAt() > 0 {
return nil, errors.New("comment not found")
}
if in.GetPostId() > 0 {
comment.PostId = in.GetPostId()
}
if in.GetAuthorId() > 0 {
comment.AuthorId = in.GetAuthorId()
}
if in.GetContent() != "" {
comment.Content = in.GetContent()
}
if in.GetLikeCount() > 0 {
comment.LikeCount = in.GetLikeCount()
}
if in.GetDeletedAt() > 0 {
comment.DeletedAt = in.GetDeletedAt()
}
if in.GetCreatedAt() > 0 {
comment.CreatedAt = in.GetCreatedAt()
}
return &pb.UpdateCommentsResp{}, nil
}
@@ -0,0 +1,49 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdatePostLikesLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdatePostLikesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePostLikesLogic {
return &UpdatePostLikesLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdatePostLikesLogic) UpdatePostLikes(in *pb.UpdatePostLikesReq) (*pb.UpdatePostLikesResp, error) {
if in.PostId == nil || in.UserId == nil {
return nil, errors.New("postId and userId are required")
}
postID := in.GetPostId()
userID := in.GetUserId()
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
if _, ok := store.Posts[postID]; !ok {
return nil, errors.New("post not found")
}
key := postLikeKey(postID, userID)
store.PostLikes[key] = &pb.PostLikes{
PostId: postID,
UserId: userID,
CreatedAt: nowUnix(in.GetCreatedAt()),
}
return &pb.UpdatePostLikesResp{}, nil
}
@@ -0,0 +1,86 @@
package logic
import (
"context"
"errors"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdatePostsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdatePostsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePostsLogic {
return &UpdatePostsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdatePostsLogic) UpdatePosts(in *pb.UpdatePostsReq) (*pb.UpdatePostsResp, error) {
if in.GetId() <= 0 {
return nil, errors.New("id is required")
}
store := l.svcCtx.Store
store.Mu.Lock()
defer store.Mu.Unlock()
post, ok := store.Posts[in.GetId()]
if !ok || post.GetDeletedAt() > 0 {
return nil, errors.New("post not found")
}
if in.AuthorId != nil {
post.AuthorId = in.GetAuthorId()
}
if in.AuthorRole != nil {
post.AuthorRole = in.GetAuthorRole()
}
if in.Title != nil {
post.Title = in.GetTitle()
}
if in.Content != nil {
post.Content = in.GetContent()
}
if len(in.Images) > 0 {
post.Images = append([]string(nil), in.GetImages()...)
}
if len(in.Tags) > 0 {
post.Tags = append([]string(nil), in.GetTags()...)
}
if in.LinkedOrderId != nil {
post.LinkedOrderId = in.GetLinkedOrderId()
}
if in.QuotedPostId != nil {
post.QuotedPostId = in.GetQuotedPostId()
}
if in.LikeCount != nil {
post.LikeCount = in.GetLikeCount()
}
if in.CommentCount != nil {
post.CommentCount = in.GetCommentCount()
}
if in.Pinned != nil {
post.Pinned = in.GetPinned()
}
if in.SearchText != nil {
post.SearchText = in.GetSearchText()
}
if in.DeletedAt != nil {
post.DeletedAt = in.GetDeletedAt()
}
if in.CreatedAt != nil {
post.CreatedAt = in.GetCreatedAt()
}
post.UpdatedAt = nowUnix(in.GetUpdatedAt())
return &pb.UpdatePostsResp{}, nil
}
@@ -0,0 +1,128 @@
// Code generated by goctl. DO NOT EDIT.
// goctl 1.9.2
// Source: community.proto
package server
import (
"context"
"juwan-backend/app/community/rpc/internal/logic"
"juwan-backend/app/community/rpc/internal/svc"
"juwan-backend/app/community/rpc/pb"
)
type CommunityServiceServer struct {
svcCtx *svc.ServiceContext
pb.UnimplementedCommunityServiceServer
}
func NewCommunityServiceServer(svcCtx *svc.ServiceContext) *CommunityServiceServer {
return &CommunityServiceServer{
svcCtx: svcCtx,
}
}
// -----------------------commentLikes-----------------------
func (s *CommunityServiceServer) AddCommentLikes(ctx context.Context, in *pb.AddCommentLikesReq) (*pb.AddCommentLikesResp, error) {
l := logic.NewAddCommentLikesLogic(ctx, s.svcCtx)
return l.AddCommentLikes(in)
}
func (s *CommunityServiceServer) UpdateCommentLikes(ctx context.Context, in *pb.UpdateCommentLikesReq) (*pb.UpdateCommentLikesResp, error) {
l := logic.NewUpdateCommentLikesLogic(ctx, s.svcCtx)
return l.UpdateCommentLikes(in)
}
func (s *CommunityServiceServer) DelCommentLikes(ctx context.Context, in *pb.DelCommentLikesReq) (*pb.DelCommentLikesResp, error) {
l := logic.NewDelCommentLikesLogic(ctx, s.svcCtx)
return l.DelCommentLikes(in)
}
func (s *CommunityServiceServer) GetCommentLikesById(ctx context.Context, in *pb.GetCommentLikesByIdReq) (*pb.GetCommentLikesByIdResp, error) {
l := logic.NewGetCommentLikesByIdLogic(ctx, s.svcCtx)
return l.GetCommentLikesById(in)
}
func (s *CommunityServiceServer) SearchCommentLikes(ctx context.Context, in *pb.SearchCommentLikesReq) (*pb.SearchCommentLikesResp, error) {
l := logic.NewSearchCommentLikesLogic(ctx, s.svcCtx)
return l.SearchCommentLikes(in)
}
// -----------------------comments-----------------------
func (s *CommunityServiceServer) AddComments(ctx context.Context, in *pb.AddCommentsReq) (*pb.AddCommentsResp, error) {
l := logic.NewAddCommentsLogic(ctx, s.svcCtx)
return l.AddComments(in)
}
func (s *CommunityServiceServer) UpdateComments(ctx context.Context, in *pb.UpdateCommentsReq) (*pb.UpdateCommentsResp, error) {
l := logic.NewUpdateCommentsLogic(ctx, s.svcCtx)
return l.UpdateComments(in)
}
func (s *CommunityServiceServer) DelComments(ctx context.Context, in *pb.DelCommentsReq) (*pb.DelCommentsResp, error) {
l := logic.NewDelCommentsLogic(ctx, s.svcCtx)
return l.DelComments(in)
}
func (s *CommunityServiceServer) GetCommentsById(ctx context.Context, in *pb.GetCommentsByIdReq) (*pb.GetCommentsByIdResp, error) {
l := logic.NewGetCommentsByIdLogic(ctx, s.svcCtx)
return l.GetCommentsById(in)
}
func (s *CommunityServiceServer) SearchComments(ctx context.Context, in *pb.SearchCommentsReq) (*pb.SearchCommentsResp, error) {
l := logic.NewSearchCommentsLogic(ctx, s.svcCtx)
return l.SearchComments(in)
}
// -----------------------postLikes-----------------------
func (s *CommunityServiceServer) AddPostLikes(ctx context.Context, in *pb.AddPostLikesReq) (*pb.AddPostLikesResp, error) {
l := logic.NewAddPostLikesLogic(ctx, s.svcCtx)
return l.AddPostLikes(in)
}
func (s *CommunityServiceServer) UpdatePostLikes(ctx context.Context, in *pb.UpdatePostLikesReq) (*pb.UpdatePostLikesResp, error) {
l := logic.NewUpdatePostLikesLogic(ctx, s.svcCtx)
return l.UpdatePostLikes(in)
}
func (s *CommunityServiceServer) DelPostLikes(ctx context.Context, in *pb.DelPostLikesReq) (*pb.DelPostLikesResp, error) {
l := logic.NewDelPostLikesLogic(ctx, s.svcCtx)
return l.DelPostLikes(in)
}
func (s *CommunityServiceServer) GetPostLikesById(ctx context.Context, in *pb.GetPostLikesByIdReq) (*pb.GetPostLikesByIdResp, error) {
l := logic.NewGetPostLikesByIdLogic(ctx, s.svcCtx)
return l.GetPostLikesById(in)
}
func (s *CommunityServiceServer) SearchPostLikes(ctx context.Context, in *pb.SearchPostLikesReq) (*pb.SearchPostLikesResp, error) {
l := logic.NewSearchPostLikesLogic(ctx, s.svcCtx)
return l.SearchPostLikes(in)
}
// -----------------------posts-----------------------
func (s *CommunityServiceServer) AddPosts(ctx context.Context, in *pb.AddPostsReq) (*pb.AddPostsResp, error) {
l := logic.NewAddPostsLogic(ctx, s.svcCtx)
return l.AddPosts(in)
}
func (s *CommunityServiceServer) UpdatePosts(ctx context.Context, in *pb.UpdatePostsReq) (*pb.UpdatePostsResp, error) {
l := logic.NewUpdatePostsLogic(ctx, s.svcCtx)
return l.UpdatePosts(in)
}
func (s *CommunityServiceServer) DelPosts(ctx context.Context, in *pb.DelPostsReq) (*pb.DelPostsResp, error) {
l := logic.NewDelPostsLogic(ctx, s.svcCtx)
return l.DelPosts(in)
}
func (s *CommunityServiceServer) GetPostsById(ctx context.Context, in *pb.GetPostsByIdReq) (*pb.GetPostsByIdResp, error) {
l := logic.NewGetPostsByIdLogic(ctx, s.svcCtx)
return l.GetPostsById(in)
}
func (s *CommunityServiceServer) SearchPosts(ctx context.Context, in *pb.SearchPostsReq) (*pb.SearchPostsResp, error) {
l := logic.NewSearchPostsLogic(ctx, s.svcCtx)
return l.SearchPosts(in)
}
@@ -0,0 +1,15 @@
package svc
import "juwan-backend/app/community/rpc/internal/config"
type ServiceContext struct {
Config config.Config
Store *CommunityStore
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
Store: NewCommunityStore(),
}
}
+40
View File
@@ -0,0 +1,40 @@
package svc
import (
"sync"
"juwan-backend/app/community/rpc/pb"
)
type CommunityStore struct {
Mu sync.RWMutex
nextPostID int64
nextCommentID int64
Posts map[int64]*pb.Posts
Comments map[int64]*pb.Comments
PostLikes map[string]*pb.PostLikes
CommentLikes map[string]*pb.CommentLikes
}
func NewCommunityStore() *CommunityStore {
return &CommunityStore{
nextPostID: 1000,
nextCommentID: 1000,
Posts: make(map[int64]*pb.Posts),
Comments: make(map[int64]*pb.Comments),
PostLikes: make(map[string]*pb.PostLikes),
CommentLikes: make(map[string]*pb.CommentLikes),
}
}
func (s *CommunityStore) NextPost() int64 {
s.nextPostID++
return s.nextPostID
}
func (s *CommunityStore) NextComment() int64 {
s.nextCommentID++
return s.nextCommentID
}