@@ -2,30 +2,20 @@ package controller
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
- "log"
7
5
"net/http"
8
- "os"
9
6
"strconv"
10
- "strings"
11
- "sync"
12
7
13
- "github.com/hblab-ngocnd/get-started/models"
14
- "github.com/hblab-ngocnd/get-started/services"
8
+ "github.com/hblab-ngocnd/get-started/usecase"
15
9
"github.com/labstack/echo/v4"
16
10
)
17
11
18
12
type dictHandler struct {
19
- translateService services.TranslateService
20
- dictionaryService services.DictionaryService
21
- cacheData map [string ][]models.Word
22
- mu sync.Mutex
13
+ dictUseCase usecase.DictUseCase
23
14
}
24
15
25
16
func NewDictHandler () * dictHandler {
26
17
return & dictHandler {
27
- translateService : services .NewTranslate (),
28
- dictionaryService : services .NewDictionary (),
18
+ dictUseCase : usecase .NewDictUseCase (),
29
19
}
30
20
}
31
21
@@ -37,7 +27,6 @@ func (f *dictHandler) ApiDict(c echo.Context) error {
37
27
notCache := c .QueryParam ("not_cache" )
38
28
level := c .QueryParam ("level" )
39
29
start , err := strconv .Atoi (c .QueryParam ("start" ))
40
- var end int
41
30
if err != nil {
42
31
start = 0
43
32
}
@@ -53,45 +42,19 @@ func (f *dictHandler) ApiDict(c echo.Context) error {
53
42
default :
54
43
return c .NoContent (http .StatusBadRequest )
55
44
}
56
- if notCache != "true" && f .cacheData != nil && f .cacheData [level ] != nil {
57
- f .mu .Lock ()
58
- defer f .mu .Unlock ()
59
- if start > len (f .cacheData [level ]) {
60
- start = len (f .cacheData [level ])
61
- }
62
- end = start + pageSize
63
- if end > len (f .cacheData [level ]) {
64
- end = len (f .cacheData [level ])
65
- }
66
- return c .JSON (http .StatusOK , f .cacheData [level ][start :end ])
67
- }
68
- if notCache == "true" {
69
- pwd := c .QueryParam ("password" )
70
- if len (strings .TrimSpace (pwd )) == 0 || pwd != os .Getenv ("SYNC_PASS" ) {
71
- return c .NoContent (http .StatusBadRequest )
72
- }
73
- }
74
- f .mu .Lock ()
75
- defer f .mu .Unlock ()
45
+ pwd := c .QueryParam ("password" )
76
46
ctx := context .Background ()
77
- url := fmt .Sprintf ("https://japanesetest4you.com/jlpt-%s-vocabulary-list/" , level )
78
- data , err := f .dictionaryService .GetDictionary (ctx , url )
79
- if err != nil {
80
- log .Fatal (err )
81
- }
82
- data = f .translateService .TranslateData (ctx , data )
83
- if f .cacheData == nil {
84
- f .cacheData = make (map [string ][]models.Word )
85
- }
86
- f .cacheData [level ] = data
87
- if start > len (data ) {
88
- start = len (data )
89
- }
90
- end = start + pageSize
91
- if end > len (data ) {
92
- end = len (data )
47
+ data , err := f .dictUseCase .GetDict (ctx , start , pageSize , notCache , level , pwd )
48
+ switch err {
49
+ case nil :
50
+ case usecase .InvalidErr :
51
+ return c .NoContent (http .StatusBadRequest )
52
+ case usecase .PermissionDeniedErr :
53
+ return c .NoContent (http .StatusForbidden )
54
+ default :
55
+ return c .String (http .StatusInternalServerError , err .Error ())
93
56
}
94
- return c .JSON (http .StatusOK , data [ start : end ] )
57
+ return c .JSON (http .StatusOK , data )
95
58
}
96
59
97
60
func (f * dictHandler ) ApiGetDetail (c echo.Context ) error {
@@ -105,18 +68,17 @@ func (f *dictHandler) ApiGetDetail(c echo.Context) error {
105
68
default :
106
69
return c .NoContent (http .StatusBadRequest )
107
70
}
108
- if f .cacheData [level ] == nil && index >= len (f .cacheData [level ]) {
71
+ ctx := context .Background ()
72
+ data , err := f .dictUseCase .GetDetail (ctx , level , index )
73
+ switch err {
74
+ case nil :
75
+ case usecase .InvalidErr :
109
76
return c .NoContent (http .StatusBadRequest )
77
+ default :
78
+ return c .String (http .StatusInternalServerError , err .Error ())
110
79
}
111
- detailURL := f.cacheData [level ][index ].Link
112
- if strings .TrimSpace (detailURL ) == "" {
80
+ if data == nil {
113
81
return c .String (http .StatusOK , "" )
114
82
}
115
- ctx := context .Background ()
116
- data , err := f .dictionaryService .GetDetail (ctx , detailURL , index )
117
- if err != nil {
118
- return c .String (http .StatusInternalServerError , err .Error ())
119
- }
120
- f.cacheData [level ][index ].Detail = data
121
- return c .String (http .StatusOK , data )
83
+ return c .String (http .StatusOK , * data )
122
84
}
0 commit comments