Skip to content

Commit 1d8fa7b

Browse files
committed
Provide minor fixes
1 parent cfac91e commit 1d8fa7b

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

data/repository/board_repository.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@ func (r *boardRepository) DeleteBoard(ctx context.Context, id string) (int64, er
138138
if err != nil {
139139
return err
140140
}
141-
if _, err = r.db.Threads.DeleteMany(ctx, bson.M{"board_id": id}); err != nil {
141+
if _, err = r.db.Threads.DeleteMany(ctx, bson.D{{"board_id", id}}); err != nil {
142142
return err
143143
}
144-
if _, err = r.db.Posts.DeleteMany(ctx, bson.M{"thread_id": threadId}); err != nil {
144+
if _, err = r.db.Posts.DeleteMany(ctx, bson.D{{"thread_id", threadId}}); err != nil {
145145
return err
146146
}
147147
return nil

data/repository/post_repository.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,26 @@ func (r *postRepository) GetPosts(ctx context.Context, threadId string, skip int
5050
}
5151

5252
func (r *postRepository) CreatePost(ctx context.Context, post *domain.Post) (string, error) {
53-
post.Id = primitive.NewObjectID().Hex()
54-
_, err := r.db.Posts.InsertOne(ctx, post)
53+
session, err := r.db.Boards.Database().Client().StartSession()
5554
if err != nil {
5655
return "", err
5756
}
57+
if err = mongo.WithSession(ctx, session, func(sc mongo.SessionContext) error {
58+
post.Id = primitive.NewObjectID().Hex()
59+
if _, err := r.db.Posts.InsertOne(ctx, post); err != nil {
60+
return err
61+
}
62+
count, err := r.db.Posts.CountDocuments(ctx, bson.M{"thread_id": post.ThreadId})
63+
if err != nil {
64+
return err
65+
}
66+
if _, err = r.db.Threads.UpdateOne(ctx, bson.M{"_id": post.ThreadId}, bson.M{"post_count": count}); err != nil {
67+
return err
68+
}
69+
return nil
70+
}); err != nil {
71+
return "", err
72+
}
5873
return post.Id, nil
5974
}
6075

0 commit comments

Comments
 (0)