feat: add likes filed to comment (#358)

* feat: add likes filed to comment

* fix: set likes field default value
pull/388/head
Raison 1 year ago committed by GitHub
parent 5089665048
commit 0960573d8a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -42,6 +42,7 @@ func newComment(db *gorm.DB, opts ...gen.DOOption) comment {
_comment.Status = field.NewField(tableName, "status")
_comment.TopPriority = field.NewInt32(tableName, "top_priority")
_comment.UserAgent = field.NewString(tableName, "user_agent")
_comment.Likes = field.NewInt32(tableName, "likes")
_comment.fillFieldMap()
@ -69,6 +70,7 @@ type comment struct {
Status field.Field
TopPriority field.Int32
UserAgent field.String
Likes field.Int32
fieldMap map[string]field.Expr
}
@ -102,6 +104,7 @@ func (c *comment) updateTableName(table string) *comment {
c.Status = field.NewField(table, "status")
c.TopPriority = field.NewInt32(table, "top_priority")
c.UserAgent = field.NewString(table, "user_agent")
c.Likes = field.NewInt32(table, "likes")
c.fillFieldMap()
@ -144,6 +147,7 @@ func (c *comment) fillFieldMap() {
c.fieldMap["status"] = c.Status
c.fieldMap["top_priority"] = c.TopPriority
c.fieldMap["user_agent"] = c.UserAgent
c.fieldMap["likes"] = c.Likes
}
func (c comment) clone(db *gorm.DB) comment {

@ -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)
}

@ -12,5 +12,6 @@ func init() {
NewSheetHandler,
NewOptionHandler,
NewPhotoHandler,
NewCommentHandler,
)
}

@ -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))
}
}
}

@ -77,6 +77,7 @@ type Server struct {
ContentAPISheetHandler *api.SheetHandler
ContentAPIOptionHandler *api.OptionHandler
ContentAPIPhotoHandler *api.PhotoHandler
ContentAPICommentHandler *api.CommentHandler
}
type ServerParams struct {
@ -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 {
@ -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,

@ -17,4 +17,5 @@ type Comment struct {
AllowNotification bool `json:"allowNotification"`
CreateTime int64 `json:"createTime"`
Avatar string `json:"avatar"`
Likes int32 `json:"likes"`
}

@ -31,6 +31,7 @@ type Comment struct {
Status consts.CommentStatus `gorm:"column:status;type:bigint;not null;index:comment_type_status,priority:2" json:"status"`
TopPriority int32 `gorm:"column:top_priority;type:int;not null" json:"top_priority"`
UserAgent string `gorm:"column:user_agent;type:varchar(511);not null" json:"user_agent"`
Likes int32 `gorm:"column:likes;type:int;not null;default: 0" json:"likes"`
}
// TableName Comment's table name

@ -5,7 +5,7 @@ import "reflect"
var (
CommentGravatarDefault = Property{
KeyValue: "comment_gravatar_default",
DefaultValue: "mm",
DefaultValue: "identicon",
Kind: reflect.String,
}
CommentNewNeedCheck = Property{
@ -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{

@ -29,4 +29,5 @@ type JournalCommentWithJournal struct {
type CommentWithHasChildren struct {
*dto.Comment
HasChildren bool `json:"hasChildren"`
ChildrenCount int64 `json:"childrenCount"`
}

@ -13,3 +13,18 @@
<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}}

@ -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)

@ -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)
}
@ -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 {
@ -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 {

@ -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
}

@ -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
}

Loading…
Cancel
Save