Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit 9c3e83b

Browse files
committed
Impl DeleteUserBlog
1 parent 455ad6a commit 9c3e83b

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

app/server/user_blogs_server.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/ProgrammingLab/prolab-accounts/app/interceptor"
1616
"github.com/ProgrammingLab/prolab-accounts/app/util"
1717
"github.com/ProgrammingLab/prolab-accounts/infra/record"
18+
"github.com/ProgrammingLab/prolab-accounts/model"
1819
)
1920

2021
// UserBlogServiceServer is a composite interface of api_pb.UserBlogServiceServer and grapiserver.Server.
@@ -88,6 +89,11 @@ func (s *userBlogServiceServerImpl) UpdateUserBlog(ctx context.Context, req *api
8889
}
8990

9091
bs := s.UserBlogStore(ctx)
92+
93+
if err := s.canWrite(ctx, userID, b.ID); err != nil {
94+
return nil, err
95+
}
96+
9197
err = bs.UpdateUserBlog(b)
9298
if err != nil {
9399
if errors.Cause(err) == sql.ErrNoRows {
@@ -100,8 +106,39 @@ func (s *userBlogServiceServerImpl) UpdateUserBlog(ctx context.Context, req *api
100106
}
101107

102108
func (s *userBlogServiceServerImpl) DeleteUserBlog(ctx context.Context, req *api_pb.DeleteUserBlogRequest) (*empty.Empty, error) {
103-
// TODO: Not yet implemented.
104-
return nil, status.Error(codes.Unimplemented, "TODO: You should implement it!")
109+
userID, ok := interceptor.GetCurrentUserID(ctx)
110+
if !ok {
111+
return nil, util.ErrUnauthenticated
112+
}
113+
114+
blogID := int64(req.GetBlogId())
115+
bs := s.UserBlogStore(ctx)
116+
117+
if err := s.canWrite(ctx, userID, blogID); err != nil {
118+
return nil, err
119+
}
120+
121+
err := bs.DeleteUserBlog(blogID)
122+
if err != nil {
123+
return nil, err
124+
}
125+
126+
return &empty.Empty{}, nil
127+
}
128+
129+
func (s *userBlogServiceServerImpl) canWrite(ctx context.Context, userID model.UserID, blogID int64) error {
130+
bs := s.UserBlogStore(ctx)
131+
b, err := bs.GetUserBlog(int64(blogID))
132+
if err != nil {
133+
if errors.Cause(err) == sql.ErrNoRows {
134+
return util.ErrNotFound
135+
}
136+
return err
137+
}
138+
if b.UserID != int64(userID) {
139+
return util.ErrNotFound
140+
}
141+
return nil
105142
}
106143

107144
type blogRequest interface {

infra/store/user_blog/user_blog_store.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/pkg/errors"
88
"github.com/volatiletech/sqlboiler/boil"
9+
"github.com/volatiletech/sqlboiler/queries/qm"
910

1011
"github.com/ProgrammingLab/prolab-accounts/app/util"
1112
"github.com/ProgrammingLab/prolab-accounts/infra/record"
@@ -25,6 +26,14 @@ func NewUserBlogStore(ctx context.Context, db *sql.DB) store.UserBlogStore {
2526
}
2627
}
2728

29+
func (s *userBlogStoreImpl) GetUserBlog(blogID int64) (*record.Blog, error) {
30+
b, err := record.FindBlog(s.ctx, s.db, int64(blogID))
31+
if err != nil {
32+
return nil, errors.WithStack(err)
33+
}
34+
return b, nil
35+
}
36+
2837
func (s *userBlogStoreImpl) CreateUserBlog(blog *record.Blog) error {
2938
blog.ID = 0
3039
err := blog.Insert(s.ctx, s.db, boil.Infer())
@@ -72,5 +81,10 @@ func (s *userBlogStoreImpl) UpdateUserBlog(blog *record.Blog) error {
7281
}
7382

7483
func (s *userBlogStoreImpl) DeleteUserBlog(blogID int64) error {
75-
panic("not implemented")
84+
_, err := record.Blogs(qm.Where("id = ?", blogID)).DeleteAll(s.ctx, s.db)
85+
if err != nil {
86+
return errors.WithStack(err)
87+
}
88+
89+
return nil
7690
}

infra/store/user_blog_store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
// UserBlogStore accesses users data
88
type UserBlogStore interface {
9+
GetUserBlog(blogID int64) (*record.Blog, error)
910
CreateUserBlog(blog *record.Blog) error
1011
UpdateUserBlog(blog *record.Blog) error
1112
DeleteUserBlog(blogID int64) error

0 commit comments

Comments
 (0)