From df7e661b036d3af7f55f12c31cca080310f064c9 Mon Sep 17 00:00:00 2001 From: comavius Date: Fri, 7 Jun 2024 23:07:12 +0900 Subject: [PATCH 01/10] get user from db when getme and create if user not exist --- router/user.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/router/user.go b/router/user.go index eb5095e5..9853b869 100644 --- a/router/user.go +++ b/router/user.go @@ -82,11 +82,27 @@ func (h Handlers) GetMe(c echo.Context) error { h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } - user, ok := sess.Values[sessionUserKey].(User) + user_in_session, ok := sess.Values[sessionUserKey].(User) if !ok { h.Logger.Error("failed to parse stored session as user info") return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user info") } - return c.JSON(http.StatusOK, user) + users, err := h.Repository.GetUsers(c.Request().Context()) + if err != nil { + h.Logger.Error("failed to get users from repository", zap.Error(err)) + return echo.NewHTTPError(http.StatusInternalServerError, err) + } + for _, user_i := range users { + if user_i.ID == user_in_session.ID { + return c.JSON(http.StatusOK, user_i) + } + } + // if user not found, create new user + new_user, err := h.Repository.CreateUser(c.Request().Context(), user_in_session.Name, user_in_session.DisplayName, user_in_session.Admin) + if err != nil { + h.Logger.Error("failed to create user", zap.Error(err)) + return echo.NewHTTPError(http.StatusInternalServerError, err) + } + return c.JSON(http.StatusOK, new_user) } From 1feb362ff7cbb7958c25ba76d86202947dda70ec Mon Sep 17 00:00:00 2001 From: comavius Date: Tue, 9 Jul 2024 15:46:50 +0900 Subject: [PATCH 02/10] use GetUserByID --- router/user.go | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/router/user.go b/router/user.go index 9853b869..fde11ea4 100644 --- a/router/user.go +++ b/router/user.go @@ -88,21 +88,25 @@ func (h Handlers) GetMe(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user info") } - users, err := h.Repository.GetUsers(c.Request().Context()) + user, err := h.Repository.GetUserByID(c.Request().Context(), user_in_session.ID) if err != nil { - h.Logger.Error("failed to get users from repository", zap.Error(err)) - return echo.NewHTTPError(http.StatusInternalServerError, err) - } - for _, user_i := range users { - if user_i.ID == user_in_session.ID { - return c.JSON(http.StatusOK, user_i) + users, err := h.Repository.GetUsers(c.Request().Context()) + for _, user_i := range users { + if user_i.ID == user_in_session.ID { + return c.JSON(http.StatusOK, user_i) + } } + if err != nil { + h.Logger.Error("failed to get users from repository", zap.Error(err)) + return echo.NewHTTPError(http.StatusInternalServerError, err) + } + // if user not found, create new user + new_user, err := h.Repository.CreateUser(c.Request().Context(), user_in_session.Name, user_in_session.DisplayName, user_in_session.Admin) + if err != nil { + h.Logger.Error("failed to create user", zap.Error(err)) + return echo.NewHTTPError(http.StatusInternalServerError, err) + } + return c.JSON(http.StatusOK, new_user) } - // if user not found, create new user - new_user, err := h.Repository.CreateUser(c.Request().Context(), user_in_session.Name, user_in_session.DisplayName, user_in_session.Admin) - if err != nil { - h.Logger.Error("failed to create user", zap.Error(err)) - return echo.NewHTTPError(http.StatusInternalServerError, err) - } - return c.JSON(http.StatusOK, new_user) + return c.JSON(http.StatusOK, user) } From e432c1dae661fca03596b2ba8121e4e52d172595 Mon Sep 17 00:00:00 2001 From: Hueter Date: Fri, 8 Nov 2024 23:24:47 +0900 Subject: [PATCH 03/10] private variable name change to lower camel case --- router/user.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/router/user.go b/router/user.go index bf40042b..2003a882 100644 --- a/router/user.go +++ b/router/user.go @@ -85,17 +85,17 @@ func (h Handlers) GetMe(c echo.Context) error { h.Logger.Error("failed to get session", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } - user_in_session, ok := sess.Values[sessionUserKey].(User) + userInSession, ok := sess.Values[sessionUserKey].(User) if !ok { h.Logger.Error("failed to parse stored session as user info") return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user info") } - user, err := h.Repository.GetUserByID(c.Request().Context(), user_in_session.ID) + user, err := h.Repository.GetUserByID(c.Request().Context(), userInSession.ID) if err != nil { users, err := h.Repository.GetUsers(c.Request().Context()) for _, user_i := range users { - if user_i.ID == user_in_session.ID { + if user_i.ID == userInSession.ID { return c.JSON(http.StatusOK, user_i) } } @@ -104,7 +104,7 @@ func (h Handlers) GetMe(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, err) } // if user not found, create new user - new_user, err := h.Repository.CreateUser(c.Request().Context(), user_in_session.Name, user_in_session.DisplayName, user_in_session.Admin) + new_user, err := h.Repository.CreateUser(c.Request().Context(), userInSession.Name, userInSession.DisplayName, userInSession.Admin) if err != nil { h.Logger.Error("failed to create user", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) From 29fbad98ccbebf324c5454596d22e6c71d8fe5bc Mon Sep 17 00:00:00 2001 From: Hueter Date: Fri, 8 Nov 2024 23:25:39 +0900 Subject: [PATCH 04/10] fix golangci-lint error --- router/user.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/router/user.go b/router/user.go index 2003a882..83a27546 100644 --- a/router/user.go +++ b/router/user.go @@ -104,7 +104,8 @@ func (h Handlers) GetMe(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, err) } // if user not found, create new user - new_user, err := h.Repository.CreateUser(c.Request().Context(), userInSession.Name, userInSession.DisplayName, userInSession.Admin) + new_user, err := h.Repository.CreateUser(c.Request().Context(), userInSession.Name, + userInSession.DisplayName, userInSession.Admin) if err != nil { h.Logger.Error("failed to create user", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) From adee1a7805cf85b8ecfc884c02fa3dd64d24223c Mon Sep 17 00:00:00 2001 From: Hueter Date: Fri, 8 Nov 2024 23:45:31 +0900 Subject: [PATCH 05/10] private variable name change to lower camel case part 2 --- router/user.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/router/user.go b/router/user.go index 83a27546..f44eecbd 100644 --- a/router/user.go +++ b/router/user.go @@ -94,9 +94,9 @@ func (h Handlers) GetMe(c echo.Context) error { user, err := h.Repository.GetUserByID(c.Request().Context(), userInSession.ID) if err != nil { users, err := h.Repository.GetUsers(c.Request().Context()) - for _, user_i := range users { - if user_i.ID == userInSession.ID { - return c.JSON(http.StatusOK, user_i) + for _, user := range users { + if user.ID == userInSession.ID { + return c.JSON(http.StatusOK, user) } } if err != nil { @@ -104,13 +104,13 @@ func (h Handlers) GetMe(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, err) } // if user not found, create new user - new_user, err := h.Repository.CreateUser(c.Request().Context(), userInSession.Name, + newUser, err := h.Repository.CreateUser(c.Request().Context(), userInSession.Name, userInSession.DisplayName, userInSession.Admin) if err != nil { h.Logger.Error("failed to create user", zap.Error(err)) return echo.NewHTTPError(http.StatusInternalServerError, err) } - return c.JSON(http.StatusOK, new_user) + return c.JSON(http.StatusOK, newUser) } return c.JSON(http.StatusOK, user) } From 0f76080bd219e50a079e6abb9af864b2a8f5c0c8 Mon Sep 17 00:00:00 2001 From: Hueter Date: Sat, 9 Nov 2024 01:03:26 +0900 Subject: [PATCH 06/10] delete getting user form repository and creating new user --- router/user.go | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/router/user.go b/router/user.go index f44eecbd..8dcc524f 100644 --- a/router/user.go +++ b/router/user.go @@ -93,24 +93,8 @@ func (h Handlers) GetMe(c echo.Context) error { user, err := h.Repository.GetUserByID(c.Request().Context(), userInSession.ID) if err != nil { - users, err := h.Repository.GetUsers(c.Request().Context()) - for _, user := range users { - if user.ID == userInSession.ID { - return c.JSON(http.StatusOK, user) - } - } - if err != nil { - h.Logger.Error("failed to get users from repository", zap.Error(err)) - return echo.NewHTTPError(http.StatusInternalServerError, err) - } - // if user not found, create new user - newUser, err := h.Repository.CreateUser(c.Request().Context(), userInSession.Name, - userInSession.DisplayName, userInSession.Admin) - if err != nil { - h.Logger.Error("failed to create user", zap.Error(err)) - return echo.NewHTTPError(http.StatusInternalServerError, err) - } - return c.JSON(http.StatusOK, newUser) + h.Logger.Error("failed to get user by ID") + return c.JSON(http.StatusInternalServerError, err) } return c.JSON(http.StatusOK, user) } From a0ff91a25468fea3f99b9fccb084fd22945d4ec1 Mon Sep 17 00:00:00 2001 From: Hueter Date: Sat, 9 Nov 2024 01:23:18 +0900 Subject: [PATCH 07/10] add return err when user is not found --- router/user.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/router/user.go b/router/user.go index 8dcc524f..635da01b 100644 --- a/router/user.go +++ b/router/user.go @@ -8,6 +8,7 @@ import ( "github.com/labstack/echo-contrib/session" "github.com/labstack/echo/v4" "github.com/samber/lo" + "github.com/traPtitech/Jomon/ent" "github.com/traPtitech/Jomon/model" "go.uber.org/zap" ) @@ -93,6 +94,10 @@ func (h Handlers) GetMe(c echo.Context) error { user, err := h.Repository.GetUserByID(c.Request().Context(), userInSession.ID) if err != nil { + if ent.IsNotFound(err) { + h.Logger.Error("failed to find user from DB by ID") + return c.JSON(http.StatusNotFound, err) + } h.Logger.Error("failed to get user by ID") return c.JSON(http.StatusInternalServerError, err) } From c83bbe0515fee9bb5f4ae1460f0dda1b837dd0fb Mon Sep 17 00:00:00 2001 From: Hueter Date: Thu, 14 Nov 2024 23:15:52 +0900 Subject: [PATCH 08/10] fix test in TestHandlers_GetMe --- model/user.go | 14 +++++++------- router/user_test.go | 7 ++++++- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/model/user.go b/model/user.go index f8252ca2..c75554d3 100644 --- a/model/user.go +++ b/model/user.go @@ -9,13 +9,13 @@ import ( ) type User struct { - ID uuid.UUID - Name string - DisplayName string - Admin bool - CreatedAt time.Time - UpdatedAt time.Time - DeletedAt *time.Time + ID uuid.UUID `json:"id"` + Name string `json:"name"` + DisplayName string `json:"display_name"` + Admin bool `json:"admin"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` + DeletedAt *time.Time `json:"deleted_at"` } type UserRepository interface { diff --git a/router/user_test.go b/router/user_test.go index b5f28adb..b08047ac 100644 --- a/router/user_test.go +++ b/router/user_test.go @@ -289,7 +289,7 @@ func TestHandlers_GetMe(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() ctrl := gomock.NewController(t) - accessUser := makeUser(t, false) + accessUser := makeUser(t, random.Numeric(t, 2) == 1) user := User{ ID: accessUser.ID, Name: accessUser.Name, @@ -321,6 +321,11 @@ func TestHandlers_GetMe(t *testing.T) { sess.Values[sessionUserKey] = user require.NoError(t, sess.Save(c.Request(), c.Response())) + h.Repository.MockUserRepository. + EXPECT(). + GetUserByID(c.Request().Context(), user.ID). + Return(accessUser, nil) + err = h.Handlers.GetMe(c) if assert.NoError(t, err) { assert.Equal(t, http.StatusOK, rec.Code) From 17f536008431e80a960ec39f68844e2d7bc39853 Mon Sep 17 00:00:00 2001 From: Hueter Date: Fri, 15 Nov 2024 01:49:54 +0900 Subject: [PATCH 09/10] change 'expect return' model.User to User --- model/user.go | 14 +++++++------- router/user_test.go | 27 ++++++++++++++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/model/user.go b/model/user.go index c75554d3..f8252ca2 100644 --- a/model/user.go +++ b/model/user.go @@ -9,13 +9,13 @@ import ( ) type User struct { - ID uuid.UUID `json:"id"` - Name string `json:"name"` - DisplayName string `json:"display_name"` - Admin bool `json:"admin"` - CreatedAt time.Time `json:"created_at"` - UpdatedAt time.Time `json:"updated_at"` - DeletedAt *time.Time `json:"deleted_at"` + ID uuid.UUID + Name string + DisplayName string + Admin bool + CreatedAt time.Time + UpdatedAt time.Time + DeletedAt *time.Time } type UserRepository interface { diff --git a/router/user_test.go b/router/user_test.go index b08047ac..2d216cd6 100644 --- a/router/user_test.go +++ b/router/user_test.go @@ -22,6 +22,18 @@ import ( "github.com/traPtitech/Jomon/testutil/random" ) +func userFromModelUser(u model.User) User { + return User{ + ID: u.ID, + Name: u.Name, + DisplayName: u.DisplayName, + Admin: u.Admin, + CreatedAt: u.CreatedAt, + UpdatedAt: u.UpdatedAt, + DeletedAt: u.DeletedAt, + } +} + func TestHandlers_GetUsers(t *testing.T) { t.Parallel() @@ -289,14 +301,14 @@ func TestHandlers_GetMe(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() ctrl := gomock.NewController(t) - accessUser := makeUser(t, random.Numeric(t, 2) == 1) + accessModelUser := makeUser(t, random.Numeric(t, 2) == 1) user := User{ - ID: accessUser.ID, - Name: accessUser.Name, - DisplayName: accessUser.DisplayName, - Admin: accessUser.Admin, - CreatedAt: accessUser.CreatedAt, - UpdatedAt: accessUser.UpdatedAt, + ID: accessModelUser.ID, + Name: accessModelUser.Name, + DisplayName: accessModelUser.DisplayName, + Admin: accessModelUser.Admin, + CreatedAt: accessModelUser.CreatedAt, + UpdatedAt: accessModelUser.UpdatedAt, } bodyAccessUser, err := json.Marshal(user) assert.NoError(t, err) @@ -320,6 +332,7 @@ func TestHandlers_GetMe(t *testing.T) { require.NoError(t, err) sess.Values[sessionUserKey] = user require.NoError(t, sess.Save(c.Request(), c.Response())) + accessUser := userFromModelUser(*accessModelUser) h.Repository.MockUserRepository. EXPECT(). From a95baa42f4e8bc5d6abc47044c5d233a367b1e2a Mon Sep 17 00:00:00 2001 From: Hueter Date: Fri, 15 Nov 2024 15:59:31 +0900 Subject: [PATCH 10/10] change GetMe return model.User to router.User --- router/user.go | 15 ++++++++++++++- router/user_test.go | 27 +++++++-------------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/router/user.go b/router/user.go index 635da01b..f41965fd 100644 --- a/router/user.go +++ b/router/user.go @@ -80,6 +80,18 @@ func (h Handlers) UpdateUserInfo(c echo.Context) error { }) } +func userFromModelUser(u model.User) User { + return User{ + ID: u.ID, + Name: u.Name, + DisplayName: u.DisplayName, + Admin: u.Admin, + CreatedAt: u.CreatedAt, + UpdatedAt: u.UpdatedAt, + DeletedAt: u.DeletedAt, + } +} + func (h Handlers) GetMe(c echo.Context) error { sess, err := session.Get(h.SessionName, c) if err != nil { @@ -92,7 +104,7 @@ func (h Handlers) GetMe(c echo.Context) error { return echo.NewHTTPError(http.StatusInternalServerError, "failed to get user info") } - user, err := h.Repository.GetUserByID(c.Request().Context(), userInSession.ID) + modelUser, err := h.Repository.GetUserByID(c.Request().Context(), userInSession.ID) if err != nil { if ent.IsNotFound(err) { h.Logger.Error("failed to find user from DB by ID") @@ -101,5 +113,6 @@ func (h Handlers) GetMe(c echo.Context) error { h.Logger.Error("failed to get user by ID") return c.JSON(http.StatusInternalServerError, err) } + user := userFromModelUser(*modelUser) return c.JSON(http.StatusOK, user) } diff --git a/router/user_test.go b/router/user_test.go index 2d216cd6..b08047ac 100644 --- a/router/user_test.go +++ b/router/user_test.go @@ -22,18 +22,6 @@ import ( "github.com/traPtitech/Jomon/testutil/random" ) -func userFromModelUser(u model.User) User { - return User{ - ID: u.ID, - Name: u.Name, - DisplayName: u.DisplayName, - Admin: u.Admin, - CreatedAt: u.CreatedAt, - UpdatedAt: u.UpdatedAt, - DeletedAt: u.DeletedAt, - } -} - func TestHandlers_GetUsers(t *testing.T) { t.Parallel() @@ -301,14 +289,14 @@ func TestHandlers_GetMe(t *testing.T) { t.Run("Success", func(t *testing.T) { t.Parallel() ctrl := gomock.NewController(t) - accessModelUser := makeUser(t, random.Numeric(t, 2) == 1) + accessUser := makeUser(t, random.Numeric(t, 2) == 1) user := User{ - ID: accessModelUser.ID, - Name: accessModelUser.Name, - DisplayName: accessModelUser.DisplayName, - Admin: accessModelUser.Admin, - CreatedAt: accessModelUser.CreatedAt, - UpdatedAt: accessModelUser.UpdatedAt, + ID: accessUser.ID, + Name: accessUser.Name, + DisplayName: accessUser.DisplayName, + Admin: accessUser.Admin, + CreatedAt: accessUser.CreatedAt, + UpdatedAt: accessUser.UpdatedAt, } bodyAccessUser, err := json.Marshal(user) assert.NoError(t, err) @@ -332,7 +320,6 @@ func TestHandlers_GetMe(t *testing.T) { require.NoError(t, err) sess.Values[sessionUserKey] = user require.NoError(t, sess.Save(c.Request(), c.Response())) - accessUser := userFromModelUser(*accessModelUser) h.Repository.MockUserRepository. EXPECT().