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

Commit 455ad6a

Browse files
committed
Impl UpdateUserBlog
1 parent d3a8eee commit 455ad6a

File tree

6 files changed

+99
-24
lines changed

6 files changed

+99
-24
lines changed

api/entries.validator.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/oauth.validator.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/users.validator.pb.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/server/user_blogs_server.go

Lines changed: 59 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ package server
22

33
import (
44
"context"
5+
"database/sql"
56

67
"github.com/golang/protobuf/ptypes/empty"
78
"github.com/izumin5210/grapi/pkg/grapiserver"
9+
"github.com/pkg/errors"
810
"google.golang.org/grpc/codes"
911
"google.golang.org/grpc/status"
1012

@@ -46,22 +48,9 @@ func (s *userBlogServiceServerImpl) CreateUserBlog(ctx context.Context, req *api
4648
}
4749

4850
blog := req.GetBlog()
49-
var feedURL string
50-
if req.GetAutoDetectFeed() {
51-
fs := s.FeedStore(ctx)
52-
u, err := fs.GetFeedURL(blog.GetUrl())
53-
if err != nil {
54-
return nil, ErrFeedURLDetectAutomatically
55-
}
56-
feedURL = u
57-
} else {
58-
u := blog.GetFeedUrl()
59-
fs := s.FeedStore(ctx)
60-
err := fs.IsValidFeedURL(u)
61-
if err != nil {
62-
return nil, ErrInvalidFeedURL
63-
}
64-
feedURL = u
51+
feedURL, err := getFeedURL(ctx, s, req)
52+
if err != nil {
53+
return nil, err
6554
}
6655

6756
b := &record.Blog{
@@ -71,7 +60,7 @@ func (s *userBlogServiceServerImpl) CreateUserBlog(ctx context.Context, req *api
7160
}
7261

7362
bs := s.UserBlogStore(ctx)
74-
err := bs.CreateUserBlog(b)
63+
err = bs.CreateUserBlog(b)
7564
if err != nil {
7665
return nil, err
7766
}
@@ -80,15 +69,66 @@ func (s *userBlogServiceServerImpl) CreateUserBlog(ctx context.Context, req *api
8069
}
8170

8271
func (s *userBlogServiceServerImpl) UpdateUserBlog(ctx context.Context, req *api_pb.UpdateUserBlogRequest) (*api_pb.Blog, error) {
83-
// TODO: Not yet implemented.
84-
return nil, status.Error(codes.Unimplemented, "TODO: You should implement it!")
72+
userID, ok := interceptor.GetCurrentUserID(ctx)
73+
if !ok {
74+
return nil, util.ErrUnauthenticated
75+
}
76+
77+
blog := req.GetBlog()
78+
feedURL, err := getFeedURL(ctx, s, req)
79+
if err != nil {
80+
return nil, err
81+
}
82+
83+
b := &record.Blog{
84+
ID: int64(blog.GetBlogId()),
85+
URL: blog.GetUrl(),
86+
FeedURL: feedURL,
87+
UserID: int64(userID),
88+
}
89+
90+
bs := s.UserBlogStore(ctx)
91+
err = bs.UpdateUserBlog(b)
92+
if err != nil {
93+
if errors.Cause(err) == sql.ErrNoRows {
94+
return nil, util.ErrNotFound
95+
}
96+
return nil, err
97+
}
98+
99+
return blogToResponse(b), nil
85100
}
86101

87102
func (s *userBlogServiceServerImpl) DeleteUserBlog(ctx context.Context, req *api_pb.DeleteUserBlogRequest) (*empty.Empty, error) {
88103
// TODO: Not yet implemented.
89104
return nil, status.Error(codes.Unimplemented, "TODO: You should implement it!")
90105
}
91106

107+
type blogRequest interface {
108+
GetBlog() *api_pb.Blog
109+
GetAutoDetectFeed() bool
110+
}
111+
112+
func getFeedURL(ctx context.Context, s di.StoreComponent, req blogRequest) (string, error) {
113+
blog := req.GetBlog()
114+
if req.GetAutoDetectFeed() {
115+
fs := s.FeedStore(ctx)
116+
u, err := fs.GetFeedURL(blog.GetUrl())
117+
if err != nil {
118+
return "", ErrFeedURLDetectAutomatically
119+
}
120+
return u, nil
121+
}
122+
123+
u := blog.GetFeedUrl()
124+
fs := s.FeedStore(ctx)
125+
err := fs.IsValidFeedURL(u)
126+
if err != nil {
127+
return "", ErrInvalidFeedURL
128+
}
129+
return u, nil
130+
}
131+
92132
func blogToResponse(blog *record.Blog) *api_pb.Blog {
93133
return &api_pb.Blog{
94134
BlogId: uint32(blog.ID),

app/util/errors.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import (
1010

1111
var (
1212
// ErrUnauthenticated represents unauthenticated error
13-
ErrUnauthenticated = status.Error(codes.Unauthenticated, "Unauthenticated")
13+
ErrUnauthenticated = status.Error(codes.Unauthenticated, "unauthenticated")
14+
// ErrNotFound represents not found error
15+
ErrNotFound = status.Error(codes.NotFound, "not found")
1416
)
1517

1618
// CodeFromHTTPStatus converts corresponding HTTP response status into the gRPC error code.

infra/store/user_blog/user_blog_store.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/pkg/errors"
88
"github.com/volatiletech/sqlboiler/boil"
99

10+
"github.com/ProgrammingLab/prolab-accounts/app/util"
1011
"github.com/ProgrammingLab/prolab-accounts/infra/record"
1112
"github.com/ProgrammingLab/prolab-accounts/infra/store"
1213
)
@@ -35,7 +36,39 @@ func (s *userBlogStoreImpl) CreateUserBlog(blog *record.Blog) error {
3536
}
3637

3738
func (s *userBlogStoreImpl) UpdateUserBlog(blog *record.Blog) error {
38-
panic("not implemented")
39+
tx, err := s.db.Begin()
40+
if err != nil {
41+
return errors.WithStack(err)
42+
}
43+
defer func() {
44+
if err = util.ErrorFromRecover(recover()); err != nil {
45+
_ = tx.Rollback()
46+
}
47+
}()
48+
49+
exists, err := record.FindBlog(s.ctx, tx, blog.ID)
50+
if err != nil {
51+
_ = tx.Rollback()
52+
return errors.WithStack(err)
53+
}
54+
if exists.UserID != blog.UserID {
55+
_ = tx.Rollback()
56+
return sql.ErrNoRows
57+
}
58+
59+
_, err = blog.Update(s.ctx, tx, boil.Infer())
60+
if err != nil {
61+
_ = tx.Rollback()
62+
return errors.WithStack(err)
63+
}
64+
65+
err = tx.Commit()
66+
if err != nil {
67+
_ = tx.Rollback()
68+
return errors.WithStack(err)
69+
}
70+
71+
return nil
3972
}
4073

4174
func (s *userBlogStoreImpl) DeleteUserBlog(blogID int64) error {

0 commit comments

Comments
 (0)