feat: 统一sql命名、函数命名
This commit is contained in:
11
internal/service/web/module.go
Normal file
11
internal/service/web/module.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"go.uber.org/fx"
|
||||
)
|
||||
|
||||
var Module = fx.Module("web-services",
|
||||
fx.Provide(
|
||||
NewPostService,
|
||||
),
|
||||
)
|
||||
115
internal/service/web/post.go
Normal file
115
internal/service/web/post.go
Normal file
@@ -0,0 +1,115 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/netip"
|
||||
"server/internal/db"
|
||||
"server/internal/db/sqlc"
|
||||
"server/internal/model/common"
|
||||
"server/internal/model/response"
|
||||
"server/internal/pkg/dberr"
|
||||
"server/internal/pkg/errs"
|
||||
)
|
||||
|
||||
type PostService struct {
|
||||
store *db.Store
|
||||
}
|
||||
|
||||
func NewPostService(store *db.Store) *PostService {
|
||||
return &PostService{
|
||||
store: store,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PostService) ListPage(ctx context.Context, p *common.Pagination) (*common.PageResult[sqlc.ListPublishedPostsRow], error) {
|
||||
params := sqlc.ListPublishedPostsParams{
|
||||
Limit: p.PageSize,
|
||||
Offset: (p.Page - 1) * p.PageSize,
|
||||
}
|
||||
|
||||
total, err := s.store.CountPublishedPosts(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
list, err := s.store.ListPublishedPosts(ctx, params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &common.PageResult[sqlc.ListPublishedPostsRow]{
|
||||
List: list,
|
||||
Total: total,
|
||||
}, nil
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
|
||||
return &post, nil
|
||||
}
|
||||
|
||||
func (s *PostService) ListCategoryStats(ctx context.Context) ([]sqlc.ListCategoryStatsRow, error) {
|
||||
return s.store.ListCategoryStats(ctx)
|
||||
}
|
||||
|
||||
func (s *PostService) ListArchives(ctx context.Context) ([]response.ArchiveYear, error) {
|
||||
list, err := s.store.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
|
||||
}
|
||||
|
||||
func (s *PostService) ListPostTags(ctx context.Context) ([]sqlc.Tag, error) {
|
||||
return s.store.ListAllTags(ctx)
|
||||
}
|
||||
Reference in New Issue
Block a user