Skip to content

Commit af4a404

Browse files
committed
fix: 修复别名错误
1 parent 87cd4aa commit af4a404

File tree

2 files changed

+7
-13
lines changed

2 files changed

+7
-13
lines changed

internal/post/internal/repository/dao/post.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ func (d *PostDao) GetAllPublishPost(ctx context.Context) ([]*Post, error) {
227227
func (d *PostDao) FindByAlias(ctx context.Context, alias string) (*Post, error) {
228228
post, err := d.coll.Finder().Filter(query.Eq("alias", alias)).FindOne(ctx)
229229
if err != nil {
230-
if errors.Is(err, mongo.ErrNoDocuments) {
231-
return nil, nil
232-
}
233230
return nil, err
234231
}
235232
return post, nil

internal/post/internal/service/post.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/codepzj/stellux/server/internal/post/internal/domain"
99
"github.com/codepzj/stellux/server/internal/post/internal/repository"
1010
"go.mongodb.org/mongo-driver/v2/bson"
11+
"go.mongodb.org/mongo-driver/v2/mongo"
1112
)
1213

1314
type IPostService interface {
@@ -41,25 +42,21 @@ type PostService struct {
4142
}
4243

4344
func (s *PostService) AdminCreatePost(ctx context.Context, post *domain.Post) error {
44-
existPost, err := s.repo.FindByAlias(ctx, post.Alias)
45-
if err != nil {
45+
if existPost, err := s.repo.FindByAlias(ctx, post.Alias); err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
4646
return err
47-
}
48-
if existPost != nil {
47+
} else if existPost != nil {
4948
return errors.New("别名已存在")
5049
}
5150
return s.repo.Create(ctx, post)
5251
}
5352

5453
func (s *PostService) AdminUpdatePost(ctx context.Context, post *domain.Post) error {
55-
existPost, err := s.repo.FindByAlias(ctx, post.Alias)
56-
if err != nil {
54+
if existPost, err := s.repo.FindByAlias(ctx, post.Alias); err != nil && !errors.Is(err, mongo.ErrNoDocuments) {
5755
return err
56+
} else if existPost != nil && existPost.Id != post.Id {
57+
return errors.New("别名已存在")
5858
}
59-
if existPost == nil || existPost.Id == post.Id {
60-
return s.repo.Update(ctx, post)
61-
}
62-
return errors.New("别名已存在")
59+
return s.repo.Update(ctx, post)
6360
}
6461

6562
func (s *PostService) AdminUpdatePostPublishStatus(ctx context.Context, id bson.ObjectID, isPublish bool) error {

0 commit comments

Comments
 (0)