Skip to content

Commit b38c99a

Browse files
committed
Provide minor fixes
1 parent 998ce29 commit b38c99a

File tree

5 files changed

+33
-0
lines changed

5 files changed

+33
-0
lines changed

data/repository/post_repository.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ func (r *postRepository) GetPosts(ctx context.Context, threadId string, skip int
4949
return data, nil
5050
}
5151

52+
func (r *postRepository) GetPostById(ctx context.Context, id string) (*domain.Post, error) {
53+
data := new(domain.Post)
54+
if err := r.db.Posts.FindOne(ctx, bson.M{"_id": id}).Decode(&data); err != nil {
55+
return nil, err
56+
}
57+
return data, nil
58+
}
59+
5260
func (r *postRepository) CreatePost(ctx context.Context, post *domain.Post) (string, error) {
5361
session, err := r.db.Boards.Database().Client().StartSession()
5462
if err != nil {

domain/repository.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type ThreadRepository interface {
2424

2525
type PostRepository interface {
2626
GetPosts(ctx context.Context, threadId string, skip int64, limit int64) ([]*Post, error)
27+
GetPostById(ctx context.Context, id string) (*Post, error)
2728
CreatePost(ctx context.Context, post *Post) (string, error)
2829
DeletePost(ctx context.Context, id string) (int64, error)
2930
}

infrastructure/handler/post_handler.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
type PostHandler interface {
1212
GetPosts(req *proto.GetPostsReq, stream proto.PostService_GetPostsServer) error
13+
GetPostById(ctx context.Context, req *proto.GetPostByIdReq) (*proto.GetPostByIdRes, error)
1314
CreatePost(ctx context.Context, req *proto.CreatePostReq) (*proto.CreatePostRes, error)
1415
DeletePost(ctx context.Context, req *proto.DeletePostReq) (*proto.DeletePostRes, error)
1516
}
@@ -38,6 +39,15 @@ func (h *postHandler) GetPosts(req *proto.GetPostsReq, stream proto.PostService_
3839
return nil
3940
}
4041

42+
func (h *postHandler) GetPostById(ctx context.Context, req *proto.GetPostByIdReq) (*proto.GetPostByIdRes, error) {
43+
post, err := h.useCase.GetPostById(ctx, req.GetId())
44+
if err != nil {
45+
log.Println(err)
46+
return &proto.GetPostByIdRes{}, nil
47+
}
48+
return &proto.GetPostByIdRes{Post: mapping.PostToProto(post)}, nil
49+
}
50+
4151
func (h *postHandler) CreatePost(ctx context.Context, req *proto.CreatePostReq) (*proto.CreatePostRes, error) {
4252
id, err := h.useCase.CreatePost(ctx, mapping.PostToModel(req.GetPost()))
4353
if err != nil {

proto/post.proto

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ option go_package = "./proto";
66

77
service PostService{
88
rpc GetPosts(GetPostsReq) returns (stream GetPostsRes);
9+
rpc GetPostById(GetPostByIdReq) returns (GetPostByIdRes);
910
rpc CreatePost(CreatePostReq) returns (CreatePostRes);
1011
rpc DeletePost(DeletePostReq) returns (DeletePostRes);
1112
}
@@ -20,6 +21,14 @@ message GetPostsRes{
2021
Post post = 1;
2122
}
2223

24+
message GetPostByIdReq{
25+
string id = 1;
26+
}
27+
28+
message GetPostByIdRes{
29+
Post post = 1;
30+
}
31+
2332
message CreatePostReq{
2433
Post post = 1;
2534
}

usecase/post_usecase.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
type PostUseCase interface {
99
GetPosts(ctx context.Context, threadId string, skip int64, limit int64) ([]*domain.Post, error)
10+
GetPostById(ctx context.Context, id string) (*domain.Post, error)
1011
CreatePost(ctx context.Context, post *domain.Post) (string, error)
1112
DeletePost(ctx context.Context, id string) (int64, error)
1213
}
@@ -23,6 +24,10 @@ func (u *postUseCase) GetPosts(ctx context.Context, threadId string, skip int64,
2324
return u.Repository.GetPosts(ctx, threadId, skip, limit)
2425
}
2526

27+
func (u *postUseCase) GetPostById(ctx context.Context, id string) (*domain.Post, error) {
28+
return u.Repository.GetPostById(ctx, id)
29+
}
30+
2631
func (u *postUseCase) CreatePost(ctx context.Context, post *domain.Post) (string, error) {
2732
return u.Repository.CreatePost(ctx, post)
2833
}

0 commit comments

Comments
 (0)