feat: update template
This commit is contained in:
@@ -2,13 +2,16 @@ package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/netip"
|
||||
"server/internal/db"
|
||||
"server/internal/db/sqlc"
|
||||
"server/internal/model/common"
|
||||
"server/internal/model/request"
|
||||
"server/internal/model/response"
|
||||
"server/internal/pkg/dberr"
|
||||
"server/internal/pkg/errs"
|
||||
"server/internal/pkg/safego"
|
||||
)
|
||||
|
||||
type PostService struct {
|
||||
@@ -21,7 +24,7 @@ func NewPostService(store *db.Store) *PostService {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PostService) ListPage(ctx context.Context, p *common.Pagination) (*common.PageResult[sqlc.ListPublishedPostsRow], error) {
|
||||
func (s *PostService) List(ctx context.Context, p *common.Pagination) (*common.PageResult[sqlc.ListPublishedPostsRow], error) {
|
||||
params := sqlc.ListPublishedPostsParams{
|
||||
Limit: p.PageSize,
|
||||
Offset: (p.Page - 1) * p.PageSize,
|
||||
@@ -43,15 +46,31 @@ func (s *PostService) ListPage(ctx context.Context, p *common.Pagination) (*comm
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *PostService) ListSearch(ctx context.Context, req request.SearchPublishedPostsParams) ([]sqlc.ListPublishedPostsWithFiltersRow, error) {
|
||||
params := sqlc.ListPublishedPostsWithFiltersParams{
|
||||
CategoryCode: &req.CategoryCode,
|
||||
TagCode: &req.TagCode,
|
||||
}
|
||||
|
||||
return s.store.ListPublishedPostsWithFilters(ctx, params)
|
||||
}
|
||||
|
||||
func (s *PostService) GetPost(ctx context.Context, slug string, ip netip.Addr) (*sqlc.GetPublicPostBySlugRow, error) {
|
||||
post, err := s.store.GetPublicPostBySlug(ctx, slug)
|
||||
if err != nil {
|
||||
return nil, dberr.MapNoRows(err, errs.ErrPostNotFound)
|
||||
}
|
||||
|
||||
_ = s.store.IncrementPostStatsView(ctx, sqlc.IncrementPostStatsViewParams{
|
||||
PostID: post.ID,
|
||||
Ip: ip,
|
||||
// 异步统计浏览量,不阻塞响应
|
||||
// 用 WithoutCancel 脱离请求 context:否则 handler 一返回、请求 ctx 被取消,写库会被中断
|
||||
safego.Go(func() {
|
||||
statsCtx := context.WithoutCancel(ctx)
|
||||
if err = s.store.IncrementPostStatsView(statsCtx, sqlc.IncrementPostStatsViewParams{
|
||||
PostID: post.ID,
|
||||
Ip: ip,
|
||||
}); err != nil {
|
||||
slog.Error("increment post stats view failed", "post_id", post.ID, "error", err)
|
||||
}
|
||||
})
|
||||
|
||||
return &post, nil
|
||||
@@ -94,6 +113,11 @@ func (s *PostService) ListArchives(ctx context.Context) ([]response.ArchiveYear,
|
||||
|
||||
lastMonthIndex := len(archive[lastYearIndex].ArchiveMonth) - 1
|
||||
|
||||
categoryName := ""
|
||||
if item.CategoryName != nil {
|
||||
categoryName = *item.CategoryName
|
||||
}
|
||||
|
||||
archive[lastYearIndex].ArchiveMonth[lastMonthIndex].Archive =
|
||||
append(archive[lastYearIndex].ArchiveMonth[lastMonthIndex].Archive, response.ArchivePost{
|
||||
ID: item.ID,
|
||||
@@ -101,7 +125,7 @@ func (s *PostService) ListArchives(ctx context.Context) ([]response.ArchiveYear,
|
||||
Title: item.Title,
|
||||
PublishedAt: item.PublishedAt,
|
||||
PublishedAtDisplay: item.PublishedAt.Format("01-02"),
|
||||
CategoryName: *item.CategoryName,
|
||||
CategoryName: categoryName,
|
||||
})
|
||||
|
||||
archive[lastYearIndex].Total++
|
||||
@@ -113,3 +137,7 @@ func (s *PostService) ListArchives(ctx context.Context) ([]response.ArchiveYear,
|
||||
func (s *PostService) ListPostTags(ctx context.Context) ([]sqlc.Tag, error) {
|
||||
return s.store.ListAllTags(ctx)
|
||||
}
|
||||
|
||||
func (s *PostService) GetPostsForSitemap(ctx context.Context) ([]sqlc.GetPostsForSitemapRow, error) {
|
||||
return s.store.GetPostsForSitemap(ctx)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user