8000 feat: add likes filed to comment by raisons · Pull Request #358 · go-sonic/sonic · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add likes filed to comment #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dal/comment.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions handler/content/api/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package api

import (
"github.com/gin-gonic/gin"

"github.com/go-sonic/sonic/service"
"github.com/go-sonic/sonic/util"
)

type CommentHandler struct {
BaseCommentService service.BaseCommentService
}

func NewCommentHandler(baseCommentService service.BaseCommentService) *CommentHandler {
return &CommentHandler{
BaseCommentService: baseCommentService,
}
}

func (c *CommentHandler) Like(ctx *gin.Context) (interface{}, error) {
commentID, err := util.ParamInt32(ctx, "commentID")
if err != nil {
return nil, err
}
return nil, c.BaseCommentService.IncreaseLike(ctx, commentID)
}
1 change: 1 addition & 0 deletions handler/content/api/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ func init() {
NewSheetHandler,
NewOptionHandler,
NewPhotoHandler,
NewCommentHandler,
)
}
2 changes: 2 additions & 0 deletions handler/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ func (s *Server) RegisterRouters() {
contentAPIRouter.GET("/links/team_view", s.wrapHandler(s.ContentAPILinkHandler.LinkTeamVO))

contentAPIRouter.GET("/options/comment", s.wrapHandler(s.ContentAPIOptionHandler.Comment))

contentAPIRouter.POST("/comments/:commentID/likes", s.wrapHandler(s.ContentAPICommentHandler.Like))
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions handler/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ type Server struct {
ContentAPISheetHandler *api.SheetHandler
ContentAPIOptionHandler *api.OptionHandler
ContentAPIPhotoHandler *api.PhotoHandler
ContentAPICommentHandler *api.CommentHandler
}

type ServerParams struct {
Expand Down Expand Up @@ -132,6 +133,7 @@ type ServerParams struct {
ContentAPISheetHandler *api.SheetHandler
ContentAPIOptionHandler *api.OptionHandler
ContentAPIPhotoHandler *api.PhotoHandler
ContentAPICommentHandler *api.CommentHandler
}

func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server {
Expand Down Expand Up @@ -197,6 +199,7 @@ func NewServer(param ServerParams, lifecycle fx.Lifecycle) *Server {
ContentAPIOptionHandler: param.ContentAPIOptionHandler,
ContentSearchHandler: param.ContentSearchHandler,
ContentAPIPhotoHandler: param.ContentAPIPhotoHandler,
ContentAPICommentHandler: param.ContentAPICommentHandler,
}
lifecycle.Append(fx.Hook{
OnStop: httpServer.Shutdown,
Expand Down
1 change: 1 addition & 0 deletions model/dto/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ type Comment struct {
AllowNotification bool `json:"allowNotification"`
CreateTime int64 `json:"createTime"`
Avatar string `json:"avatar"`
Likes int32 `json:"likes"`
}
1 change: 1 addition & 0 deletions model/entity/comment.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions model/property/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import "reflect"
var (
CommentGravatarDefault = Property{
KeyValue: "comment_gravatar_default",
DefaultValue: "mm",
DefaultValue: "identicon",
Kind: reflect.String,
}
CommentNewNeedCheck = Property{
Expand Down Expand Up @@ -40,12 +40,12 @@ var (
}
CommentInternalPluginJs = Property{
KeyValue: "comment_internal_plugin_js",
DefaultValue: "//cdn.jsdelivr.net/npm/halo-comment@latest/dist/halo-comment.min.js",
DefaultValue: "https://cdn.jsdelivr.net/npm/halo-comment@latest/dist/halo-comment.min.js",
Kind: reflect.String,
}
CommentGravatarSource = Property{
KeyValue: "gravatar_source",
DefaultValue: "//gravatar.com/avatar/",
DefaultValue: "https://gravatar.com/avatar/",
Kind: reflect.String,
}
CommentBanTime = Property{
Expand Down
3 changes: 2 additions & 1 deletion model/vo/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ type JournalCommentWithJournal struct {

type CommentWithHasChildren struct {
*dto.Comment
HasChildren bool `json:"hasChildren"`
HasChildren bool `json:"hasChildren"`
ChildrenCount int64 `json:"childrenCount"`
}
15 changes: 15 additions & 0 deletions resources/template/common/macro/global_macro.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,19 @@
<script src="{{or .options.comment_internal_plugin_js "https://cdn.jsdelivr.net/npm/halo-comment@latest/dist/halo-comment.min.js"}}"></script>
<halo-comment id="{{.target.ID}}" type="{{.type}}"/>
{{end}}
{{end}}

{{define "global.sonic_comment"}}
{{if or (eq .type "journal") (not .target.DisallowComment)}}
<div id="comment"></div>
<script src="https://cdn.staticfile.org/vue/3.3.4/vue.global.prod.js"></script>
<script src="{{.options.comment_internal_plugin_js}}"></script>
<script>
SonicComment.init("#comment", {
target: "{{.type}}s",
targetId: {{.target.ID}},
colorScheme: "light"
});
</script>
{{end}}
{{end}}
1 change: 1 addition & 0 deletions scripts/table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ create table if not exists comment
status int default 0 not null,
top_priority int default 0 not null,
user_agent varchar(511) default '' not null,
likes int default 0 not null ,
index comment_parent_id (parent_id),
index comment_post_id (post_id),
index comment_type_status (type, status)
Expand Down
4 changes: 4 additions & 0 deletions service/assembler/comment_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ func (b *baseCommentAssembler) ConvertToWithHasChildren(ctx context.Context, com
}
if count, ok := countMap[commentDTO.ID]; ok && count > 0 {
commentWithHasChildren.HasChildren = true
commentWithHasChildren.ChildrenCount = count
} else {
commentWithHasChildren.HasChildren = false
commentWithHasChildren.ChildrenCount = 0
}
result = append(result, commentWithHasChildren)
}
Expand All @@ -90,6 +92,7 @@ func (b *baseCommentAssembler) ConvertToDTO(ctx context.Context, comment *entity
IsAdmin: comment.IsAdmin,
AllowNotification: comment.AllowNotification,
CreateTime: comment.CreateTime.UnixMilli(),
Likes: comment.Likes,
}
avatarURL, err := b.BaseCommentService.BuildAvatarURL(ctx, comment.GravatarMd5, nil, nil)
if err != nil {
Expand Down Expand Up @@ -124,6 +127,7 @@ func (b *baseCommentAssembler) ConvertToDTOList(ctx context.Context, comments []
IsAdmin: comment.IsAdmin,
AllowNotification: comment.AllowNotification,
CreateTime: comment.CreateTime.UnixMilli(),
Likes: comment.Likes,
}
avatarURL, err := b.BaseCommentService.BuildAvatarURL(ctx, comment.GravatarMd5, util.StringPtr(gravatarSource.(string)), util.StringPtr(gravatarDefault.(string)))
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions service/comment_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ type BaseCommentService interface {
CountByStatusAndContentIDs(ctx context.Context, status consts.CommentStatus, contentIDs []int32) (map[int32]int64, error)
CountChildren(ctx context.Context, parentCommentIDs []int32) (map[int32]int64, error)
GetChildren(ctx context.Context, parentCommentID int32, contentID int32, commentType consts.CommentType) ([]*entity.Comment, error)
IncreaseLike(ctx context.Context, commentID int32) error
}
12 changes: 12 additions & 0 deletions service/impl/comment_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,15 @@ func (b *baseCommentServiceImpl) GetChildren(ctx context.Context, parentCommentI
}
return children, nil
}

func (b baseCommentServiceImpl) IncreaseLike(ctx context.Context, commentID int32) error {
commentDAL := dal.GetQueryByCtx(ctx).Comment
info, err := commentDAL.WithContext(ctx).Where(commentDAL.ID.Eq(commentID)).UpdateSimple(commentDAL.Likes.Add(1))
if err != nil {
return WrapDBErr(err)
}
if info.RowsAffected != 1 {
return xerr.NoType.New("increase comment like failed postID=%v", commentID).WithStatus(xerr.StatusBadRequest).WithMsg("failed to like comment")
}
return nil
}
0