Skip to content

Commit

Permalink
Add authenticated endpoint for deleting a word varnamproject#21
Browse files Browse the repository at this point in the history
  • Loading branch information
subins2000 committed Sep 27, 2020
1 parent e65b78b commit 8b42528
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
23 changes: 23 additions & 0 deletions http_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,29 @@ func handleTrainBulk(c echo.Context) error {
return c.JSON(200, "Words Trained")
}

// Delete a word
func handleDelete(c echo.Context) error {
var (
a args

app = c.Get("app").(*App)
)

c.Request().Header.Set("Content-Type", "application/json")

if err := c.Bind(&a); err != nil {
app.log.Printf("error in binding request details for delete, err: %s", err.Error())
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("error getting metadata. message: %s", err.Error()))
}

_, err := deleteWord(a.LangCode, a.Text)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("error: %s", err.Error()))
}

return c.JSON(http.StatusOK, "success")
}

func toggleDownloadEnabledStatus(langCode string, status bool) (interface{}, error) {
if err := varnamdConfig.setDownloadStatus(langCode, status); err != nil {
return nil, err
Expand Down
13 changes: 13 additions & 0 deletions libvarnam/libvarnam.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ func (v *Varnam) Learn(text string) error {
return nil
}

// Learn from given input text.
func (v *Varnam) DeleteWord(text string) error {
rc := C.varnam_delete_word(v.handle, C.CString(text))

if rc != 0 {
errorCode := (int)(rc)

return &VarnamError{errorCode: errorCode, message: v.getVarnamError(errorCode)}
}

return nil
}

func (v *Varnam) getVarnamError(errorCode int) string {
errormessage := C.varnam_get_last_error(v.handle)
varnamErrorMsg := C.GoString(errormessage)
Expand Down
4 changes: 3 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@ func initHandlers(app *App, enableInternalApis bool) *echo.Echo {
e.GET("/rtl/:langCode/:word", handleReverseTransliteration)
e.GET("/meta/:langCode:", handleMetadata)
e.GET("/download/:langCode/:downloadStart", handleDownload)
e.POST("/learn", handleLearn)
e.GET("/languages", handleLanguages)
e.GET("/languages/:langCode/download", handleLanguageDownload)
e.GET("/status", handleStatus)

e.POST("/learn", handleLearn)
e.POST("/train/:langCode", authUser(handleTrain))
e.POST("/train/bulk/:langCode", authUser(handleTrainBulk))
e.POST("/delete", authUser(handleDelete))

e.GET("/", handleIndex)

Expand Down
6 changes: 6 additions & 0 deletions varnam_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,9 @@ func getSchemeFilePath(schemeIdentifier string) (interface{}, error) {
return handle.GetSchemeFilePath(), nil
})
}

func deleteWord(schemeIdentifier string, word string) (interface{}, error) {
return getOrCreateHandler(schemeIdentifier, func(handle *libvarnam.Varnam) (data interface{}, err error) {
return nil, handle.DeleteWord(word)
})
}

0 comments on commit 8b42528

Please sign in to comment.