-
Notifications
You must be signed in to change notification settings - Fork 0
/
tatoeba_handler.go
130 lines (108 loc) · 3.75 KB
/
tatoeba_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package controller
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
"github.com/kujilabo/cocotola/cocotola-api/src/plugin/common/controller/converter"
"github.com/kujilabo/cocotola/cocotola-api/src/plugin/common/controller/entity"
"github.com/kujilabo/cocotola/cocotola-api/src/plugin/common/service"
controllerhelper "github.com/kujilabo/cocotola/cocotola-api/src/user/controller/helper"
userD "github.com/kujilabo/cocotola/cocotola-api/src/user/domain"
liberrors "github.com/kujilabo/cocotola/lib/errors"
"github.com/kujilabo/cocotola/lib/log"
)
type TatoebaHandler interface {
FindSentencePairs(c *gin.Context)
ImportSentences(c *gin.Context)
ImportLinks(c *gin.Context)
}
type tatoebaHandler struct {
tatoebaClient service.TatoebaClient
}
func NewTatoebaHandler(tatoebaClient service.TatoebaClient) TatoebaHandler {
return &tatoebaHandler{
tatoebaClient: tatoebaClient,
}
}
func (h *tatoebaHandler) FindSentencePairs(c *gin.Context) {
ctx := c.Request.Context()
logger := log.FromContext(ctx)
controllerhelper.HandleFunction(c, func(organizationID userD.OrganizationID, operatorID userD.AppUserID) error {
param := entity.TatoebaSentenceFindParameter{}
if err := c.ShouldBindJSON(¶m); err != nil {
logger.Warnf("err: %+v", err)
c.Status(http.StatusBadRequest)
return nil
}
parameter, err := converter.ToTatoebaSentenceSearchCondition(ctx, ¶m)
if err != nil {
return liberrors.Errorf("failed to ToTatoebaSentenceSearchCondition. err: %w", err)
}
result, err := h.tatoebaClient.FindSentencePairs(ctx, parameter)
if err != nil {
return liberrors.Errorf("failed to FindSentencePairs. err: %w", err)
}
response, err := converter.ToTatoebaSentenceResponse(ctx, result)
if err != nil {
return liberrors.Errorf("failed to ToTatoebaSentenceResponse. err: %w", err)
}
c.JSON(http.StatusOK, response)
return nil
}, h.errorHandle)
}
func (h *tatoebaHandler) ImportSentences(c *gin.Context) {
ctx := c.Request.Context()
logger := log.FromContext(ctx)
controllerhelper.HandleRoleFunction(c, "Owner", func(organizationID userD.OrganizationID, operatorID userD.AppUserID) error {
file, err := c.FormFile("file")
if err != nil {
if errors.Is(err, http.ErrMissingFile) {
logger.Warnf("err: %+v", err)
c.Status(http.StatusBadRequest)
return nil
}
return liberrors.Errorf("c.FormFile. err: %w", err)
}
multipartFile, err := file.Open()
if err != nil {
return liberrors.Errorf("failed to file.Open. err: %w", err)
}
defer multipartFile.Close()
if err := h.tatoebaClient.ImportSentences(ctx, multipartFile); err != nil {
return liberrors.Errorf("failed to ImportSentences. err: %w", err)
}
c.Status(http.StatusOK)
return nil
}, h.errorHandle)
}
func (h *tatoebaHandler) ImportLinks(c *gin.Context) {
ctx := c.Request.Context()
logger := log.FromContext(ctx)
controllerhelper.HandleRoleFunction(c, "Owner", func(organizationID userD.OrganizationID, operatorID userD.AppUserID) error {
file, err := c.FormFile("file")
if err != nil {
if errors.Is(err, http.ErrMissingFile) {
logger.Warnf("err: %+v", err)
c.Status(http.StatusBadRequest)
return nil
}
return liberrors.Errorf("c.FormFile. err: %w", err)
}
multipartFile, err := file.Open()
if err != nil {
return liberrors.Errorf("failed to file.Open. err: %w", err)
}
defer multipartFile.Close()
if err := h.tatoebaClient.ImportSentences(ctx, multipartFile); err != nil {
return liberrors.Errorf("h.tatoebaClient.ImportSentences. err: %w", err)
}
c.Status(http.StatusOK)
return nil
}, h.errorHandle)
}
func (h *tatoebaHandler) errorHandle(c *gin.Context, err error) bool {
ctx := c.Request.Context()
logger := log.FromContext(ctx)
logger.Errorf("tatoebaHandler. err: %v", err)
return false
}