feat: release v1.0.0
This commit is contained in:
@@ -2,41 +2,37 @@ package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
db "server/internal/db/sqlc"
|
||||
"server/internal/db"
|
||||
"server/internal/db/sqlc"
|
||||
"server/internal/model/common"
|
||||
"server/internal/model/request"
|
||||
"server/internal/pkg/dberr"
|
||||
"server/internal/pkg/errs"
|
||||
"server/internal/pkg/httputil"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type SysPostService struct {
|
||||
queries *db.Queries
|
||||
pool *pgxpool.Pool
|
||||
store *db.Store
|
||||
}
|
||||
|
||||
func NewSysPostService(queries *db.Queries, pool *pgxpool.Pool) *SysPostService {
|
||||
func NewSysPostService(store *db.Store) *SysPostService {
|
||||
return &SysPostService{
|
||||
queries: queries,
|
||||
pool: pool,
|
||||
store: store,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SysPostService) ListPage(ctx context.Context, p *common.Pagination) ([]db.ListPostsRow, int64, error) {
|
||||
params := db.ListPostsParams{
|
||||
func (s *SysPostService) ListPage(ctx context.Context, p *common.Pagination) ([]sqlc.ListPostsRow, int64, error) {
|
||||
params := sqlc.ListPostsParams{
|
||||
Limit: p.PageSize,
|
||||
Offset: (p.Page - 1) * p.PageSize,
|
||||
}
|
||||
|
||||
total, err := s.queries.CountPosts(ctx)
|
||||
total, err := s.store.CountPosts(ctx)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
list, err := s.queries.ListPosts(ctx, params)
|
||||
list, err := s.store.ListPosts(ctx, params)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
@@ -49,8 +45,8 @@ func (s *SysPostService) ListPage(ctx context.Context, p *common.Pagination) ([]
|
||||
return list, total, nil
|
||||
}
|
||||
|
||||
func (s *SysPostService) FindByID(ctx context.Context, id int32) (*db.GetPostByIdRow, error) {
|
||||
post, err := s.queries.GetPostById(ctx, id)
|
||||
func (s *SysPostService) FindByID(ctx context.Context, id int32) (*sqlc.GetPostByIdRow, error) {
|
||||
post, err := s.store.GetPostById(ctx, id)
|
||||
if err != nil {
|
||||
return nil, dberr.MapNoRows(err, errs.ErrPostNotFound)
|
||||
}
|
||||
@@ -61,103 +57,117 @@ func (s *SysPostService) FindByID(ctx context.Context, id int32) (*db.GetPostByI
|
||||
}
|
||||
|
||||
func (s *SysPostService) Create(ctx context.Context, req request.CreatePostRequest) (int32, error) {
|
||||
// 开启事务
|
||||
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
postID, err := db.WithTxResult(ctx, s.store, func(q *sqlc.Queries) (int32, error) {
|
||||
params := sqlc.CreatePostParams{
|
||||
Title: req.Title,
|
||||
CoverID: &req.CoverID,
|
||||
Slug: req.Slug,
|
||||
Content: req.Content,
|
||||
Summary: req.Summary,
|
||||
Status: *req.Status,
|
||||
Sort: req.Sort,
|
||||
PublishedAt: req.PublishedAt,
|
||||
}
|
||||
|
||||
// 创建博客
|
||||
postID, err := q.CreatePost(ctx, params)
|
||||
if err != nil {
|
||||
return 0, dberr.MapUniqueViolation(err, dberr.PostSlugKey, errs.ErrSlugAlreadyExists)
|
||||
}
|
||||
|
||||
// 关联分类
|
||||
if err = q.CreatePostCategory(ctx, sqlc.CreatePostCategoryParams{
|
||||
PostID: postID,
|
||||
CategoryID: *req.CategoryID,
|
||||
}); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
var tagsParams []sqlc.CreatePostTagParams
|
||||
for _, tagID := range req.Tags {
|
||||
tagsParams = append(tagsParams, sqlc.CreatePostTagParams{
|
||||
PostID: postID,
|
||||
TagID: tagID,
|
||||
})
|
||||
}
|
||||
|
||||
if _, err = q.CreatePostTag(ctx, tagsParams); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return postID, nil
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = tx.Rollback(ctx)
|
||||
}()
|
||||
|
||||
q := db.New(tx)
|
||||
|
||||
params := db.CreatePostParams{
|
||||
Title: req.Title,
|
||||
CoverID: req.CoverID,
|
||||
Slug: req.Slug,
|
||||
Content: req.Content,
|
||||
Summary: req.Summary,
|
||||
Status: *req.Status,
|
||||
Sort: req.Sort,
|
||||
PublishedAt: req.PublishedAt,
|
||||
}
|
||||
|
||||
postId, err := q.CreatePost(ctx, params)
|
||||
if err != nil {
|
||||
return 0, dberr.MapUniqueViolation(err, dberr.PostSlugKey, errs.ErrSlugAlreadyExists)
|
||||
}
|
||||
|
||||
if err = q.CreatePostCategory(ctx, db.CreatePostCategoryParams{
|
||||
PostID: postId,
|
||||
CategoryID: *req.CategoryID,
|
||||
}); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// 提交
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
return postId, nil
|
||||
return postID, nil
|
||||
}
|
||||
|
||||
func (s *SysPostService) Update(ctx context.Context, id int32, req request.UpdatePostRequest) error {
|
||||
// 开启事务
|
||||
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return s.store.WithTx(ctx, func(q *sqlc.Queries) error {
|
||||
params := sqlc.UpdatePostParams{
|
||||
Title: req.Title,
|
||||
CoverID: req.CoverID,
|
||||
Slug: req.Slug,
|
||||
Content: req.Content,
|
||||
Summary: req.Summary,
|
||||
Status: req.Status,
|
||||
Sort: req.Sort,
|
||||
PublishedAt: req.PublishedAt,
|
||||
ID: id,
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = tx.Rollback(ctx)
|
||||
}()
|
||||
rows, err := q.UpdatePost(ctx, params)
|
||||
if err != nil {
|
||||
return dberr.MapUniqueViolation(err, dberr.PostSlugKey, errs.ErrSlugAlreadyExists)
|
||||
}
|
||||
|
||||
q := db.New(tx)
|
||||
if err = dberr.MapRowsAffected(rows, nil, errs.ErrPostNotFound); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
params := db.UpdatePostParams{
|
||||
Title: req.Title,
|
||||
CoverID: req.CoverID,
|
||||
Slug: req.Slug,
|
||||
Content: req.Content,
|
||||
Summary: req.Summary,
|
||||
Status: req.Status,
|
||||
Sort: req.Sort,
|
||||
PublishedAt: req.PublishedAt,
|
||||
ID: id,
|
||||
}
|
||||
// 先删除分类
|
||||
if err = q.DeletePostCategoryByPostID(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, err := q.UpdatePost(ctx, params)
|
||||
if err != nil {
|
||||
return dberr.MapUniqueViolation(err, dberr.PostSlugKey, errs.ErrSlugAlreadyExists)
|
||||
}
|
||||
if err = q.CreatePostCategory(ctx, sqlc.CreatePostCategoryParams{
|
||||
PostID: id,
|
||||
CategoryID: *req.CategoryID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = dberr.MapRowsAffected(rows, nil, errs.ErrPostNotFound); err != nil {
|
||||
return err
|
||||
}
|
||||
// 先删除标签
|
||||
if err = q.DeletePostTagByPostID(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = q.DeletePostCategory(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
var tagsParams []sqlc.CreatePostTagParams
|
||||
for _, tagID := range req.Tags {
|
||||
tagsParams = append(tagsParams, sqlc.CreatePostTagParams{
|
||||
PostID: id,
|
||||
TagID: tagID,
|
||||
})
|
||||
}
|
||||
|
||||
if err = q.CreatePostCategory(ctx, db.CreatePostCategoryParams{
|
||||
PostID: id,
|
||||
CategoryID: *req.CategoryID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = q.CreatePostTag(ctx, tagsParams); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 提交
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *SysPostService) Delete(ctx context.Context, id int32) error {
|
||||
rows, err := s.queries.DeletePost(ctx, id)
|
||||
return dberr.MapRowsAffected(rows, err, errs.ErrPostNotFound)
|
||||
return s.store.WithTx(ctx, func(q *sqlc.Queries) error {
|
||||
err := q.DeletePostCategoryByPostID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rows, err := q.DeletePost(ctx, id)
|
||||
return dberr.MapRowsAffected(rows, err, errs.ErrPostNotFound)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user