Skip to content

Commit f409eaa

Browse files
committed
feat: 添加评论模块
1 parent 2110103 commit f409eaa

File tree

9 files changed

+298
-0
lines changed

9 files changed

+298
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package domain
2+
3+
import (
4+
"time"
5+
6+
"go.mongodb.org/mongo-driver/v2/bson"
7+
)
8+
9+
type Comment struct {
10+
Id bson.ObjectID
11+
CreatedAt time.Time
12+
UpdatedAt time.Time
13+
DeletedAt time.Time
14+
Path string
15+
Content string
16+
RootId bson.ObjectID
17+
ParentId bson.ObjectID
18+
Nickname string
19+
Avatar string
20+
Email string
21+
SiteUrl string
22+
IsAdmin bool
23+
}
24+
25+
type CommentShow struct {
26+
Id bson.ObjectID
27+
CreatedAt time.Time
28+
UpdatedAt time.Time
29+
DeletedAt time.Time
30+
Path string
31+
Content string
32+
Nickname string
33+
Avatar string
34+
SiteUrl string
35+
IsAdmin bool
36+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package repository
2+
3+
import (
4+
"context"
5+
6+
"github.com/codepzj/Stellux-Server/internal/comment/internal/domain"
7+
"github.com/codepzj/Stellux-Server/internal/comment/internal/repository/dao"
8+
"github.com/samber/lo"
9+
"go.mongodb.org/mongo-driver/v2/bson"
10+
)
11+
12+
type ICommentRepository interface {
13+
Create(ctx context.Context, comment *domain.Comment) error
14+
GetListByPath(ctx context.Context, path string) ([]*domain.CommentShow, error)
15+
Update(ctx context.Context, comment *domain.Comment) error
16+
Delete(ctx context.Context, id bson.ObjectID) error
17+
}
18+
19+
var _ ICommentRepository = (*CommentRepository)(nil)
20+
21+
func NewCommentRepository(dao dao.ICommentDao) *CommentRepository {
22+
return &CommentRepository{dao: dao}
23+
}
24+
25+
type CommentRepository struct {
26+
dao dao.ICommentDao
27+
}
28+
29+
// Create 创建评论
30+
func (r *CommentRepository) Create(ctx context.Context, comment *domain.Comment) error {
31+
return r.dao.Create(ctx, &dao.Comment{
32+
Path: comment.Path,
33+
Content: comment.Content,
34+
RootId: comment.RootId,
35+
ParentId: comment.ParentId,
36+
Nickname: comment.Nickname,
37+
Avatar: comment.Avatar,
38+
Email: comment.Email,
39+
SiteUrl: comment.SiteUrl,
40+
IsAdmin: comment.IsAdmin,
41+
})
42+
}
43+
44+
// GetListByPath 根据路径获取评论列表
45+
func (r *CommentRepository) GetListByPath(ctx context.Context, path string) ([]*domain.CommentShow, error) {
46+
comments, err := r.dao.GetList(ctx, bson.D{{Key: "path", Value: path}})
47+
if err != nil {
48+
return nil, err
49+
}
50+
return r.CommentDaoToShowDomainList(comments), nil
51+
}
52+
53+
// Update 更新评论
54+
func (r *CommentRepository) Update(ctx context.Context, comment *domain.Comment) error {
55+
return r.dao.Update(ctx, comment.Id, &dao.Comment{
56+
Content: comment.Content,
57+
Nickname: comment.Nickname,
58+
Avatar: comment.Avatar,
59+
SiteUrl: comment.SiteUrl,
60+
IsAdmin: comment.IsAdmin,
61+
})
62+
}
63+
64+
// Delete 删除评论
65+
func (r *CommentRepository) Delete(ctx context.Context, id bson.ObjectID) error {
66+
return r.dao.Delete(ctx, id)
67+
}
68+
69+
func (r *CommentRepository) CommentDaoToShowDomainList(comments []*dao.Comment) []*domain.CommentShow {
70+
return lo.Map(comments, func(comment *dao.Comment, _ int) *domain.CommentShow {
71+
return r.CommentDaoToShowDomain(comment)
72+
})
73+
}
74+
75+
// 转成前端展示的评论
76+
func (r *CommentRepository) CommentDaoToShowDomain(comment *dao.Comment) *domain.CommentShow {
77+
return &domain.CommentShow{
78+
Id: comment.ID,
79+
CreatedAt: comment.CreatedAt,
80+
UpdatedAt: comment.UpdatedAt,
81+
DeletedAt: comment.DeletedAt,
82+
Path: comment.Path,
83+
Content: comment.Content,
84+
Nickname: comment.Nickname,
85+
Avatar: comment.Avatar,
86+
SiteUrl: comment.SiteUrl,
87+
IsAdmin: comment.IsAdmin,
88+
}
89+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package dao
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/chenmingyong0423/go-mongox/v2"
8+
"github.com/chenmingyong0423/go-mongox/v2/builder/query"
9+
"go.mongodb.org/mongo-driver/v2/bson"
10+
)
11+
12+
type Comment struct {
13+
mongox.Model `bson:",inline"`
14+
Path string `bson:"path"`
15+
Content string `bson:"content"`
16+
RootId bson.ObjectID `bson:"root_id"` // 根评论Id
17+
ParentId bson.ObjectID `bson:"parent_id"` // 父级评论Id
18+
Nickname string `bson:"nickname"` // 昵称
19+
Avatar string `bson:"avatar"` // 头像
20+
Email string `bson:"email"` // 邮箱
21+
SiteUrl string `bson:"site_url"` // 链接
22+
IsAdmin bool `bson:"is_admin"` // 是否管理员
23+
}
24+
25+
type ICommentDao interface {
26+
Create(ctx context.Context, comment *Comment) error
27+
GetList(ctx context.Context, filter bson.D) ([]*Comment, error)
28+
Update(ctx context.Context, id bson.ObjectID, comment *Comment) error
29+
Delete(ctx context.Context, id bson.ObjectID) error
30+
}
31+
32+
var _ ICommentDao = (*CommentDao)(nil)
33+
34+
func NewCommentDao(db *mongox.Database) *CommentDao {
35+
return &CommentDao{coll: mongox.NewCollection[Comment](db, "comment")}
36+
}
37+
38+
type CommentDao struct {
39+
coll *mongox.Collection[Comment]
40+
}
41+
42+
// Create 创建评论
43+
func (d *CommentDao) Create(ctx context.Context, comment *Comment) error {
44+
res, err := d.coll.Creator().InsertOne(ctx, comment)
45+
if err != nil {
46+
return err
47+
}
48+
if res.InsertedID == nil {
49+
return errors.New("创建评论失败")
50+
}
51+
return nil
52+
}
53+
54+
// GetList 获取评论列表
55+
func (d *CommentDao) GetList(ctx context.Context, filter bson.D) ([]*Comment, error) {
56+
return d.coll.Finder().Filter(filter).Sort(bson.M{"created_at": -1}).Find(ctx)
57+
}
58+
59+
// Update 更新评论
60+
func (d *CommentDao) Update(ctx context.Context, id bson.ObjectID, comment *Comment) error {
61+
res, err := d.coll.Updater().Filter(query.Id(id)).Updates(map[string]any{
62+
"content": comment.Content,
63+
"nickname": comment.Nickname,
64+
"avatar": comment.Avatar,
65+
"email": comment.Email,
66+
"site_url": comment.SiteUrl,
67+
}).UpdateOne(ctx)
68+
if err != nil {
69+
return err
70+
}
71+
if res.MatchedCount == 0 {
72+
return errors.New("更新评论失败")
73+
}
74+
return nil
75+
}
76+
77+
// Delete 删除评论
78+
func (d *CommentDao) Delete(ctx context.Context, id bson.ObjectID) error {
79+
res, err := d.coll.Deleter().Filter(query.Id(id)).DeleteOne(ctx)
80+
if err != nil {
81+
return err
82+
}
83+
if res.DeletedCount == 0 {
84+
return errors.New("删除评论失败")
85+
}
86+
return nil
87+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package service
2+
3+
import (
4+
"github.com/codepzj/Stellux-Server/internal/comment/internal/repository"
5+
)
6+
7+
8+
type ICommentService interface {
9+
}
10+
11+
var _ ICommentService = (*CommentService)(nil)
12+
13+
func NewCommentService(repo repository.ICommentRepository) *CommentService {
14+
return &CommentService{
15+
repo: repo,
16+
}
17+
}
18+
19+
type CommentService struct {
20+
repo repository.ICommentRepository
21+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package web
2+
3+
import (
4+
"github.com/codepzj/Stellux-Server/internal/comment/internal/service"
5+
"github.com/gin-gonic/gin"
6+
)
7+
8+
func NewCommentHandler(serv service.ICommentService) *CommentHandler {
9+
return &CommentHandler{
10+
serv: serv,
11+
}
12+
}
13+
14+
type CommentHandler struct {
15+
serv service.ICommentService
16+
}
17+
18+
func (h *CommentHandler) RegisterGinRoutes(engine *gin.Engine) {
19+
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package web
2+
3+
type CommentRequest struct {}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package web
2+
3+
type CommentVO struct {}

internal/comment/module.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package comment
2+
3+
import (
4+
"github.com/codepzj/Stellux-Server/internal/comment/internal/service"
5+
"github.com/codepzj/Stellux-Server/internal/comment/internal/web"
6+
)
7+
8+
type (
9+
Handler = web.CommentHandler
10+
Service = service.ICommentService
11+
Module struct {
12+
Svc Service
13+
Hdl *Handler
14+
}
15+
)

internal/comment/wire.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//go:build wireinject
2+
3+
package comment
4+
5+
import (
6+
"github.com/chenmingyong0423/go-mongox/v2"
7+
"github.com/codepzj/Stellux-Server/internal/comment/internal/repository"
8+
"github.com/codepzj/Stellux-Server/internal/comment/internal/repository/dao"
9+
"github.com/codepzj/Stellux-Server/internal/comment/internal/service"
10+
"github.com/codepzj/Stellux-Server/internal/comment/internal/web"
11+
"github.com/google/wire"
12+
)
13+
14+
var CommentProviders = wire.NewSet(web.NewCommentHandler, service.NewCommentService, repository.NewCommentRepository, dao.NewCommentDao,
15+
wire.Bind(new(service.ICommentService), new(*service.CommentService)),
16+
wire.Bind(new(repository.ICommentRepository), new(*repository.CommentRepository)),
17+
wire.Bind(new(dao.ICommentDao), new(*dao.CommentDao)))
18+
19+
func InitCommentModule(mongoDB *mongox.Database) *Module {
20+
panic(wire.Build(
21+
CommentProviders,
22+
wire.Struct(new(Module), "Svc", "Hdl"),
23+
))
24+
}

0 commit comments

Comments
 (0)