From 430c861692ee01b53bf128fc6560751c4ac866a8 Mon Sep 17 00:00:00 2001 From: Estelle Maudet Date: Wed, 14 Nov 2018 12:05:58 +0100 Subject: [PATCH] #16 Create _post_mapping route to send a new doctype description --- web/fulltext/fulltext.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/web/fulltext/fulltext.go b/web/fulltext/fulltext.go index 58f0dd0d107..88fe3458486 100644 --- a/web/fulltext/fulltext.go +++ b/web/fulltext/fulltext.go @@ -28,6 +28,7 @@ func Routes(router *echo.Group) { router.POST("/_delete_index", DeleteIndex) router.POST("/_delete_all_indexes", DeleteAllIndexes) router.POST("/_delete_index_query/:instance/:doctype/:lang", DeleteIndexQuery) + router.POST("/_post_mapping/:doctype", PostMapping) } func SearchQuery(c echo.Context) error { @@ -324,6 +325,45 @@ func DeleteIndexQuery(c echo.Context) error { return c.JSON(http.StatusOK, nil) } +func PostMapping(c echo.Context) error { + + docType := c.Param("doctype") + + tmpFile, err := ioutil.TempFile(indexation.MappingDescriptionPath, "store.tmp.") + if err != nil { + fmt.Println(err) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) + } + + _, err = io.Copy(tmpFile, c.Request().Body) + if err != nil { + fmt.Println(err) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) + } + + err = tmpFile.Close() + if err != nil { + fmt.Println(err) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) + } + + err = os.Rename(tmpFile.Name(), indexation.MappingDescriptionPath+docType+".json") + if err != nil { + fmt.Println(err) + return c.JSON(http.StatusInternalServerError, echo.Map{ + "error": err.Error(), + }) + } + + return c.JSON(http.StatusOK, nil) +} + func MakeRequest(mapJSONRequest map[string]interface{}) search.QueryRequest { request := search.QueryRequest{