From 481838e7511e397e5942c0665b89a2f7f338dab9 Mon Sep 17 00:00:00 2001 From: Nzt3 Date: Fri, 5 Jan 2024 21:17:21 +0900 Subject: [PATCH 1/7] use go-traq in GetGroup --- infra/traq/group.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/infra/traq/group.go b/infra/traq/group.go index 3e3d1938..4f29f692 100644 --- a/infra/traq/group.go +++ b/infra/traq/group.go @@ -1,6 +1,7 @@ package traq import ( + "context" "encoding/json" "fmt" "net/http" @@ -12,18 +13,30 @@ import ( ) func (repo *TraQRepository) GetGroup(token *oauth2.Token, groupID uuid.UUID) (*traq.UserGroup, error) { - URL := fmt.Sprintf("%s/groups/%s", repo.URL, groupID) - req, err := http.NewRequest(http.MethodGet, URL, nil) + // URL := fmt.Sprintf("%s/groups/%s", repo.URL, groupID) + // req, err := http.NewRequest(http.MethodGet, URL, nil) + // if err != nil { + // return nil, err + // } + // data, err := repo.doRequest(token, req) + // if err != nil { + // return nil, err + // } + + // group := new(traq.UserGroup) + // err = json.Unmarshal(data, &group) + // fmt.Println("GetGroup") + ctx := context.TODO() + apiClient := NewAPIClient(ctx, token) + group, resp, err := apiClient.GroupApi.GetUserGroup(ctx, groupID.String()).Execute() if err != nil { return nil, err } - data, err := repo.doRequest(token, req) + err = handleStatusCode(resp.StatusCode) if err != nil { return nil, err } - - group := new(traq.UserGroup) - err = json.Unmarshal(data, &group) + // fmt.Println("GetOK") return group, err } From 46e16fa6239fc3d9f3e8fdbcfd84bc898026dbb6 Mon Sep 17 00:00:00 2001 From: Nzt3 Date: Fri, 5 Jan 2024 22:16:34 +0900 Subject: [PATCH 2/7] use go-traq in GetAllGroups --- infra/traq/group.go | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/infra/traq/group.go b/infra/traq/group.go index 4f29f692..9c0e4460 100644 --- a/infra/traq/group.go +++ b/infra/traq/group.go @@ -25,7 +25,6 @@ func (repo *TraQRepository) GetGroup(token *oauth2.Token, groupID uuid.UUID) (*t // group := new(traq.UserGroup) // err = json.Unmarshal(data, &group) - // fmt.Println("GetGroup") ctx := context.TODO() apiClient := NewAPIClient(ctx, token) group, resp, err := apiClient.GroupApi.GetUserGroup(ctx, groupID.String()).Execute() @@ -36,24 +35,38 @@ func (repo *TraQRepository) GetGroup(token *oauth2.Token, groupID uuid.UUID) (*t if err != nil { return nil, err } - // fmt.Println("GetOK") return group, err } func (repo *TraQRepository) GetAllGroups(token *oauth2.Token) ([]*traq.UserGroup, error) { - URL := fmt.Sprintf("%s/groups", repo.URL) - req, err := http.NewRequest(http.MethodGet, URL, nil) + // URL := fmt.Sprintf("%s/groups", repo.URL) + // req, err := http.NewRequest(http.MethodGet, URL, nil) + // if err != nil { + // return nil, err + // } + // data, err := repo.doRequest(token, req) + // if err != nil { + // return nil, err + // } + + // groups := make([]*traq.UserGroup, 0) + // err = json.Unmarshal(data, &groups) + ctx := context.TODO() + apiClient := NewAPIClient(ctx, token) + groups, resp, err := apiClient.GroupApi.GetUserGroups(ctx).Execute() if err != nil { return nil, err } - data, err := repo.doRequest(token, req) + err = handleStatusCode(resp.StatusCode) if err != nil { return nil, err } - - groups := make([]*traq.UserGroup, 0) - err = json.Unmarshal(data, &groups) - return groups, err + ret := make([]*traq.UserGroup, len(groups)) + for i, _g := range groups { + g := _g + ret[i] = &g + } + return ret, err } func (repo *TraQRepository) GetUserBelongingGroupIDs(token *oauth2.Token, userID uuid.UUID) ([]uuid.UUID, error) { From 54ad11bdfc4d22d5effe151d51f1f0bf602c0f4b Mon Sep 17 00:00:00 2001 From: Nzt3 Date: Fri, 5 Jan 2024 23:02:08 +0900 Subject: [PATCH 3/7] use go-traq in GetUserBelongingGroupIDs --- infra/traq/group.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/infra/traq/group.go b/infra/traq/group.go index 9c0e4460..1e6a7f96 100644 --- a/infra/traq/group.go +++ b/infra/traq/group.go @@ -2,9 +2,6 @@ package traq import ( "context" - "encoding/json" - "fmt" - "net/http" "github.com/gofrs/uuid" "golang.org/x/oauth2" @@ -70,19 +67,29 @@ func (repo *TraQRepository) GetAllGroups(token *oauth2.Token) ([]*traq.UserGroup } func (repo *TraQRepository) GetUserBelongingGroupIDs(token *oauth2.Token, userID uuid.UUID) ([]uuid.UUID, error) { - URL := fmt.Sprintf("%s/users/%s", repo.URL, userID) - req, err := http.NewRequest(http.MethodGet, URL, nil) + // URL := fmt.Sprintf("%s/users/%s", repo.URL, userID) + // req, err := http.NewRequest(http.MethodGet, URL, nil) + // if err != nil { + // return nil, err + // } + // data, err := repo.doRequest(token, req) + // if err != nil { + // return nil, err + // } + // user := new(traq.UserDetail) + // if err := json.Unmarshal(data, &user); err != nil { + // return nil, err + // } + ctx := context.TODO() + apiClient := NewAPIClient(ctx, token) + user, resp, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute() if err != nil { return nil, err } - data, err := repo.doRequest(token, req) + err = handleStatusCode(resp.StatusCode) if err != nil { return nil, err } - user := new(traq.UserDetail) - if err := json.Unmarshal(data, &user); err != nil { - return nil, err - } groups := make([]uuid.UUID, 0, len(user.Groups)) for _, gid := range user.Groups { groupUUID, err := uuid.FromString(gid) From e3a1f8522e016d7e51a93cefc2eeefe232518e81 Mon Sep 17 00:00:00 2001 From: Nzt3 Date: Fri, 5 Jan 2024 23:04:33 +0900 Subject: [PATCH 4/7] remove previous code --- infra/traq/group.go | 38 -------------------------------------- 1 file changed, 38 deletions(-) diff --git a/infra/traq/group.go b/infra/traq/group.go index 1e6a7f96..8eb7c3fc 100644 --- a/infra/traq/group.go +++ b/infra/traq/group.go @@ -10,18 +10,6 @@ import ( ) func (repo *TraQRepository) GetGroup(token *oauth2.Token, groupID uuid.UUID) (*traq.UserGroup, error) { - // URL := fmt.Sprintf("%s/groups/%s", repo.URL, groupID) - // req, err := http.NewRequest(http.MethodGet, URL, nil) - // if err != nil { - // return nil, err - // } - // data, err := repo.doRequest(token, req) - // if err != nil { - // return nil, err - // } - - // group := new(traq.UserGroup) - // err = json.Unmarshal(data, &group) ctx := context.TODO() apiClient := NewAPIClient(ctx, token) group, resp, err := apiClient.GroupApi.GetUserGroup(ctx, groupID.String()).Execute() @@ -36,18 +24,6 @@ func (repo *TraQRepository) GetGroup(token *oauth2.Token, groupID uuid.UUID) (*t } func (repo *TraQRepository) GetAllGroups(token *oauth2.Token) ([]*traq.UserGroup, error) { - // URL := fmt.Sprintf("%s/groups", repo.URL) - // req, err := http.NewRequest(http.MethodGet, URL, nil) - // if err != nil { - // return nil, err - // } - // data, err := repo.doRequest(token, req) - // if err != nil { - // return nil, err - // } - - // groups := make([]*traq.UserGroup, 0) - // err = json.Unmarshal(data, &groups) ctx := context.TODO() apiClient := NewAPIClient(ctx, token) groups, resp, err := apiClient.GroupApi.GetUserGroups(ctx).Execute() @@ -67,19 +43,6 @@ func (repo *TraQRepository) GetAllGroups(token *oauth2.Token) ([]*traq.UserGroup } func (repo *TraQRepository) GetUserBelongingGroupIDs(token *oauth2.Token, userID uuid.UUID) ([]uuid.UUID, error) { - // URL := fmt.Sprintf("%s/users/%s", repo.URL, userID) - // req, err := http.NewRequest(http.MethodGet, URL, nil) - // if err != nil { - // return nil, err - // } - // data, err := repo.doRequest(token, req) - // if err != nil { - // return nil, err - // } - // user := new(traq.UserDetail) - // if err := json.Unmarshal(data, &user); err != nil { - // return nil, err - // } ctx := context.TODO() apiClient := NewAPIClient(ctx, token) user, resp, err := apiClient.UserApi.GetUser(ctx, userID.String()).Execute() @@ -98,6 +61,5 @@ func (repo *TraQRepository) GetUserBelongingGroupIDs(token *oauth2.Token, userID } groups = append(groups, groupUUID) } - return groups, err } From 27f42f4818ae95b7678c61459d53e1fedcb4f05e Mon Sep 17 00:00:00 2001 From: Nzt3 Date: Fri, 12 Jan 2024 20:39:06 +0900 Subject: [PATCH 5/7] change GetAllGroups return type --- infra/traq/group.go | 9 ++------- repository/converter.go | 8 +++----- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/infra/traq/group.go b/infra/traq/group.go index 8eb7c3fc..c0ef2821 100644 --- a/infra/traq/group.go +++ b/infra/traq/group.go @@ -23,7 +23,7 @@ func (repo *TraQRepository) GetGroup(token *oauth2.Token, groupID uuid.UUID) (*t return group, err } -func (repo *TraQRepository) GetAllGroups(token *oauth2.Token) ([]*traq.UserGroup, error) { +func (repo *TraQRepository) GetAllGroups(token *oauth2.Token) ([]traq.UserGroup, error) { ctx := context.TODO() apiClient := NewAPIClient(ctx, token) groups, resp, err := apiClient.GroupApi.GetUserGroups(ctx).Execute() @@ -34,12 +34,7 @@ func (repo *TraQRepository) GetAllGroups(token *oauth2.Token) ([]*traq.UserGroup if err != nil { return nil, err } - ret := make([]*traq.UserGroup, len(groups)) - for i, _g := range groups { - g := _g - ret[i] = &g - } - return ret, err + return groups, err } func (repo *TraQRepository) GetUserBelongingGroupIDs(token *oauth2.Token, userID uuid.UUID) ([]uuid.UUID, error) { diff --git a/repository/converter.go b/repository/converter.go index 2f7f6fb4..26e84e65 100644 --- a/repository/converter.go +++ b/repository/converter.go @@ -14,13 +14,11 @@ func ConvPtraqUserToPdomainUser(src *traq.User) (dst *domain.User) { return } -func ConvSPtraqUserGroupToSPdomainGroup(src []*traq.UserGroup) (dst []*domain.Group) { +func ConvSPtraqUserGroupToSPdomainGroup(src []traq.UserGroup) (dst []*domain.Group) { dst = make([]*domain.Group, len(src)) for i := range src { - if src[i] != nil { - dst[i] = new(domain.Group) - (*dst[i]) = ConvtraqUserGroupTodomainGroup((*src[i])) - } + dst[i] = new(domain.Group) + (*dst[i]) = ConvtraqUserGroupTodomainGroup((src[i])) } return } From 873952a9d74debad47cc294f3a2628f9dbf90319 Mon Sep 17 00:00:00 2001 From: Nzt3 Date: Fri, 12 Jan 2024 20:57:05 +0900 Subject: [PATCH 6/7] fix unnecessary code --- repository/converter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/repository/converter.go b/repository/converter.go index 26e84e65..5486d67c 100644 --- a/repository/converter.go +++ b/repository/converter.go @@ -18,7 +18,7 @@ func ConvSPtraqUserGroupToSPdomainGroup(src []traq.UserGroup) (dst []*domain.Gro dst = make([]*domain.Group, len(src)) for i := range src { dst[i] = new(domain.Group) - (*dst[i]) = ConvtraqUserGroupTodomainGroup((src[i])) + (*dst[i]) = ConvtraqUserGroupTodomainGroup(src[i]) } return } From 8c516c366271a86c48fc31ed1fff920592c34872 Mon Sep 17 00:00:00 2001 From: Nzt3 Date: Fri, 12 Jan 2024 22:21:32 +0900 Subject: [PATCH 7/7] delete unused function --- infra/traq/traq.go | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/infra/traq/traq.go b/infra/traq/traq.go index adfe458d..5162acd3 100644 --- a/infra/traq/traq.go +++ b/infra/traq/traq.go @@ -5,8 +5,6 @@ import ( "crypto/sha256" "encoding/base64" "errors" - "io" - "net/http" "net/url" "github.com/traPtitech/go-traq" @@ -66,19 +64,6 @@ func (repo *TraQRepository) GetOAuthToken(query, state, codeVerifier string) (*o return repo.Config.Exchange(ctx, code, option) } -func (repo *TraQRepository) doRequest(token *oauth2.Token, req *http.Request) ([]byte, error) { - client := repo.Config.Client(context.TODO(), token) - resp, err := client.Do(req) - if err != nil { - return nil, err - } - err = handleStatusCode(resp.StatusCode) - if err != nil { - return nil, err - } - return io.ReadAll(resp.Body) -} - func NewAPIClient(ctx context.Context, token *oauth2.Token) *traq.APIClient { traqconf := traq.NewConfiguration() conf := TraQDefaultConfig