Skip to content

Commit

Permalink
refactor: refactor getDictionary to use func in argument
Browse files Browse the repository at this point in the history
  • Loading branch information
hblab-annq committed Sep 6, 2022
1 parent a590297 commit 994f5ad
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 57 deletions.
67 changes: 36 additions & 31 deletions controller/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"context"
"github.com/hbl-ngocnd1/dictionary/models"
"net/http"
"strconv"

Expand All @@ -24,37 +25,7 @@ func (f *dictHandler) Dict(c echo.Context) error {
}

func (f *dictHandler) ApiDict(c echo.Context) error {
notCache := c.QueryParam("not_cache")
level := c.QueryParam("level")
start, err := strconv.Atoi(c.QueryParam("start"))
if err != nil {
start = 0
}
pageSize, err := strconv.Atoi(c.QueryParam("page_size"))
if err != nil {
pageSize = 20
}
if level == "" {
level = "n1"
}
switch level {
case "n1", "n2", "n3", "n4", "n5":
default:
return c.NoContent(http.StatusBadRequest)
}
pwd := c.QueryParam("password")
ctx := context.Background()
data, err := f.dictUseCase.GetDict(ctx, start, pageSize, notCache, level, pwd)
switch err {
case nil:
case usecase.InvalidErr:
return c.NoContent(http.StatusBadRequest)
case usecase.PermissionDeniedErr:
return c.NoContent(http.StatusForbidden)
default:
return c.String(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, data)
return getDataJapanese(f, c, models.MakeWord)
}

func (f *dictHandler) ApiGetDetail(c echo.Context) error {
Expand Down Expand Up @@ -102,3 +73,37 @@ func (f *dictHandler) ApiITJapanWonderWord(c echo.Context) error {
}
return c.JSON(http.StatusOK, data)
}

func getDataJapanese(f *dictHandler, c echo.Context, fn models.Fn) error {
notCache := c.QueryParam("not_cache")
level := c.QueryParam("level")
start, err := strconv.Atoi(c.QueryParam("start"))
if err != nil {
start = 0
}
pageSize, err := strconv.Atoi(c.QueryParam("page_size"))
if err != nil {
pageSize = 20
}
if level == "" {
level = "n1"
}
switch level {
case "n1", "n2", "n3", "n4", "n5":
default:
return c.NoContent(http.StatusBadRequest)
}
pwd := c.QueryParam("password")
ctx := context.Background()
data, err := f.dictUseCase.GetDict(ctx, start, pageSize, notCache, level, pwd, fn)
switch err {
case nil:
case usecase.InvalidErr:
return c.NoContent(http.StatusBadRequest)
case usecase.PermissionDeniedErr:
return c.NoContent(http.StatusForbidden)
default:
return c.String(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, data)
}
10 changes: 5 additions & 5 deletions controller/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestNewDictHandler(t *testing.T) {
statusCode: http.StatusOK,
newMockDictUC: func(ctrl *gomock.Controller) usecase.DictUseCase {
mock := mock_usecase.NewMockDictUseCase(ctrl)
mock.EXPECT().GetDict(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]models.Word{{}, {}, {}}, nil)
mock.EXPECT().GetDict(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]models.Word{{}, {}, {}}, nil)
return mock
},
},
Expand All @@ -54,7 +54,7 @@ func TestNewDictHandler(t *testing.T) {
urlParam: "",
newMockDictUC: func(ctrl *gomock.Controller) usecase.DictUseCase {
mock := mock_usecase.NewMockDictUseCase(ctrl)
mock.EXPECT().GetDict(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("unexpected"))
mock.EXPECT().GetDict(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errors.New("unexpected"))
return mock
},
statusCode: http.StatusInternalServerError,
Expand All @@ -65,7 +65,7 @@ func TestNewDictHandler(t *testing.T) {
statusCode: http.StatusBadRequest,
newMockDictUC: func(ctrl *gomock.Controller) usecase.DictUseCase {
mock := mock_usecase.NewMockDictUseCase(ctrl)
mock.EXPECT().GetDict(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]models.Word{{}, {}, {}}, usecase.InvalidErr)
mock.EXPECT().GetDict(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]models.Word{{}, {}, {}}, usecase.InvalidErr)
return mock
},
},
Expand All @@ -75,7 +75,7 @@ func TestNewDictHandler(t *testing.T) {
statusCode: http.StatusForbidden,
newMockDictUC: func(ctrl *gomock.Controller) usecase.DictUseCase {
mock := mock_usecase.NewMockDictUseCase(ctrl)
mock.EXPECT().GetDict(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]models.Word{{}, {}, {}}, usecase.PermissionDeniedErr)
mock.EXPECT().GetDict(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]models.Word{{}, {}, {}}, usecase.PermissionDeniedErr)
return mock
},
},
Expand All @@ -85,7 +85,7 @@ func TestNewDictHandler(t *testing.T) {
statusCode: http.StatusInternalServerError,
newMockDictUC: func(ctrl *gomock.Controller) usecase.DictUseCase {
mock := mock_usecase.NewMockDictUseCase(ctrl)
mock.EXPECT().GetDict(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]models.Word{{}, {}, {}}, errors.New("another Error"))
mock.EXPECT().GetDict(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return([]models.Word{{}, {}, {}}, errors.New("another Error"))
return mock
},
},
Expand Down
2 changes: 2 additions & 0 deletions models/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"golang.org/x/net/html"
)

type Fn func(c *html.Node, link, detail string, index int) *Word

type Word struct {
Index int `json:"index"`
Text string `json:"text"`
Expand Down
6 changes: 3 additions & 3 deletions services/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func NewDictionary() *dictionaryService {
}

type DictionaryService interface {
GetDictionary(context.Context, string) ([]models.Word, error)
GetDictionary(context.Context, string, models.Fn) ([]models.Word, error)
GetDetail(context.Context, string, int) (string, error)
GetITJapanWonderWork(context.Context, string) ([][]models.WonderWord, error)
}

func (d *dictionaryService) GetDictionary(ctx context.Context, url string) ([]models.Word, error) {
func (d *dictionaryService) GetDictionary(ctx context.Context, url string, fn models.Fn) ([]models.Word, error) {
ctx, cancel := context.WithTimeout(ctx, 50*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
Expand Down Expand Up @@ -102,7 +102,7 @@ func (d *dictionaryService) GetDictionary(ctx context.Context, url string) ([]mo
log.Println(errDetail)
}
}
w := models.MakeWord(child, detailURL, detail, id)
w := fn(child, detailURL, detail, id)
if w == nil {
return
}
Expand Down
3 changes: 2 additions & 1 deletion services/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"context"
"fmt"
"github.com/hbl-ngocnd1/dictionary/models"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -12,7 +13,7 @@ func TestDictionaryService_GetDictionary(t *testing.T) {
InitTest()
BucketSize = 2
ctx := context.Background()
_, err := NewDictionary().GetDictionary(ctx, "https://japanesetest4you.com/jlpt-n2-vocabulary-list/")
_, err := NewDictionary().GetDictionary(ctx, "https://japanesetest4you.com/jlpt-n2-vocabulary-list/", models.MakeWord)
assert.Equal(t, nil, err)
}

Expand Down
8 changes: 4 additions & 4 deletions services/mock_services/mock_dictionary.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions usecase/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ func NewDictUseCase() *dictUseCase {
}

type DictUseCase interface {
GetDict(context.Context, int, int, string, string, string) ([]models.Word, error)
GetDict(context.Context, int, int, string, string, string, models.Fn) ([]models.Word, error)
GetDetail(context.Context, string, int) (*string, error)
GetITJapanWonderWork(context.Context) ([][]models.WonderWord, error)
}

func (u *dictUseCase) GetDict(ctx context.Context, start, pageSize int, notCache, level, pwd string) ([]models.Word, error) {
func (u *dictUseCase) GetDict(ctx context.Context, start, pageSize int, notCache, level, pwd string, fn models.Fn) ([]models.Word, error) {
if notCache != "true" && u.cacheData != nil && u.cacheData[level] != nil && len(u.cacheData[level]) > 0 {
log.Println("use data from cache")
if start > len(u.cacheData[level]) {
Expand All @@ -56,7 +56,7 @@ func (u *dictUseCase) GetDict(ctx context.Context, start, pageSize int, notCache
}
log.Println("use data from source")
url := fmt.Sprintf("https://japanesetest4you.com/jlpt-%s-vocabulary-list/", level)
data, err := u.dictionaryService.GetDictionary(ctx, url)
data, err := u.dictionaryService.GetDictionary(ctx, url, fn)
if err != nil {
log.Print(err)
return nil, err
Expand Down
12 changes: 6 additions & 6 deletions usecase/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func Test_GetDict(t *testing.T) {
pwd: "sync_pass",
newMockDictService: func(ctrl *gomock.Controller) services.DictionaryService {
mock := mock_services.NewMockDictionaryService(ctrl)
mock.EXPECT().GetDictionary(gomock.Any(), gomock.Eq("https://japanesetest4you.com/jlpt-n1-vocabulary-list/")).Return([]models.Word{
mock.EXPECT().GetDictionary(gomock.Any(), gomock.Eq("https://japanesetest4you.com/jlpt-n1-vocabulary-list/"), gomock.Any()).Return([]models.Word{
{}, {}, {}, {},
}, nil)
return mock
Expand All @@ -61,7 +61,7 @@ func Test_GetDict(t *testing.T) {
pwd: "sync_pass",
newMockDictService: func(ctrl *gomock.Controller) services.DictionaryService {
mock := mock_services.NewMockDictionaryService(ctrl)
mock.EXPECT().GetDictionary(gomock.Any(), gomock.Eq("https://japanesetest4you.com/jlpt-n1-vocabulary-list/")).Return([]models.Word{
mock.EXPECT().GetDictionary(gomock.Any(), gomock.Eq("https://japanesetest4you.com/jlpt-n1-vocabulary-list/"), gomock.Any()).Return([]models.Word{
{}, {}, {}, {},
}, nil)
return mock
Expand All @@ -87,7 +87,7 @@ func Test_GetDict(t *testing.T) {
pwd: "sync_pass",
newMockDictService: func(ctrl *gomock.Controller) services.DictionaryService {
mock := mock_services.NewMockDictionaryService(ctrl)
mock.EXPECT().GetDictionary(gomock.Any(), gomock.Eq("https://japanesetest4you.com/jlpt-n1-vocabulary-list/")).Return([]models.Word{
mock.EXPECT().GetDictionary(gomock.Any(), gomock.Eq("https://japanesetest4you.com/jlpt-n1-vocabulary-list/"), gomock.Any()).Return([]models.Word{
{},
}, nil)
return mock
Expand Down Expand Up @@ -121,7 +121,7 @@ func Test_GetDict(t *testing.T) {
pwd: "sync_pass",
newMockDictService: func(ctrl *gomock.Controller) services.DictionaryService {
mock := mock_services.NewMockDictionaryService(ctrl)
mock.EXPECT().GetDictionary(gomock.Any(), gomock.Eq("https://japanesetest4you.com/jlpt-n1-vocabulary-list/")).Return([]models.Word{
mock.EXPECT().GetDictionary(gomock.Any(), gomock.Eq("https://japanesetest4you.com/jlpt-n1-vocabulary-list/"), gomock.Any()).Return([]models.Word{
{}, {}, {}, {},
}, nil)
return mock
Expand Down Expand Up @@ -175,7 +175,7 @@ func Test_GetDict(t *testing.T) {
pwd: "sync_pass",
newMockDictService: func(ctrl *gomock.Controller) services.DictionaryService {
mock := mock_services.NewMockDictionaryService(ctrl)
mock.EXPECT().GetDictionary(gomock.Any(), gomock.Eq("https://japanesetest4you.com/jlpt-n1-vocabulary-list/")).Return([]models.Word{
mock.EXPECT().GetDictionary(gomock.Any(), gomock.Eq("https://japanesetest4you.com/jlpt-n1-vocabulary-list/"), gomock.Any()).Return([]models.Word{
{}, {}, {}, {},
}, InvalidErr)
return mock
Expand Down Expand Up @@ -225,7 +225,7 @@ func Test_GetDict(t *testing.T) {
}
ctx := context.Background()
os.Setenv("SYNC_PASS", "sync_pass")
actual, err := uc.GetDict(ctx, p.start, p.pageSize, p.notCache, p.level, p.pwd)
actual, err := uc.GetDict(ctx, p.start, p.pageSize, p.notCache, p.level, p.pwd, models.MakeWord)
assert.Equal(t, p.expect, actual)
assert.Equal(t, p.err, err)
})
Expand Down
8 changes: 4 additions & 4 deletions usecase/mock_usecase/mock_dictionary.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 994f5ad

Please sign in to comment.