|
8 | 8 | "github.com/codepzj/stellux/server/internal/post/internal/domain" |
9 | 9 | "github.com/codepzj/stellux/server/internal/post/internal/repository" |
10 | 10 | "go.mongodb.org/mongo-driver/v2/bson" |
| 11 | + "go.mongodb.org/mongo-driver/v2/mongo" |
11 | 12 | ) |
12 | 13 |
|
13 | 14 | type IPostService interface { |
@@ -41,25 +42,21 @@ type PostService struct { |
41 | 42 | } |
42 | 43 |
|
43 | 44 | 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) { |
46 | 46 | return err |
47 | | - } |
48 | | - if existPost != nil { |
| 47 | + } else if existPost != nil { |
49 | 48 | return errors.New("别名已存在") |
50 | 49 | } |
51 | 50 | return s.repo.Create(ctx, post) |
52 | 51 | } |
53 | 52 |
|
54 | 53 | 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) { |
57 | 55 | return err |
| 56 | + } else if existPost != nil && existPost.Id != post.Id { |
| 57 | + return errors.New("别名已存在") |
58 | 58 | } |
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) |
63 | 60 | } |
64 | 61 |
|
65 | 62 | func (s *PostService) AdminUpdatePostPublishStatus(ctx context.Context, id bson.ObjectID, isPublish bool) error { |
|
0 commit comments