114 lines
2.8 KiB
Go
114 lines
2.8 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"net/netip"
|
|
db "server/internal/db/sqlc"
|
|
"server/internal/model/common"
|
|
"server/internal/model/response"
|
|
"server/internal/pkg/dberr"
|
|
"server/internal/pkg/errs"
|
|
"server/internal/pkg/httputil"
|
|
)
|
|
|
|
type PostService struct {
|
|
queries *db.Queries
|
|
}
|
|
|
|
func NewPostService(queries *db.Queries) *PostService {
|
|
return &PostService{
|
|
queries: queries,
|
|
}
|
|
}
|
|
|
|
func (s *PostService) ListPage(ctx context.Context, p *common.Pagination) ([]db.ListPublishedPostsRow, int64, error) {
|
|
params := db.ListPublishedPostsParams{
|
|
Limit: p.PageSize,
|
|
Offset: (p.Page - 1) * p.PageSize,
|
|
}
|
|
|
|
total, err := s.queries.CountPublishedPosts(ctx)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
list, err := s.queries.ListPublishedPosts(ctx, params)
|
|
if err != nil {
|
|
return nil, 0, err
|
|
}
|
|
|
|
for i := range list {
|
|
url := httputil.BuildFileUrl(list[i].Cover)
|
|
list[i].Cover = &url
|
|
}
|
|
|
|
return list, total, nil
|
|
}
|
|
|
|
func (s *PostService) GetPost(ctx context.Context, slug string, ip netip.Addr) (*db.GetPublicPostBySlugRow, error) {
|
|
post, err := s.queries.GetPublicPostBySlug(ctx, slug)
|
|
if err != nil {
|
|
return nil, dberr.MapNoRows(err, errs.ErrPostNotFound)
|
|
}
|
|
|
|
_ = s.queries.IncrementPostStatsView(ctx, db.IncrementPostStatsViewParams{
|
|
PostID: post.ID,
|
|
Ip: ip,
|
|
})
|
|
|
|
return &post, nil
|
|
}
|
|
|
|
func (s *PostService) ListCategoryStats(ctx context.Context) ([]db.ListCategoryStatsRow, error) {
|
|
return s.queries.ListCategoryStats(ctx)
|
|
}
|
|
|
|
func (s *PostService) ListArchives(ctx context.Context) ([]response.ArchiveYear, error) {
|
|
list, err := s.queries.ListArchives(ctx)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
archive := make([]response.ArchiveYear, 0)
|
|
|
|
for _, item := range list {
|
|
y := item.PublishedAt.Year()
|
|
m := int(item.PublishedAt.Month())
|
|
|
|
if len(archive) == 0 || archive[len(archive)-1].Year != y {
|
|
archive = append(archive, response.ArchiveYear{
|
|
Year: y,
|
|
Total: 0,
|
|
ArchiveMonth: make([]response.ArchiveMonth, 0),
|
|
})
|
|
}
|
|
|
|
// 获取索引
|
|
lastYearIndex := len(archive) - 1
|
|
|
|
months := archive[lastYearIndex].ArchiveMonth
|
|
if len(months) == 0 || months[len(months)-1].Month != m {
|
|
archive[lastYearIndex].ArchiveMonth = append(archive[lastYearIndex].ArchiveMonth, response.ArchiveMonth{
|
|
Month: m,
|
|
Archive: make([]response.ArchivePost, 0),
|
|
})
|
|
}
|
|
|
|
lastMonthIndex := len(archive[lastYearIndex].ArchiveMonth) - 1
|
|
|
|
archive[lastYearIndex].ArchiveMonth[lastMonthIndex].Archive =
|
|
append(archive[lastYearIndex].ArchiveMonth[lastMonthIndex].Archive, response.ArchivePost{
|
|
ID: item.ID,
|
|
Slug: item.Slug,
|
|
Title: item.Title,
|
|
PublishedAt: item.PublishedAt,
|
|
PublishedAtDisplay: item.PublishedAt.Format("01-02"),
|
|
CategoryName: *item.CategoryName,
|
|
})
|
|
|
|
archive[lastYearIndex].Total++
|
|
}
|
|
|
|
return archive, nil
|
|
}
|