mirror of https://gitee.com/answerdev/answer.git
add dashboard service
This commit is contained in:
parent
27e3e2ec8c
commit
872689b93a
|
@ -44,6 +44,7 @@ import (
|
||||||
auth2 "github.com/answerdev/answer/internal/service/auth"
|
auth2 "github.com/answerdev/answer/internal/service/auth"
|
||||||
"github.com/answerdev/answer/internal/service/collection_common"
|
"github.com/answerdev/answer/internal/service/collection_common"
|
||||||
comment2 "github.com/answerdev/answer/internal/service/comment"
|
comment2 "github.com/answerdev/answer/internal/service/comment"
|
||||||
|
"github.com/answerdev/answer/internal/service/dashboard"
|
||||||
export2 "github.com/answerdev/answer/internal/service/export"
|
export2 "github.com/answerdev/answer/internal/service/export"
|
||||||
"github.com/answerdev/answer/internal/service/follow"
|
"github.com/answerdev/answer/internal/service/follow"
|
||||||
meta2 "github.com/answerdev/answer/internal/service/meta"
|
meta2 "github.com/answerdev/answer/internal/service/meta"
|
||||||
|
@ -148,7 +149,8 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
|
||||||
questionService := service.NewQuestionService(questionRepo, tagCommonService, questionCommon, userCommon, revisionService, metaService, collectionCommon, answerActivityService)
|
questionService := service.NewQuestionService(questionRepo, tagCommonService, questionCommon, userCommon, revisionService, metaService, collectionCommon, answerActivityService)
|
||||||
questionController := controller.NewQuestionController(questionService, rankService)
|
questionController := controller.NewQuestionController(questionService, rankService)
|
||||||
answerService := service.NewAnswerService(answerRepo, questionRepo, questionCommon, userCommon, collectionCommon, userRepo, revisionService, answerActivityService, answerCommon, voteRepo)
|
answerService := service.NewAnswerService(answerRepo, questionRepo, questionCommon, userCommon, collectionCommon, userRepo, revisionService, answerActivityService, answerCommon, voteRepo)
|
||||||
answerController := controller.NewAnswerController(answerService, rankService)
|
dashboardService := dashboard.NewDashboardService(questionRepo, answerRepo, commentCommonRepo, voteRepo, userRepo, reportRepo, configRepo)
|
||||||
|
answerController := controller.NewAnswerController(answerService, rankService, dashboardService)
|
||||||
searchRepo := search_common.NewSearchRepo(dataData, uniqueIDRepo, userCommon)
|
searchRepo := search_common.NewSearchRepo(dataData, uniqueIDRepo, userCommon)
|
||||||
searchService := service.NewSearchService(searchRepo, tagRepo, userCommon, followRepo)
|
searchService := service.NewSearchService(searchRepo, tagRepo, userCommon, followRepo)
|
||||||
searchController := controller.NewSearchController(searchService)
|
searchController := controller.NewSearchController(searchService)
|
||||||
|
|
|
@ -9,20 +9,30 @@ import (
|
||||||
"github.com/answerdev/answer/internal/entity"
|
"github.com/answerdev/answer/internal/entity"
|
||||||
"github.com/answerdev/answer/internal/schema"
|
"github.com/answerdev/answer/internal/schema"
|
||||||
"github.com/answerdev/answer/internal/service"
|
"github.com/answerdev/answer/internal/service"
|
||||||
|
"github.com/answerdev/answer/internal/service/dashboard"
|
||||||
"github.com/answerdev/answer/internal/service/rank"
|
"github.com/answerdev/answer/internal/service/rank"
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/segmentfault/pacman/errors"
|
"github.com/segmentfault/pacman/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// AnswerController answer controller
|
// AnswerController answer controller
|
||||||
type AnswerController struct {
|
type AnswerController struct {
|
||||||
answerService *service.AnswerService
|
answerService *service.AnswerService
|
||||||
rankService *rank.RankService
|
rankService *rank.RankService
|
||||||
|
dashboardService *dashboard.DashboardService
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAnswerController new controller
|
// NewAnswerController new controller
|
||||||
func NewAnswerController(answerService *service.AnswerService, rankService *rank.RankService) *AnswerController {
|
func NewAnswerController(answerService *service.AnswerService,
|
||||||
return &AnswerController{answerService: answerService, rankService: rankService}
|
rankService *rank.RankService,
|
||||||
|
dashboardService *dashboard.DashboardService,
|
||||||
|
) *AnswerController {
|
||||||
|
return &AnswerController{
|
||||||
|
answerService: answerService,
|
||||||
|
rankService: rankService,
|
||||||
|
dashboardService: dashboardService,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// RemoveAnswer delete answer
|
// RemoveAnswer delete answer
|
||||||
|
@ -236,3 +246,12 @@ func (ac *AnswerController) AdminSetAnswerStatus(ctx *gin.Context) {
|
||||||
err := ac.answerService.AdminSetAnswerStatus(ctx, req.AnswerID, req.StatusStr)
|
err := ac.answerService.AdminSetAnswerStatus(ctx, req.AnswerID, req.StatusStr)
|
||||||
handler.HandleResponse(ctx, err, gin.H{})
|
handler.HandleResponse(ctx, err, gin.H{})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// dashboardService
|
||||||
|
func (ac *AnswerController) Dashboard(ctx *gin.Context) {
|
||||||
|
err := ac.dashboardService.Statistical(ctx)
|
||||||
|
spew.Dump(err)
|
||||||
|
handler.HandleResponse(ctx, err, gin.H{
|
||||||
|
"ping": "pong",
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -4,8 +4,10 @@ import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/answerdev/answer/internal/base/data"
|
"github.com/answerdev/answer/internal/base/data"
|
||||||
|
"github.com/answerdev/answer/internal/base/reason"
|
||||||
"github.com/answerdev/answer/internal/entity"
|
"github.com/answerdev/answer/internal/entity"
|
||||||
"github.com/answerdev/answer/internal/service/activity_common"
|
"github.com/answerdev/answer/internal/service/activity_common"
|
||||||
|
"github.com/segmentfault/pacman/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
// VoteRepo activity repository
|
// VoteRepo activity repository
|
||||||
|
@ -39,3 +41,12 @@ func (vr *VoteRepo) GetVoteStatus(ctx context.Context, objectID, userID string)
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (vr *VoteRepo) GetVoteCount(ctx context.Context, activityTypes []int) (count int64, err error) {
|
||||||
|
list := make([]*entity.Activity, 0)
|
||||||
|
count, err = vr.data.DB.Where("cancelled =0").In("activity_type", activityTypes).FindAndCount(&list)
|
||||||
|
if err != nil {
|
||||||
|
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
|
|
||||||
"github.com/answerdev/answer/internal/base/constant"
|
"github.com/answerdev/answer/internal/base/constant"
|
||||||
|
@ -102,6 +103,16 @@ func (ar *answerRepo) GetAnswer(ctx context.Context, id string) (
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetQuestionCount
|
||||||
|
func (ar *answerRepo) GetAnswerCount(ctx context.Context) (count int64, err error) {
|
||||||
|
list := make([]*entity.Answer, 0)
|
||||||
|
count, err = ar.data.DB.Where("status = ?", entity.AnswerStatusAvailable).FindAndCount(&list)
|
||||||
|
if err != nil {
|
||||||
|
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// GetAnswerList get answer list all
|
// GetAnswerList get answer list all
|
||||||
func (ar *answerRepo) GetAnswerList(ctx context.Context, answer *entity.Answer) (answerList []*entity.Answer, err error) {
|
func (ar *answerRepo) GetAnswerList(ctx context.Context, answer *entity.Answer) (answerList []*entity.Answer, err error) {
|
||||||
answerList = make([]*entity.Answer, 0)
|
answerList = make([]*entity.Answer, 0)
|
||||||
|
|
|
@ -79,6 +79,15 @@ func (cr *commentRepo) GetComment(ctx context.Context, commentID string) (
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cr *commentRepo) GetCommentCount(ctx context.Context) (count int64, err error) {
|
||||||
|
list := make([]*entity.Comment, 0)
|
||||||
|
count, err = cr.data.DB.Where("status = ?", entity.CommentStatusAvailable).FindAndCount(&list)
|
||||||
|
if err != nil {
|
||||||
|
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// GetCommentPage get comment page
|
// GetCommentPage get comment page
|
||||||
func (cr *commentRepo) GetCommentPage(ctx context.Context, commentQuery *comment.CommentQuery) (
|
func (cr *commentRepo) GetCommentPage(ctx context.Context, commentQuery *comment.CommentQuery) (
|
||||||
commentList []*entity.Comment, total int64, err error,
|
commentList []*entity.Comment, total int64, err error,
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
|
|
||||||
"github.com/answerdev/answer/internal/base/constant"
|
"github.com/answerdev/answer/internal/base/constant"
|
||||||
|
@ -162,6 +163,16 @@ func (qr *questionRepo) GetQuestionList(ctx context.Context, question *entity.Qu
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, err error) {
|
||||||
|
questionList := make([]*entity.Question, 0)
|
||||||
|
|
||||||
|
count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusclosed}).FindAndCount(&questionList)
|
||||||
|
if err != nil {
|
||||||
|
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// GetQuestionPage get question page
|
// GetQuestionPage get question page
|
||||||
func (qr *questionRepo) GetQuestionPage(ctx context.Context, page, pageSize int, question *entity.Question) (questionList []*entity.Question, total int64, err error) {
|
func (qr *questionRepo) GetQuestionPage(ctx context.Context, page, pageSize int, question *entity.Question) (questionList []*entity.Question, total int64, err error) {
|
||||||
questionList = make([]*entity.Question, 0)
|
questionList = make([]*entity.Question, 0)
|
||||||
|
|
|
@ -86,6 +86,9 @@ func (a *AnswerAPIRouter) RegisterUnAuthAnswerAPIRouter(r *gin.RouterGroup) {
|
||||||
r.GET("/personal/comment/page", a.commentController.GetCommentPersonalWithPage)
|
r.GET("/personal/comment/page", a.commentController.GetCommentPersonalWithPage)
|
||||||
r.GET("/comment", a.commentController.GetComment)
|
r.GET("/comment", a.commentController.GetComment)
|
||||||
|
|
||||||
|
//test
|
||||||
|
r.GET("/test", a.answerController.Dashboard)
|
||||||
|
|
||||||
// user
|
// user
|
||||||
r.GET("/user/info", a.userController.GetUserInfoByUserID)
|
r.GET("/user/info", a.userController.GetUserInfoByUserID)
|
||||||
r.GET("/user/status", a.userController.GetUserStatus)
|
r.GET("/user/status", a.userController.GetUserStatus)
|
||||||
|
|
|
@ -7,4 +7,5 @@ import (
|
||||||
// VoteRepo activity repository
|
// VoteRepo activity repository
|
||||||
type VoteRepo interface {
|
type VoteRepo interface {
|
||||||
GetVoteStatus(ctx context.Context, objectId, userId string) (status string)
|
GetVoteStatus(ctx context.Context, objectId, userId string) (status string)
|
||||||
|
GetVoteCount(ctx context.Context, activityTypes []int) (count int64, err error)
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ type AnswerRepo interface {
|
||||||
SearchList(ctx context.Context, search *entity.AnswerSearch) ([]*entity.Answer, int64, error)
|
SearchList(ctx context.Context, search *entity.AnswerSearch) ([]*entity.Answer, int64, error)
|
||||||
CmsSearchList(ctx context.Context, search *entity.CmsAnswerSearch) ([]*entity.Answer, int64, error)
|
CmsSearchList(ctx context.Context, search *entity.CmsAnswerSearch) ([]*entity.Answer, int64, error)
|
||||||
UpdateAnswerStatus(ctx context.Context, answer *entity.Answer) (err error)
|
UpdateAnswerStatus(ctx context.Context, answer *entity.Answer) (err error)
|
||||||
|
GetAnswerCount(ctx context.Context) (count int64, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AnswerCommon user service
|
// AnswerCommon user service
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
// CommentCommonRepo comment repository
|
// CommentCommonRepo comment repository
|
||||||
type CommentCommonRepo interface {
|
type CommentCommonRepo interface {
|
||||||
GetComment(ctx context.Context, commentID string) (comment *entity.Comment, exist bool, err error)
|
GetComment(ctx context.Context, commentID string) (comment *entity.Comment, exist bool, err error)
|
||||||
|
GetCommentCount(ctx context.Context) (count int64, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CommentCommonService user service
|
// CommentCommonService user service
|
||||||
|
|
|
@ -0,0 +1,85 @@
|
||||||
|
package dashboard
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/answerdev/answer/internal/service/activity_common"
|
||||||
|
answercommon "github.com/answerdev/answer/internal/service/answer_common"
|
||||||
|
"github.com/answerdev/answer/internal/service/comment_common"
|
||||||
|
"github.com/answerdev/answer/internal/service/config"
|
||||||
|
questioncommon "github.com/answerdev/answer/internal/service/question_common"
|
||||||
|
"github.com/answerdev/answer/internal/service/report_common"
|
||||||
|
usercommon "github.com/answerdev/answer/internal/service/user_common"
|
||||||
|
"github.com/davecgh/go-spew/spew"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DashboardService struct {
|
||||||
|
questionRepo questioncommon.QuestionRepo
|
||||||
|
answerRepo answercommon.AnswerRepo
|
||||||
|
commentRepo comment_common.CommentCommonRepo
|
||||||
|
voteRepo activity_common.VoteRepo
|
||||||
|
userRepo usercommon.UserRepo
|
||||||
|
reportRepo report_common.ReportRepo
|
||||||
|
configRepo config.ConfigRepo
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewDashboardService(
|
||||||
|
questionRepo questioncommon.QuestionRepo,
|
||||||
|
answerRepo answercommon.AnswerRepo,
|
||||||
|
commentRepo comment_common.CommentCommonRepo,
|
||||||
|
voteRepo activity_common.VoteRepo,
|
||||||
|
userRepo usercommon.UserRepo,
|
||||||
|
reportRepo report_common.ReportRepo,
|
||||||
|
configRepo config.ConfigRepo,
|
||||||
|
|
||||||
|
) *DashboardService {
|
||||||
|
return &DashboardService{
|
||||||
|
questionRepo: questionRepo,
|
||||||
|
answerRepo: answerRepo,
|
||||||
|
commentRepo: commentRepo,
|
||||||
|
voteRepo: voteRepo,
|
||||||
|
userRepo: userRepo,
|
||||||
|
reportRepo: reportRepo,
|
||||||
|
configRepo: configRepo,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Statistical
|
||||||
|
func (ds *DashboardService) Statistical(ctx context.Context) error {
|
||||||
|
questionCount, err := ds.questionRepo.GetQuestionCount(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
answerCount, err := ds.answerRepo.GetAnswerCount(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
commentCount, err := ds.commentRepo.GetCommentCount(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
typeKeys := []string{
|
||||||
|
"question.vote_up",
|
||||||
|
"question.vote_down",
|
||||||
|
"answer.vote_up",
|
||||||
|
"answer.vote_down",
|
||||||
|
}
|
||||||
|
var activityTypes []int
|
||||||
|
|
||||||
|
for _, typeKey := range typeKeys {
|
||||||
|
var t int
|
||||||
|
t, err = ds.configRepo.GetConfigType(typeKey)
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
activityTypes = append(activityTypes, t)
|
||||||
|
}
|
||||||
|
|
||||||
|
voteCount, err := ds.voteRepo.GetVoteCount(ctx, activityTypes)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
spew.Dump(questionCount, answerCount, commentCount, activityTypes, voteCount)
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
package dashboard
|
|
@ -8,6 +8,7 @@ import (
|
||||||
collectioncommon "github.com/answerdev/answer/internal/service/collection_common"
|
collectioncommon "github.com/answerdev/answer/internal/service/collection_common"
|
||||||
"github.com/answerdev/answer/internal/service/comment"
|
"github.com/answerdev/answer/internal/service/comment"
|
||||||
"github.com/answerdev/answer/internal/service/comment_common"
|
"github.com/answerdev/answer/internal/service/comment_common"
|
||||||
|
"github.com/answerdev/answer/internal/service/dashboard"
|
||||||
"github.com/answerdev/answer/internal/service/export"
|
"github.com/answerdev/answer/internal/service/export"
|
||||||
"github.com/answerdev/answer/internal/service/follow"
|
"github.com/answerdev/answer/internal/service/follow"
|
||||||
"github.com/answerdev/answer/internal/service/meta"
|
"github.com/answerdev/answer/internal/service/meta"
|
||||||
|
@ -65,4 +66,5 @@ var ProviderSetService = wire.NewSet(
|
||||||
notficationcommon.NewNotificationCommon,
|
notficationcommon.NewNotificationCommon,
|
||||||
notification.NewNotificationService,
|
notification.NewNotificationService,
|
||||||
activity.NewAnswerActivityService,
|
activity.NewAnswerActivityService,
|
||||||
|
dashboard.NewDashboardService,
|
||||||
)
|
)
|
||||||
|
|
|
@ -38,6 +38,7 @@ type QuestionRepo interface {
|
||||||
UpdateLastAnswer(ctx context.Context, question *entity.Question) (err error)
|
UpdateLastAnswer(ctx context.Context, question *entity.Question) (err error)
|
||||||
FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error)
|
FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error)
|
||||||
CmsSearchList(ctx context.Context, search *schema.CmsQuestionSearch) ([]*entity.Question, int64, error)
|
CmsSearchList(ctx context.Context, search *schema.CmsQuestionSearch) ([]*entity.Question, int64, error)
|
||||||
|
GetQuestionCount(ctx context.Context) (count int64, err error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// QuestionCommon user service
|
// QuestionCommon user service
|
||||||
|
|
Loading…
Reference in New Issue