From 08610167617ee32f2eb6d008d98627848eb655c3 Mon Sep 17 00:00:00 2001 From: Daaaai0809 Date: Sat, 30 Mar 2024 21:42:12 +0900 Subject: [PATCH] refactor: remove getChannelByName --- .../domain/repository/channel_repository.go | 1 - server/infra/mysql/channel_repository.go | 10 ----- server/usecase/channel_interactor.go | 18 -------- server/usecase/channel_interactor_test.go | 43 ------------------- 4 files changed, 72 deletions(-) diff --git a/server/domain/repository/channel_repository.go b/server/domain/repository/channel_repository.go index 0f372be..284438d 100644 --- a/server/domain/repository/channel_repository.go +++ b/server/domain/repository/channel_repository.go @@ -8,7 +8,6 @@ import ( type IChannelRepository interface { FindByID(ctx context.Context, id uint64) (*model.Channel, error) - FindByName(ctx context.Context, name string) ([]*model.Channel, error) Create(ctx context.Context, channel *model.Channel) (uint64, error) Update(ctx context.Context, channel *model.Channel) error Delete(ctx context.Context, id uint64) error diff --git a/server/infra/mysql/channel_repository.go b/server/infra/mysql/channel_repository.go index 606ea4d..1e74db8 100644 --- a/server/infra/mysql/channel_repository.go +++ b/server/infra/mysql/channel_repository.go @@ -30,16 +30,6 @@ func (r *ChannelRepository) FindByID(ctx context.Context, id uint64) (*model.Cha return &channel, nil } -func (r *ChannelRepository) FindByName(ctx context.Context, name string) ([]*model.Channel, error) { - var channels []*model.Channel - - if err := r.db.NewSelect().Model(&channels).Where("name LIKE ?", "%"+name+"%").Relation("Users").Scan(ctx); err != nil { - return nil, err - } - - return channels, nil -} - func (r *ChannelRepository) Create(ctx context.Context, channel *model.Channel) (uint64, error) { r.mu.Lock() defer r.mu.Unlock() diff --git a/server/usecase/channel_interactor.go b/server/usecase/channel_interactor.go index b27ee17..6bdc815 100644 --- a/server/usecase/channel_interactor.go +++ b/server/usecase/channel_interactor.go @@ -68,24 +68,6 @@ func (i *ChannelInteractor) GetChannelByID(ctx context.Context, id uint64) (*pre return i.channelPresenter.GenerateGetChannelByIdResponse(channel.ToChannelEntity()), nil } -func (i *ChannelInteractor) GetChannelsByName(ctx context.Context, name string) (*presenter.GetChannelsByNameResponse, error) { - channels, err := i.channelRepository.FindByName(ctx, name) - if err != nil { - if err == sql.ErrNoRows { - return i.channelPresenter.GenerateGetChannelsByNameResponse([]*entity.Channel{}), nil - } - - return i.channelPresenter.GenerateGetChannelsByNameResponse([]*entity.Channel{}), err - } - - entitiedChannels := make([]*entity.Channel, len(channels)) - for i, channel := range channels { - entitiedChannels[i] = channel.ToChannelEntity() - } - - return i.channelPresenter.GenerateGetChannelsByNameResponse(entitiedChannels), nil -} - func (i *ChannelInteractor) CreateChannel(ctx context.Context, name string, userId uint64) error { id, err := i.channelRepository.Create(ctx, &model.Channel{Name: name}) if err != nil { diff --git a/server/usecase/channel_interactor_test.go b/server/usecase/channel_interactor_test.go index c7369f2..6d0f05f 100644 --- a/server/usecase/channel_interactor_test.go +++ b/server/usecase/channel_interactor_test.go @@ -116,49 +116,6 @@ func TestChannelInteractor_Failed_GetChannelByID(t *testing.T) { assert.Equal(t, expectedChannel, result) } -func TestChannelInteractor_Success_GetChannelsByName(t *testing.T) { - ctx := context.Background() - repo := &mockChannelRepository{} - repoChannelUser := &mockChannelUsersRepository{} - repoChannelToChannels := &mockChannelToChannelsRepository{} - repoUser := &mockUserRepository{} - pre := &mockChannelPresenter{} - - interactor := usecase.NewChannelInteractor(repo, repoChannelUser, repoChannelToChannels, repoUser, pre) - - expectedChannels := []*presenter.Channel{ - { - ID: 1, - Name: "test-channel", - }, - } - - result, err := interactor.GetChannelsByName(ctx, "test-channel") - assert.NoError(t, err) - assert.Equal(t, expectedChannels, result.Channels) -} - -func TestChannelInteractor_Failed_GetChannelsByName(t *testing.T) { - ctx := context.Background() - repo := &mockChannelRepository{} - repoChannelUser := &mockChannelUsersRepository{} - repoChannelToChannels := &mockChannelToChannelsRepository{} - repoUser := &mockUserRepository{} - pre := &mockChannelPresenter{} - - ctx = context.WithValue(ctx, FindByNameFailedValue, true) - - interactor := usecase.NewChannelInteractor(repo, repoChannelUser, repoChannelToChannels, repoUser, pre) - - expectedChannels := &presenter.GetChannelsByNameResponse{ - Channels: []*presenter.Channel{}, - } - - result, err := interactor.GetChannelsByName(ctx, "test-channel") - assert.Error(t, err) - assert.Equal(t, expectedChannels, result) -} - func TestChannelInteractor_Success_CreateChannel(t *testing.T) { ctx := context.Background() repo := &mockChannelRepository{}