feat:添加响应结构体方便前端渲染
This commit is contained in:
29
internal/feed/entity.go
Normal file
29
internal/feed/entity.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package feed
|
||||
|
||||
type FeedAuthor struct {
|
||||
ID uint `json:"id"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
type FeedVideoItem struct {
|
||||
ID uint `json:"id"`
|
||||
Author FeedAuthor `json:"author"`
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description,omitempty"`
|
||||
PlayURL string `json:"play_url"`
|
||||
CoverURL string `json:"cover_url"`
|
||||
CreateTime int64 `json:"create_time"`
|
||||
LikesCount int64 `json:"likes_count"`
|
||||
IsLiked bool `json:"is_liked"`
|
||||
}
|
||||
|
||||
type ListLatestRequest struct {
|
||||
Limit int `json:"limit"`
|
||||
LatestTime int64 `json:"latest_time"`
|
||||
}
|
||||
|
||||
type ListLatestResponse struct {
|
||||
VideoList []FeedVideoItem `json:"video_list"`
|
||||
NextTime int64 `json:"next_time"`
|
||||
HasMore bool `json:"has_more"`
|
||||
}
|
||||
@@ -6,11 +6,6 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ListLatestRequest struct {
|
||||
Limit int `json:"limit"`
|
||||
LatestTime int64 `json:"latest_time"`
|
||||
}
|
||||
|
||||
type FeedHandler struct {
|
||||
service *FeedService
|
||||
}
|
||||
@@ -32,10 +27,10 @@ func (f *FeedHandler) ListLatest(c *gin.Context) {
|
||||
if req.LatestTime > 0 {
|
||||
latestTime = time.Unix(req.LatestTime, 0)
|
||||
}
|
||||
feeds, err := f.service.ListLatest(c.Request.Context(), req.Limit, latestTime)
|
||||
feedItems, err := f.service.ListLatest(c.Request.Context(), req.Limit, latestTime)
|
||||
if err != nil {
|
||||
c.JSON(500, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(200, feeds)
|
||||
c.JSON(200, feedItems)
|
||||
}
|
||||
|
||||
@@ -7,23 +7,18 @@ import (
|
||||
)
|
||||
|
||||
type FeedService struct {
|
||||
repo *FeedRepository
|
||||
repo *FeedRepository
|
||||
likeRepo *video.LikeRepository
|
||||
}
|
||||
|
||||
type FeedResponse struct {
|
||||
VideoList []video.Video `json:"video_list"`
|
||||
NextTime time.Time `json:"next_time"`
|
||||
HasMore bool `json:"has_more"`
|
||||
func NewFeedService(repo *FeedRepository, likeRepo *video.LikeRepository) *FeedService {
|
||||
return &FeedService{repo: repo, likeRepo: likeRepo}
|
||||
}
|
||||
|
||||
func NewFeedService(repo *FeedRepository) *FeedService {
|
||||
return &FeedService{repo: repo}
|
||||
}
|
||||
|
||||
func (f *FeedService) ListLatest(ctx context.Context, limit int, latestBefore time.Time) (FeedResponse, error) {
|
||||
func (f *FeedService) ListLatest(ctx context.Context, limit int, latestBefore time.Time) (ListLatestResponse, error) {
|
||||
videos, err := f.repo.ListLatest(ctx, limit, latestBefore)
|
||||
if err != nil {
|
||||
return FeedResponse{}, err
|
||||
return ListLatestResponse{}, err
|
||||
}
|
||||
var nextTime time.Time
|
||||
if len(videos) > 0 {
|
||||
@@ -32,9 +27,27 @@ func (f *FeedService) ListLatest(ctx context.Context, limit int, latestBefore ti
|
||||
nextTime = time.Time{}
|
||||
}
|
||||
hasMore := len(videos) == limit
|
||||
resp := FeedResponse{
|
||||
VideoList: videos,
|
||||
NextTime: nextTime,
|
||||
feedVideos := make([]FeedVideoItem, 0, len(videos))
|
||||
for _, video := range videos {
|
||||
isLiked, err := f.likeRepo.IsLiked(ctx, video.ID, video.AuthorID)
|
||||
if err != nil {
|
||||
return ListLatestResponse{}, err
|
||||
}
|
||||
feedVideos = append(feedVideos, FeedVideoItem{
|
||||
ID: video.ID,
|
||||
Author: FeedAuthor{ID: video.AuthorID, Username: video.Username},
|
||||
Title: video.Title,
|
||||
Description: video.Description,
|
||||
PlayURL: video.PlayURL,
|
||||
CoverURL: video.CoverURL,
|
||||
CreateTime: video.CreateTime.Unix(),
|
||||
LikesCount: video.LikesCount,
|
||||
IsLiked: isLiked,
|
||||
})
|
||||
}
|
||||
resp := ListLatestResponse{
|
||||
VideoList: feedVideos,
|
||||
NextTime: nextTime.Unix(),
|
||||
HasMore: hasMore,
|
||||
}
|
||||
return resp, nil
|
||||
|
||||
Reference in New Issue
Block a user