feat(P2): 粉丝/关注数 + 用户主页统计 (getProfile/getCounts)
This commit is contained in:
@@ -65,3 +65,15 @@ type UpdateProfileRequest struct {
|
|||||||
type RefreshRequest struct {
|
type RefreshRequest struct {
|
||||||
RefreshToken string `json:"refresh_token"`
|
RefreshToken string `json:"refresh_token"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetProfileRequest struct {
|
||||||
|
AccountID uint `json:"account_id"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetProfileResponse struct {
|
||||||
|
Account FindByIDResponse `json:"account"`
|
||||||
|
VideoCount int64 `json:"video_count"`
|
||||||
|
TotalLikes int64 `json:"total_likes"`
|
||||||
|
FollowerCount int64 `json:"follower_count"`
|
||||||
|
VloggerCount int64 `json:"vlogger_count"`
|
||||||
|
}
|
||||||
|
|||||||
@@ -126,7 +126,35 @@ func SetRouter(db *gorm.DB, cache *rediscache.Client, rmq *rabbitmq.RabbitMQ) *g
|
|||||||
protectedSocialGroup.POST("/unfollow", socialLimiter, socialHandler.Unfollow)
|
protectedSocialGroup.POST("/unfollow", socialLimiter, socialHandler.Unfollow)
|
||||||
protectedSocialGroup.POST("/getAllFollowers", socialHandler.GetAllFollowers)
|
protectedSocialGroup.POST("/getAllFollowers", socialHandler.GetAllFollowers)
|
||||||
protectedSocialGroup.POST("/getAllVloggers", socialHandler.GetAllVloggers)
|
protectedSocialGroup.POST("/getAllVloggers", socialHandler.GetAllVloggers)
|
||||||
|
protectedSocialGroup.POST("/getCounts", socialHandler.GetCounts)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
accountGroup.POST("/getProfile", func(c *gin.Context) {
|
||||||
|
var req account.GetProfileRequest
|
||||||
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if req.AccountID == 0 {
|
||||||
|
c.JSON(400, gin.H{"error": "account_id is required"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
acc, err := accountService.FindByID(c.Request.Context(), req.AccountID)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(500, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
videoCount, _ := videoRepository.CountByAuthor(c.Request.Context(), req.AccountID)
|
||||||
|
totalLikes, _ := videoRepository.TotalLikesByAuthor(c.Request.Context(), req.AccountID)
|
||||||
|
followerCount, _ := socialRepository.CountFollowers(c.Request.Context(), req.AccountID)
|
||||||
|
vloggerCount, _ := socialRepository.CountVloggers(c.Request.Context(), req.AccountID)
|
||||||
|
|
||||||
|
c.JSON(200, account.GetProfileResponse{
|
||||||
|
Account: account.FindByIDResponse{ID: acc.ID, Username: acc.Username, AvatarURL: acc.AvatarURL, Bio: acc.Bio},
|
||||||
|
VideoCount: videoCount, TotalLikes: totalLikes,
|
||||||
|
FollowerCount: followerCount, VloggerCount: vloggerCount,
|
||||||
|
})
|
||||||
|
})
|
||||||
// feed
|
// feed
|
||||||
feedRepository := feed.NewFeedRepository(db)
|
feedRepository := feed.NewFeedRepository(db)
|
||||||
feedService := feed.NewFeedService(feedRepository, likeRepository, cache)
|
feedService := feed.NewFeedService(feedRepository, likeRepository, cache)
|
||||||
|
|||||||
@@ -21,13 +21,20 @@ type GetAllFollowersRequest struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type GetAllFollowersResponse struct {
|
type GetAllFollowersResponse struct {
|
||||||
Followers []*account.Account `json:"followers"`
|
Followers []*account.Account `json:"followers"`
|
||||||
|
FollowerCount int64 `json:"follower_count"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type GetAllVloggersResponse struct {
|
||||||
|
Vloggers []*account.Account `json:"vloggers"`
|
||||||
|
VloggerCount int64 `json:"vlogger_count"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type SocialCounts struct {
|
||||||
|
FollowerCount int64 `json:"follower_count"`
|
||||||
|
VloggerCount int64 `json:"vlogger_count"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetAllVloggersRequest struct {
|
type GetAllVloggersRequest struct {
|
||||||
FollowerID uint `json:"follower_id"`
|
FollowerID uint `json:"follower_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type GetAllVloggersResponse struct {
|
|
||||||
Vloggers []*account.Account `json:"vloggers"`
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -94,7 +94,8 @@ func (h *SocialHandler) GetAllFollowers(c *gin.Context) {
|
|||||||
if followers == nil {
|
if followers == nil {
|
||||||
followers = []*account.Account{}
|
followers = []*account.Account{}
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, GetAllFollowersResponse{Followers: followers})
|
followerCount, _ := h.service.CountFollowers(c.Request.Context(), vloggerID)
|
||||||
|
c.JSON(http.StatusOK, GetAllFollowersResponse{Followers: followers, FollowerCount: followerCount})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *SocialHandler) GetAllVloggers(c *gin.Context) {
|
func (h *SocialHandler) GetAllVloggers(c *gin.Context) {
|
||||||
@@ -122,5 +123,17 @@ func (h *SocialHandler) GetAllVloggers(c *gin.Context) {
|
|||||||
if vloggers == nil {
|
if vloggers == nil {
|
||||||
vloggers = []*account.Account{}
|
vloggers = []*account.Account{}
|
||||||
}
|
}
|
||||||
c.JSON(http.StatusOK, GetAllVloggersResponse{Vloggers: vloggers})
|
vloggerCount, _ := h.service.CountVloggers(c.Request.Context(), followerID)
|
||||||
|
c.JSON(http.StatusOK, GetAllVloggersResponse{Vloggers: vloggers, VloggerCount: vloggerCount})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *SocialHandler) GetCounts(c *gin.Context) {
|
||||||
|
accountID, err := jwt.GetAccountID(c)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
followerCount, _ := h.service.CountFollowers(c.Request.Context(), accountID)
|
||||||
|
vloggerCount, _ := h.service.CountVloggers(c.Request.Context(), accountID)
|
||||||
|
c.JSON(http.StatusOK, SocialCounts{FollowerCount: followerCount, VloggerCount: vloggerCount})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,3 +91,19 @@ func (r *SocialRepository) IsFollowed(ctx context.Context, social *Social) (bool
|
|||||||
}
|
}
|
||||||
return count > 0, nil
|
return count > 0, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *SocialRepository) CountFollowers(ctx context.Context, vloggerID uint) (int64, error) {
|
||||||
|
var count int64
|
||||||
|
if err := r.db.WithContext(ctx).Model(&Social{}).Where("vlogger_id = ?", vloggerID).Count(&count).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *SocialRepository) CountVloggers(ctx context.Context, followerID uint) (int64, error) {
|
||||||
|
var count int64
|
||||||
|
if err := r.db.WithContext(ctx).Model(&Social{}).Where("follower_id = ?", followerID).Count(&count).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -80,6 +80,14 @@ func (s *SocialService) GetAllVloggers(ctx context.Context, FollowerID uint) ([]
|
|||||||
return s.repo.GetAllVloggers(ctx, FollowerID)
|
return s.repo.GetAllVloggers(ctx, FollowerID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *SocialService) CountFollowers(ctx context.Context, vloggerID uint) (int64, error) {
|
||||||
|
return s.repo.CountFollowers(ctx, vloggerID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SocialService) CountVloggers(ctx context.Context, followerID uint) (int64, error) {
|
||||||
|
return s.repo.CountVloggers(ctx, followerID)
|
||||||
|
}
|
||||||
|
|
||||||
func (s *SocialService) IsFollowed(ctx context.Context, social *Social) (bool, error) {
|
func (s *SocialService) IsFollowed(ctx context.Context, social *Social) (bool, error) {
|
||||||
_, err := s.accountrepo.FindByID(ctx, social.FollowerID)
|
_, err := s.accountrepo.FindByID(ctx, social.FollowerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -102,3 +102,19 @@ func (vr *VideoRepository) ChangePopularity(ctx context.Context, id uint, change
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (vr *VideoRepository) CountByAuthor(ctx context.Context, authorID uint) (int64, error) {
|
||||||
|
var count int64
|
||||||
|
if err := vr.db.WithContext(ctx).Model(&Video{}).Where("author_id = ?", authorID).Count(&count).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (vr *VideoRepository) TotalLikesByAuthor(ctx context.Context, authorID uint) (int64, error) {
|
||||||
|
var total int64
|
||||||
|
if err := vr.db.WithContext(ctx).Model(&Video{}).Where("author_id = ?", authorID).Select("COALESCE(SUM(likes_count), 0)").Scan(&total).Error; err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return total, nil
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user