From 40de43a15737109899e26e8c5f4e6e0b91e525fa Mon Sep 17 00:00:00 2001 From: ras0q Date: Fri, 27 Oct 2023 09:55:41 +0900 Subject: [PATCH 1/3] :recycle: move interfaces/external to infrastructure/external --- infrastructure/{ => external}/apiclient.go | 2 +- infrastructure/{ => external}/knoq.go | 52 +++++++++++++------ .../external/mock_external/mock_knoq.go | 2 +- .../external/mock_external/mock_portal.go | 2 +- .../external/mock_external/mock_traq.go | 2 +- .../external/mock_external_e2e/mock_knoq.go | 2 +- .../external/mock_external_e2e/mock_portal.go | 2 +- .../external/mock_external_e2e/mock_traq.go | 2 +- infrastructure/{ => external}/portal.go | 41 +++++++++------ infrastructure/{ => external}/traq.go | 44 +++++++++++----- infrastructure/injector.go | 7 +-- infrastructure/repository/contest_impl.go | 2 +- .../repository/contest_impl_test.go | 2 +- infrastructure/repository/event_impl.go | 2 +- infrastructure/repository/event_impl_test.go | 4 +- infrastructure/repository/project_impl.go | 2 +- .../repository/project_impl_test.go | 4 +- infrastructure/repository/testutil_test.go | 2 +- infrastructure/repository/user_impl.go | 2 +- infrastructure/repository/user_impl_test.go | 4 +- integration_tests/repository/contest_test.go | 2 +- integration_tests/repository/event_test.go | 2 +- integration_tests/repository/project_test.go | 2 +- integration_tests/repository/user_test.go | 2 +- interfaces/external/knoq.go | 28 ---------- interfaces/external/portal.go | 14 ----- interfaces/external/traq.go | 23 -------- util/mockdata/external.go | 2 +- 28 files changed, 120 insertions(+), 137 deletions(-) rename infrastructure/{ => external}/apiclient.go (97%) rename infrastructure/{ => external}/knoq.go (51%) rename {interfaces => infrastructure}/external/mock_external/mock_knoq.go (97%) rename {interfaces => infrastructure}/external/mock_external/mock_portal.go (96%) rename {interfaces => infrastructure}/external/mock_external/mock_traq.go (96%) rename {interfaces => infrastructure}/external/mock_external_e2e/mock_knoq.go (94%) rename {interfaces => infrastructure}/external/mock_external_e2e/mock_portal.go (90%) rename {interfaces => infrastructure}/external/mock_external_e2e/mock_traq.go (93%) rename infrastructure/{ => external}/portal.go (62%) rename infrastructure/{ => external}/traq.go (51%) delete mode 100644 interfaces/external/knoq.go delete mode 100644 interfaces/external/portal.go delete mode 100644 interfaces/external/traq.go diff --git a/infrastructure/apiclient.go b/infrastructure/external/apiclient.go similarity index 97% rename from infrastructure/apiclient.go rename to infrastructure/external/apiclient.go index 53c99016..76353857 100644 --- a/infrastructure/apiclient.go +++ b/infrastructure/external/apiclient.go @@ -1,4 +1,4 @@ -package infrastructure +package external import ( "fmt" diff --git a/infrastructure/knoq.go b/infrastructure/external/knoq.go similarity index 51% rename from infrastructure/knoq.go rename to infrastructure/external/knoq.go index c370fdd9..7e2890da 100644 --- a/infrastructure/knoq.go +++ b/infrastructure/external/knoq.go @@ -1,36 +1,56 @@ -package infrastructure +//go:generate go run github.com/golang/mock/mockgen@latest -source=$GOFILE -destination=mock_$GOPACKAGE/mock_$GOFILE + +package external import ( "encoding/json" "errors" "fmt" "net/http" + "time" "github.com/gofrs/uuid" - "github.com/traPtitech/traPortfolio/interfaces/external" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external_e2e" "github.com/traPtitech/traPortfolio/usecases/repository" "github.com/traPtitech/traPortfolio/util/config" ) -type KnoqAPI struct { +type EventResponse struct { + ID uuid.UUID `json:"eventId"` + Name string `json:"name"` + Description string `json:"description"` + Place string `json:"place"` + GroupID uuid.UUID `json:"groupId"` + RoomID uuid.UUID `json:"roomId"` + TimeStart time.Time `json:"timeStart"` + TimeEnd time.Time `json:"timeEnd"` + SharedRoom bool `json:"sharedRoom"` + Admins []uuid.UUID `json:"admins"` +} + +type KnoqAPI interface { + GetEvents() ([]*EventResponse, error) + GetEvent(eventID uuid.UUID) (*EventResponse, error) + GetEventsByUserID(userID uuid.UUID) ([]*EventResponse, error) +} + +type knoqAPI struct { apiClient } -func NewKnoqAPI(conf *config.KnoqConfig, isDevelopment bool) (external.KnoqAPI, error) { - if isDevelopment { - return &mock_external_e2e.MockKnoqAPI{}, nil - } +func NewKnoqAPI(conf *config.KnoqConfig, isDevelopment bool) (KnoqAPI, error) { + // if isDevelopment { + // return &mock_external_e2e.MockKnoqAPI{}, nil + // } jar, err := newCookieJar(conf.API(), "session") if err != nil { return nil, err } - return &KnoqAPI{newAPIClient(jar, conf.API())}, nil + return &knoqAPI{newAPIClient(jar, conf.API())}, nil } -func (a *KnoqAPI) GetEvents() ([]*external.EventResponse, error) { +func (a *knoqAPI) GetEvents() ([]*EventResponse, error) { res, err := a.apiGet("/events") if err != nil { return nil, err @@ -41,14 +61,14 @@ func (a *KnoqAPI) GetEvents() ([]*external.EventResponse, error) { return nil, errors.New("GET /events failed") } - var er []*external.EventResponse + var er []*EventResponse if err := json.NewDecoder(res.Body).Decode(&er); err != nil { return nil, err } return er, nil } -func (a *KnoqAPI) GetEvent(eventID uuid.UUID) (*external.EventResponse, error) { +func (a *knoqAPI) GetEvent(eventID uuid.UUID) (*EventResponse, error) { res, err := a.apiGet(fmt.Sprintf("/events/%s", eventID)) if err != nil { return nil, err @@ -63,14 +83,14 @@ func (a *KnoqAPI) GetEvent(eventID uuid.UUID) (*external.EventResponse, error) { return nil, fmt.Errorf("GET /events/%s failed: %d", eventID, res.StatusCode) } - var er external.EventResponse + var er EventResponse if err := json.NewDecoder(res.Body).Decode(&er); err != nil { return nil, err } return &er, nil } -func (a *KnoqAPI) GetEventsByUserID(userID uuid.UUID) ([]*external.EventResponse, error) { +func (a *knoqAPI) GetEventsByUserID(userID uuid.UUID) ([]*EventResponse, error) { res, err := a.apiGet(fmt.Sprintf("/users/%s/events", userID)) if err != nil { return nil, err @@ -81,7 +101,7 @@ func (a *KnoqAPI) GetEventsByUserID(userID uuid.UUID) ([]*external.EventResponse return nil, fmt.Errorf("GET /users/%s/events failed", userID) } - var er []*external.EventResponse + var er []*EventResponse if err := json.NewDecoder(res.Body).Decode(&er); err != nil { return nil, err } @@ -90,5 +110,5 @@ func (a *KnoqAPI) GetEventsByUserID(userID uuid.UUID) ([]*external.EventResponse // Interface guards var ( - _ external.KnoqAPI = (*KnoqAPI)(nil) + _ KnoqAPI = (*knoqAPI)(nil) ) diff --git a/interfaces/external/mock_external/mock_knoq.go b/infrastructure/external/mock_external/mock_knoq.go similarity index 97% rename from interfaces/external/mock_external/mock_knoq.go rename to infrastructure/external/mock_external/mock_knoq.go index daaf9018..42881cdf 100644 --- a/interfaces/external/mock_external/mock_knoq.go +++ b/infrastructure/external/mock_external/mock_knoq.go @@ -9,7 +9,7 @@ import ( uuid "github.com/gofrs/uuid" gomock "github.com/golang/mock/gomock" - external "github.com/traPtitech/traPortfolio/interfaces/external" + external "github.com/traPtitech/traPortfolio/infrastructure/external" ) // MockKnoqAPI is a mock of KnoqAPI interface. diff --git a/interfaces/external/mock_external/mock_portal.go b/infrastructure/external/mock_external/mock_portal.go similarity index 96% rename from interfaces/external/mock_external/mock_portal.go rename to infrastructure/external/mock_external/mock_portal.go index d41cc7f4..83c015b9 100644 --- a/interfaces/external/mock_external/mock_portal.go +++ b/infrastructure/external/mock_external/mock_portal.go @@ -8,7 +8,7 @@ import ( reflect "reflect" gomock "github.com/golang/mock/gomock" - external "github.com/traPtitech/traPortfolio/interfaces/external" + external "github.com/traPtitech/traPortfolio/infrastructure/external" ) // MockPortalAPI is a mock of PortalAPI interface. diff --git a/interfaces/external/mock_external/mock_traq.go b/infrastructure/external/mock_external/mock_traq.go similarity index 96% rename from interfaces/external/mock_external/mock_traq.go rename to infrastructure/external/mock_external/mock_traq.go index 1f091326..dfa64392 100644 --- a/interfaces/external/mock_external/mock_traq.go +++ b/infrastructure/external/mock_external/mock_traq.go @@ -9,7 +9,7 @@ import ( uuid "github.com/gofrs/uuid" gomock "github.com/golang/mock/gomock" - external "github.com/traPtitech/traPortfolio/interfaces/external" + external "github.com/traPtitech/traPortfolio/infrastructure/external" ) // MockTraQAPI is a mock of TraQAPI interface. diff --git a/interfaces/external/mock_external_e2e/mock_knoq.go b/infrastructure/external/mock_external_e2e/mock_knoq.go similarity index 94% rename from interfaces/external/mock_external_e2e/mock_knoq.go rename to infrastructure/external/mock_external_e2e/mock_knoq.go index 79641a60..7099ed48 100644 --- a/interfaces/external/mock_external_e2e/mock_knoq.go +++ b/infrastructure/external/mock_external_e2e/mock_knoq.go @@ -2,7 +2,7 @@ package mock_external_e2e //nolint:revive import ( "github.com/gofrs/uuid" - "github.com/traPtitech/traPortfolio/interfaces/external" + "github.com/traPtitech/traPortfolio/infrastructure/external" "github.com/traPtitech/traPortfolio/usecases/repository" "github.com/traPtitech/traPortfolio/util/mockdata" ) diff --git a/interfaces/external/mock_external_e2e/mock_portal.go b/infrastructure/external/mock_external_e2e/mock_portal.go similarity index 90% rename from interfaces/external/mock_external_e2e/mock_portal.go rename to infrastructure/external/mock_external_e2e/mock_portal.go index 799e176b..3a37ec6c 100644 --- a/interfaces/external/mock_external_e2e/mock_portal.go +++ b/infrastructure/external/mock_external_e2e/mock_portal.go @@ -3,7 +3,7 @@ package mock_external_e2e //nolint:revive import ( "fmt" - "github.com/traPtitech/traPortfolio/interfaces/external" + "github.com/traPtitech/traPortfolio/infrastructure/external" "github.com/traPtitech/traPortfolio/util/mockdata" ) diff --git a/interfaces/external/mock_external_e2e/mock_traq.go b/infrastructure/external/mock_external_e2e/mock_traq.go similarity index 93% rename from interfaces/external/mock_external_e2e/mock_traq.go rename to infrastructure/external/mock_external_e2e/mock_traq.go index aa4c941e..f46c0e47 100644 --- a/interfaces/external/mock_external_e2e/mock_traq.go +++ b/infrastructure/external/mock_external_e2e/mock_traq.go @@ -5,7 +5,7 @@ import ( "github.com/gofrs/uuid" "github.com/traPtitech/traPortfolio/domain" - "github.com/traPtitech/traPortfolio/interfaces/external" + "github.com/traPtitech/traPortfolio/infrastructure/external" "github.com/traPtitech/traPortfolio/util/mockdata" ) diff --git a/infrastructure/portal.go b/infrastructure/external/portal.go similarity index 62% rename from infrastructure/portal.go rename to infrastructure/external/portal.go index bf47d2a2..70c8a706 100644 --- a/infrastructure/portal.go +++ b/infrastructure/external/portal.go @@ -1,4 +1,6 @@ -package infrastructure +//go:generate go run github.com/golang/mock/mockgen@latest -source=$GOFILE -destination=mock_$GOPACKAGE/mock_$GOFILE + +package external import ( "encoding/json" @@ -7,40 +9,49 @@ import ( "time" "github.com/patrickmn/go-cache" - "github.com/traPtitech/traPortfolio/interfaces/external" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external_e2e" "github.com/traPtitech/traPortfolio/util/config" ) +type PortalUserResponse struct { + TraQID string `json:"id"` + RealName string `json:"name"` + AlphabeticName string `json:"alphabeticName"` +} + +type PortalAPI interface { + GetUsers() ([]*PortalUserResponse, error) + GetUserByTraqID(traQID string) (*PortalUserResponse, error) +} + const ( cacheKey = "portalUsers" ) -type PortalAPI struct { +type portalAPI struct { apiClient cache *cache.Cache } -func NewPortalAPI(conf *config.PortalConfig, isDevelopment bool) (external.PortalAPI, error) { - if isDevelopment { - return mock_external_e2e.NewMockPortalAPI(), nil - } +func NewPortalAPI(conf *config.PortalConfig, isDevelopment bool) (PortalAPI, error) { + // if isDevelopment { + // return mock_external_e2e.NewMockPortalAPI(), nil + // } jar, err := newCookieJar(conf.API(), "access_token") if err != nil { return nil, err } - return &PortalAPI{ + return &portalAPI{ apiClient: newAPIClient(jar, conf.API()), cache: cache.New(1*time.Hour, 2*time.Hour), }, nil } -func (a *PortalAPI) GetUsers() ([]*external.PortalUserResponse, error) { +func (a *portalAPI) GetUsers() ([]*PortalUserResponse, error) { portalUsers, found := a.cache.Get(cacheKey) if found { - return portalUsers.([]*external.PortalUserResponse), nil + return portalUsers.([]*PortalUserResponse), nil } res, err := a.apiGet("/user") @@ -52,7 +63,7 @@ func (a *PortalAPI) GetUsers() ([]*external.PortalUserResponse, error) { if res.StatusCode != http.StatusOK { return nil, fmt.Errorf("GET /user failed: %v", res.Status) } - var userResponses []*external.PortalUserResponse + var userResponses []*PortalUserResponse if err := json.NewDecoder(res.Body).Decode(&userResponses); err != nil { return nil, fmt.Errorf("decode failed: %w", err) } @@ -60,7 +71,7 @@ func (a *PortalAPI) GetUsers() ([]*external.PortalUserResponse, error) { return userResponses, nil } -func (a *PortalAPI) GetUserByTraqID(traQID string) (*external.PortalUserResponse, error) { +func (a *portalAPI) GetUserByTraqID(traQID string) (*PortalUserResponse, error) { if traQID == "" { return nil, fmt.Errorf("invalid traQID") } @@ -75,7 +86,7 @@ func (a *PortalAPI) GetUserByTraqID(traQID string) (*external.PortalUserResponse return nil, fmt.Errorf("GET /user/%v failed: %v", traQID, res.Status) } - var userResponse external.PortalUserResponse + var userResponse PortalUserResponse if err := json.NewDecoder(res.Body).Decode(&userResponse); err != nil { return nil, fmt.Errorf("decode failed: %w", err) } @@ -84,5 +95,5 @@ func (a *PortalAPI) GetUserByTraqID(traQID string) (*external.PortalUserResponse // Interface guards var ( - _ external.PortalAPI = (*PortalAPI)(nil) + _ PortalAPI = (*portalAPI)(nil) ) diff --git a/infrastructure/traq.go b/infrastructure/external/traq.go similarity index 51% rename from infrastructure/traq.go rename to infrastructure/external/traq.go index 3743b3d3..982627ce 100644 --- a/infrastructure/traq.go +++ b/infrastructure/external/traq.go @@ -1,4 +1,6 @@ -package infrastructure +//go:generate go run github.com/golang/mock/mockgen@latest -source=$GOFILE -destination=mock_$GOPACKAGE/mock_$GOFILE + +package external import ( "encoding/json" @@ -6,29 +8,43 @@ import ( "net/http" "github.com/gofrs/uuid" - "github.com/traPtitech/traPortfolio/interfaces/external" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external_e2e" + "github.com/traPtitech/traPortfolio/domain" "github.com/traPtitech/traPortfolio/util/config" ) -type TraQAPI struct { +type TraQUserResponse struct { + ID uuid.UUID `json:"id"` + State domain.TraQState `json:"state"` +} + +type TraQGetAllArgs struct { + IncludeSuspended bool + Name string +} + +type TraQAPI interface { + GetUsers(args *TraQGetAllArgs) ([]*TraQUserResponse, error) + GetUser(userID uuid.UUID) (*TraQUserResponse, error) +} + +type traQAPI struct { apiClient } -func NewTraQAPI(conf *config.TraqConfig, isDevelopment bool) (external.TraQAPI, error) { - if isDevelopment { - return mock_external_e2e.NewMockTraQAPI(), nil - } +func NewTraQAPI(conf *config.TraqConfig, isDevelopment bool) (TraQAPI, error) { + // if isDevelopment { + // return mock_external_e2e.NewMockTraQAPI(), nil + // } jar, err := newCookieJar(conf.API(), "r_session") if err != nil { return nil, err } - return &TraQAPI{newAPIClient(jar, conf.API())}, nil + return &traQAPI{newAPIClient(jar, conf.API())}, nil } -func (a *TraQAPI) GetUsers(args *external.TraQGetAllArgs) ([]*external.TraQUserResponse, error) { +func (a *traQAPI) GetUsers(args *TraQGetAllArgs) ([]*TraQUserResponse, error) { res, err := a.apiGet(fmt.Sprintf("/users?include-suspended=%t&name=%s", args.IncludeSuspended, args.Name)) if err != nil { return nil, err @@ -39,14 +55,14 @@ func (a *TraQAPI) GetUsers(args *external.TraQGetAllArgs) ([]*external.TraQUserR return nil, fmt.Errorf("GET /users failed: %v", res.Status) } - var usersResponse []*external.TraQUserResponse + var usersResponse []*TraQUserResponse if err := json.NewDecoder(res.Body).Decode(&usersResponse); err != nil { return nil, fmt.Errorf("decode failed: %w", err) } return usersResponse, nil } -func (a *TraQAPI) GetUser(userID uuid.UUID) (*external.TraQUserResponse, error) { +func (a *traQAPI) GetUser(userID uuid.UUID) (*TraQUserResponse, error) { res, err := a.apiGet(fmt.Sprintf("/users/%v", userID)) if err != nil { return nil, err @@ -57,7 +73,7 @@ func (a *TraQAPI) GetUser(userID uuid.UUID) (*external.TraQUserResponse, error) return nil, fmt.Errorf("GET /users/%v failed: %v", userID, res.Status) } - var userResponse external.TraQUserResponse + var userResponse TraQUserResponse if err := json.NewDecoder(res.Body).Decode(&userResponse); err != nil { return nil, fmt.Errorf("decode failed: %v", err) } @@ -66,5 +82,5 @@ func (a *TraQAPI) GetUser(userID uuid.UUID) (*external.TraQUserResponse, error) // Interface guards var ( - _ external.TraQAPI = (*TraQAPI)(nil) + _ TraQAPI = (*traQAPI)(nil) ) diff --git a/infrastructure/injector.go b/infrastructure/injector.go index be7aae50..a8449768 100644 --- a/infrastructure/injector.go +++ b/infrastructure/injector.go @@ -1,6 +1,7 @@ package infrastructure import ( + "github.com/traPtitech/traPortfolio/infrastructure/external" "github.com/traPtitech/traPortfolio/infrastructure/repository" "github.com/traPtitech/traPortfolio/interfaces/handler" "github.com/traPtitech/traPortfolio/usecases/service" @@ -10,15 +11,15 @@ import ( func InjectAPIServer(c *config.Config, db *gorm.DB) (handler.API, error) { // external API - portalAPI, err := NewPortalAPI(c.PortalConf(), !c.IsProduction) + portalAPI, err := external.NewPortalAPI(c.PortalConf(), !c.IsProduction) if err != nil { return handler.API{}, err } - traQAPI, err := NewTraQAPI(c.TraqConf(), !c.IsProduction) + traQAPI, err := external.NewTraQAPI(c.TraqConf(), !c.IsProduction) if err != nil { return handler.API{}, err } - knoqAPI, err := NewKnoqAPI(c.KnoqConf(), !c.IsProduction) + knoqAPI, err := external.NewKnoqAPI(c.KnoqConf(), !c.IsProduction) if err != nil { return handler.API{}, err } diff --git a/infrastructure/repository/contest_impl.go b/infrastructure/repository/contest_impl.go index 07885d21..46a5c5c9 100644 --- a/infrastructure/repository/contest_impl.go +++ b/infrastructure/repository/contest_impl.go @@ -5,8 +5,8 @@ import ( "github.com/gofrs/uuid" "github.com/traPtitech/traPortfolio/domain" + "github.com/traPtitech/traPortfolio/infrastructure/external" "github.com/traPtitech/traPortfolio/infrastructure/repository/model" - "github.com/traPtitech/traPortfolio/interfaces/external" "github.com/traPtitech/traPortfolio/usecases/repository" "gorm.io/gorm" ) diff --git a/infrastructure/repository/contest_impl_test.go b/infrastructure/repository/contest_impl_test.go index b9184f10..b3d9ad8d 100644 --- a/infrastructure/repository/contest_impl_test.go +++ b/infrastructure/repository/contest_impl_test.go @@ -11,7 +11,7 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" "github.com/traPtitech/traPortfolio/domain" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external" + "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external" "github.com/traPtitech/traPortfolio/usecases/repository" "github.com/traPtitech/traPortfolio/util/optional" "github.com/traPtitech/traPortfolio/util/random" diff --git a/infrastructure/repository/event_impl.go b/infrastructure/repository/event_impl.go index d85ec640..34d9d1a6 100644 --- a/infrastructure/repository/event_impl.go +++ b/infrastructure/repository/event_impl.go @@ -7,8 +7,8 @@ import ( "github.com/gofrs/uuid" "github.com/samber/lo" "github.com/traPtitech/traPortfolio/domain" + "github.com/traPtitech/traPortfolio/infrastructure/external" "github.com/traPtitech/traPortfolio/infrastructure/repository/model" - "github.com/traPtitech/traPortfolio/interfaces/external" "github.com/traPtitech/traPortfolio/usecases/repository" "gorm.io/gorm" ) diff --git a/infrastructure/repository/event_impl_test.go b/infrastructure/repository/event_impl_test.go index 830c2d2e..5f8eafef 100644 --- a/infrastructure/repository/event_impl_test.go +++ b/infrastructure/repository/event_impl_test.go @@ -10,8 +10,8 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" "github.com/traPtitech/traPortfolio/domain" - "github.com/traPtitech/traPortfolio/interfaces/external" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external" + "github.com/traPtitech/traPortfolio/infrastructure/external" + "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external" "github.com/traPtitech/traPortfolio/usecases/repository" "github.com/traPtitech/traPortfolio/util/optional" "github.com/traPtitech/traPortfolio/util/random" diff --git a/infrastructure/repository/project_impl.go b/infrastructure/repository/project_impl.go index a28e79dc..6ad192f3 100644 --- a/infrastructure/repository/project_impl.go +++ b/infrastructure/repository/project_impl.go @@ -5,8 +5,8 @@ import ( "github.com/gofrs/uuid" "github.com/traPtitech/traPortfolio/domain" + "github.com/traPtitech/traPortfolio/infrastructure/external" "github.com/traPtitech/traPortfolio/infrastructure/repository/model" - "github.com/traPtitech/traPortfolio/interfaces/external" "github.com/traPtitech/traPortfolio/usecases/repository" "github.com/traPtitech/traPortfolio/util/random" "gorm.io/gorm" diff --git a/infrastructure/repository/project_impl_test.go b/infrastructure/repository/project_impl_test.go index 404d15c8..81ab1b72 100644 --- a/infrastructure/repository/project_impl_test.go +++ b/infrastructure/repository/project_impl_test.go @@ -10,8 +10,8 @@ import ( "github.com/golang/mock/gomock" "github.com/stretchr/testify/assert" "github.com/traPtitech/traPortfolio/domain" - "github.com/traPtitech/traPortfolio/interfaces/external" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external" + "github.com/traPtitech/traPortfolio/infrastructure/external" + "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external" "github.com/traPtitech/traPortfolio/usecases/repository" "github.com/traPtitech/traPortfolio/util/optional" "github.com/traPtitech/traPortfolio/util/random" diff --git a/infrastructure/repository/testutil_test.go b/infrastructure/repository/testutil_test.go index df6da06a..2824b582 100644 --- a/infrastructure/repository/testutil_test.go +++ b/infrastructure/repository/testutil_test.go @@ -14,7 +14,7 @@ import ( "github.com/DATA-DOG/go-sqlmock" "github.com/gofrs/uuid" "github.com/traPtitech/traPortfolio/domain" - "github.com/traPtitech/traPortfolio/interfaces/external" + "github.com/traPtitech/traPortfolio/infrastructure/external" "github.com/traPtitech/traPortfolio/usecases/repository" "github.com/traPtitech/traPortfolio/util/random" "gorm.io/driver/mysql" diff --git a/infrastructure/repository/user_impl.go b/infrastructure/repository/user_impl.go index b5149b99..6e3e81c6 100644 --- a/infrastructure/repository/user_impl.go +++ b/infrastructure/repository/user_impl.go @@ -6,8 +6,8 @@ import ( "github.com/gofrs/uuid" "github.com/traPtitech/traPortfolio/domain" + "github.com/traPtitech/traPortfolio/infrastructure/external" "github.com/traPtitech/traPortfolio/infrastructure/repository/model" - "github.com/traPtitech/traPortfolio/interfaces/external" "github.com/traPtitech/traPortfolio/usecases/repository" "gorm.io/gorm" ) diff --git a/infrastructure/repository/user_impl_test.go b/infrastructure/repository/user_impl_test.go index d71920e4..282424c1 100644 --- a/infrastructure/repository/user_impl_test.go +++ b/infrastructure/repository/user_impl_test.go @@ -15,8 +15,8 @@ import ( "github.com/gofrs/uuid" "github.com/stretchr/testify/assert" "github.com/traPtitech/traPortfolio/domain" - "github.com/traPtitech/traPortfolio/interfaces/external" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external" + "github.com/traPtitech/traPortfolio/infrastructure/external" + "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external" "github.com/traPtitech/traPortfolio/usecases/repository" ) diff --git a/integration_tests/repository/contest_test.go b/integration_tests/repository/contest_test.go index 3b664517..4c6c999c 100644 --- a/integration_tests/repository/contest_test.go +++ b/integration_tests/repository/contest_test.go @@ -8,8 +8,8 @@ import ( "github.com/gofrs/uuid" "github.com/stretchr/testify/assert" "github.com/traPtitech/traPortfolio/domain" + "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external_e2e" "github.com/traPtitech/traPortfolio/integration_tests/testutils" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external_e2e" irepository "github.com/traPtitech/traPortfolio/infrastructure/repository" urepository "github.com/traPtitech/traPortfolio/usecases/repository" diff --git a/integration_tests/repository/event_test.go b/integration_tests/repository/event_test.go index 1570345d..999fdc00 100644 --- a/integration_tests/repository/event_test.go +++ b/integration_tests/repository/event_test.go @@ -8,9 +8,9 @@ import ( "github.com/gofrs/uuid" "github.com/stretchr/testify/assert" "github.com/traPtitech/traPortfolio/domain" + "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external_e2e" irepository "github.com/traPtitech/traPortfolio/infrastructure/repository" "github.com/traPtitech/traPortfolio/integration_tests/testutils" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external_e2e" urepository "github.com/traPtitech/traPortfolio/usecases/repository" "github.com/traPtitech/traPortfolio/util/mockdata" "github.com/traPtitech/traPortfolio/util/optional" diff --git a/integration_tests/repository/project_test.go b/integration_tests/repository/project_test.go index 8580b28a..2364a5c5 100644 --- a/integration_tests/repository/project_test.go +++ b/integration_tests/repository/project_test.go @@ -11,9 +11,9 @@ import ( "github.com/google/go-cmp/cmp/cmpopts" "github.com/stretchr/testify/assert" "github.com/traPtitech/traPortfolio/domain" + "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external_e2e" irepository "github.com/traPtitech/traPortfolio/infrastructure/repository" "github.com/traPtitech/traPortfolio/integration_tests/testutils" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external_e2e" "github.com/traPtitech/traPortfolio/util/mockdata" "github.com/traPtitech/traPortfolio/util/random" ) diff --git a/integration_tests/repository/user_test.go b/integration_tests/repository/user_test.go index 92a874a8..40648872 100644 --- a/integration_tests/repository/user_test.go +++ b/integration_tests/repository/user_test.go @@ -9,7 +9,7 @@ import ( "github.com/traPtitech/traPortfolio/domain" irepository "github.com/traPtitech/traPortfolio/infrastructure/repository" "github.com/traPtitech/traPortfolio/integration_tests/testutils" - "github.com/traPtitech/traPortfolio/interfaces/external/mock_external_e2e" + "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external_e2e" urepository "github.com/traPtitech/traPortfolio/usecases/repository" "github.com/traPtitech/traPortfolio/util/mockdata" diff --git a/interfaces/external/knoq.go b/interfaces/external/knoq.go deleted file mode 100644 index 737a0ee5..00000000 --- a/interfaces/external/knoq.go +++ /dev/null @@ -1,28 +0,0 @@ -//go:generate go run github.com/golang/mock/mockgen@latest -source=$GOFILE -destination=mock_$GOPACKAGE/mock_$GOFILE - -package external - -import ( - "time" - - "github.com/gofrs/uuid" -) - -type EventResponse struct { - ID uuid.UUID `json:"eventId"` - Name string `json:"name"` - Description string `json:"description"` - Place string `json:"place"` - GroupID uuid.UUID `json:"groupId"` - RoomID uuid.UUID `json:"roomId"` - TimeStart time.Time `json:"timeStart"` - TimeEnd time.Time `json:"timeEnd"` - SharedRoom bool `json:"sharedRoom"` - Admins []uuid.UUID `json:"admins"` -} - -type KnoqAPI interface { - GetEvents() ([]*EventResponse, error) - GetEvent(eventID uuid.UUID) (*EventResponse, error) - GetEventsByUserID(userID uuid.UUID) ([]*EventResponse, error) -} diff --git a/interfaces/external/portal.go b/interfaces/external/portal.go deleted file mode 100644 index e3b08388..00000000 --- a/interfaces/external/portal.go +++ /dev/null @@ -1,14 +0,0 @@ -//go:generate go run github.com/golang/mock/mockgen@latest -source=$GOFILE -destination=mock_$GOPACKAGE/mock_$GOFILE - -package external - -type PortalUserResponse struct { - TraQID string `json:"id"` - RealName string `json:"name"` - AlphabeticName string `json:"alphabeticName"` -} - -type PortalAPI interface { - GetUsers() ([]*PortalUserResponse, error) - GetUserByTraqID(traQID string) (*PortalUserResponse, error) -} diff --git a/interfaces/external/traq.go b/interfaces/external/traq.go deleted file mode 100644 index 4ab5d188..00000000 --- a/interfaces/external/traq.go +++ /dev/null @@ -1,23 +0,0 @@ -//go:generate go run github.com/golang/mock/mockgen@latest -source=$GOFILE -destination=mock_$GOPACKAGE/mock_$GOFILE - -package external - -import ( - "github.com/gofrs/uuid" - "github.com/traPtitech/traPortfolio/domain" -) - -type TraQUserResponse struct { - ID uuid.UUID `json:"id"` - State domain.TraQState `json:"state"` -} - -type TraQGetAllArgs struct { - IncludeSuspended bool - Name string -} - -type TraQAPI interface { - GetUsers(args *TraQGetAllArgs) ([]*TraQUserResponse, error) - GetUser(userID uuid.UUID) (*TraQUserResponse, error) -} diff --git a/util/mockdata/external.go b/util/mockdata/external.go index 00980034..3824de82 100644 --- a/util/mockdata/external.go +++ b/util/mockdata/external.go @@ -5,7 +5,7 @@ import ( "github.com/gofrs/uuid" "github.com/traPtitech/traPortfolio/domain" - "github.com/traPtitech/traPortfolio/interfaces/external" + "github.com/traPtitech/traPortfolio/infrastructure/external" ) type TraQUser struct { From 69d2350157ae7ec29846026b6ff7c08e5fb4daff Mon Sep 17 00:00:00 2001 From: ras0q Date: Fri, 27 Oct 2023 15:47:15 +0900 Subject: [PATCH 2/3] :recycle: change external mock from injector.go --- infrastructure/external/knoq.go | 6 +---- infrastructure/external/portal.go | 6 +---- infrastructure/external/traq.go | 6 +---- infrastructure/injector.go | 39 ++++++++++++++++++++++--------- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/infrastructure/external/knoq.go b/infrastructure/external/knoq.go index 7e2890da..1781ae28 100644 --- a/infrastructure/external/knoq.go +++ b/infrastructure/external/knoq.go @@ -37,11 +37,7 @@ type knoqAPI struct { apiClient } -func NewKnoqAPI(conf *config.KnoqConfig, isDevelopment bool) (KnoqAPI, error) { - // if isDevelopment { - // return &mock_external_e2e.MockKnoqAPI{}, nil - // } - +func NewKnoqAPI(conf *config.KnoqConfig) (KnoqAPI, error) { jar, err := newCookieJar(conf.API(), "session") if err != nil { return nil, err diff --git a/infrastructure/external/portal.go b/infrastructure/external/portal.go index 70c8a706..806ca512 100644 --- a/infrastructure/external/portal.go +++ b/infrastructure/external/portal.go @@ -32,11 +32,7 @@ type portalAPI struct { cache *cache.Cache } -func NewPortalAPI(conf *config.PortalConfig, isDevelopment bool) (PortalAPI, error) { - // if isDevelopment { - // return mock_external_e2e.NewMockPortalAPI(), nil - // } - +func NewPortalAPI(conf *config.PortalConfig) (PortalAPI, error) { jar, err := newCookieJar(conf.API(), "access_token") if err != nil { return nil, err diff --git a/infrastructure/external/traq.go b/infrastructure/external/traq.go index 982627ce..40ce26d2 100644 --- a/infrastructure/external/traq.go +++ b/infrastructure/external/traq.go @@ -31,11 +31,7 @@ type traQAPI struct { apiClient } -func NewTraQAPI(conf *config.TraqConfig, isDevelopment bool) (TraQAPI, error) { - // if isDevelopment { - // return mock_external_e2e.NewMockTraQAPI(), nil - // } - +func NewTraQAPI(conf *config.TraqConfig) (TraQAPI, error) { jar, err := newCookieJar(conf.API(), "r_session") if err != nil { return nil, err diff --git a/infrastructure/injector.go b/infrastructure/injector.go index a8449768..47808798 100644 --- a/infrastructure/injector.go +++ b/infrastructure/injector.go @@ -2,6 +2,7 @@ package infrastructure import ( "github.com/traPtitech/traPortfolio/infrastructure/external" + "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external_e2e" "github.com/traPtitech/traPortfolio/infrastructure/repository" "github.com/traPtitech/traPortfolio/interfaces/handler" "github.com/traPtitech/traPortfolio/usecases/service" @@ -11,17 +12,33 @@ import ( func InjectAPIServer(c *config.Config, db *gorm.DB) (handler.API, error) { // external API - portalAPI, err := external.NewPortalAPI(c.PortalConf(), !c.IsProduction) - if err != nil { - return handler.API{}, err - } - traQAPI, err := external.NewTraQAPI(c.TraqConf(), !c.IsProduction) - if err != nil { - return handler.API{}, err - } - knoqAPI, err := external.NewKnoqAPI(c.KnoqConf(), !c.IsProduction) - if err != nil { - return handler.API{}, err + var ( + portalAPI external.PortalAPI + traQAPI external.TraQAPI + knoqAPI external.KnoqAPI + ) + + if c.IsProduction { + var err error + + portalAPI, err = external.NewPortalAPI(c.PortalConf()) + if err != nil { + return handler.API{}, err + } + + traQAPI, err = external.NewTraQAPI(c.TraqConf()) + if err != nil { + return handler.API{}, err + } + + knoqAPI, err = external.NewKnoqAPI(c.KnoqConf()) + if err != nil { + return handler.API{}, err + } + } else { + portalAPI = mock_external_e2e.NewMockPortalAPI() + traQAPI = mock_external_e2e.NewMockTraQAPI() + knoqAPI = mock_external_e2e.NewMockKnoqAPI() } // repository From 58e03eada1300f1b2fff3d95de7853ceabb27318 Mon Sep 17 00:00:00 2001 From: ras0q Date: Fri, 27 Oct 2023 15:51:03 +0900 Subject: [PATCH 3/3] :art: lint --- integration_tests/repository/user_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/integration_tests/repository/user_test.go b/integration_tests/repository/user_test.go index 40648872..cb023948 100644 --- a/integration_tests/repository/user_test.go +++ b/integration_tests/repository/user_test.go @@ -7,9 +7,9 @@ import ( "github.com/gofrs/uuid" "github.com/stretchr/testify/assert" "github.com/traPtitech/traPortfolio/domain" + "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external_e2e" irepository "github.com/traPtitech/traPortfolio/infrastructure/repository" "github.com/traPtitech/traPortfolio/integration_tests/testutils" - "github.com/traPtitech/traPortfolio/infrastructure/external/mock_external_e2e" urepository "github.com/traPtitech/traPortfolio/usecases/repository" "github.com/traPtitech/traPortfolio/util/mockdata"