chore: initial commit
This commit is contained in:
163
internal/service/sys_post.go
Normal file
163
internal/service/sys_post.go
Normal file
@@ -0,0 +1,163 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
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
|
||||
}
|
||||
|
||||
func NewSysPostService(queries *db.Queries, pool *pgxpool.Pool) *SysPostService {
|
||||
return &SysPostService{
|
||||
queries: queries,
|
||||
pool: pool,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SysPostService) ListPage(ctx context.Context, p *common.Pagination) ([]db.ListPostsRow, int64, error) {
|
||||
params := db.ListPostsParams{
|
||||
Limit: p.PageSize,
|
||||
Offset: (p.Page - 1) * p.PageSize,
|
||||
}
|
||||
|
||||
total, err := s.queries.CountPosts(ctx)
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
list, err := s.queries.ListPosts(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 *SysPostService) FindByID(ctx context.Context, id int32) (*db.GetPostByIdRow, error) {
|
||||
post, err := s.queries.GetPostById(ctx, id)
|
||||
if err != nil {
|
||||
return nil, dberr.MapNoRows(err, errs.ErrPostNotFound)
|
||||
}
|
||||
url := httputil.BuildFileUrl(post.Cover)
|
||||
post.Cover = &url
|
||||
|
||||
return &post, nil
|
||||
}
|
||||
|
||||
func (s *SysPostService) Create(ctx context.Context, req request.CreatePostRequest) (int32, error) {
|
||||
// 开启事务
|
||||
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = tx.Rollback(ctx)
|
||||
}()
|
||||
|
||||
q := db.New(tx)
|
||||
|
||||
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,
|
||||
}
|
||||
|
||||
rows, err := q.UpdatePost(ctx, params)
|
||||
if err != nil {
|
||||
return dberr.MapUniqueViolation(err, dberr.PostSlugKey, errs.ErrSlugAlreadyExists)
|
||||
}
|
||||
|
||||
if err = dberr.MapRowsAffected(rows, nil, errs.ErrPostNotFound); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = q.DeletePostCategory(ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = q.CreatePostCategory(ctx, db.CreatePostCategoryParams{
|
||||
PostID: id,
|
||||
CategoryID: *req.CategoryID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 提交
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user