From 58d7e8130813cf5f996abf2ecd13055ff74cba18 Mon Sep 17 00:00:00 2001 From: mikameyer Date: Tue, 7 Apr 2020 09:58:16 +0200 Subject: [PATCH] added getFilterCategoriesOfFilter --- .../request-handler/requestHandler.go | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/cve-alert-restapi/request-handler/requestHandler.go b/cve-alert-restapi/request-handler/requestHandler.go index bd3367c..ec38a8f 100644 --- a/cve-alert-restapi/request-handler/requestHandler.go +++ b/cve-alert-restapi/request-handler/requestHandler.go @@ -219,6 +219,28 @@ func StartWebServer(database *sqlx.DB, port string) { // description: Returns a string that the specified filter is not in the DB. e.GET("/filter/:id/cases", getFilterCases) + // swagger:operation GET /filter/{id}/categories filter getFilterCategoriesOfFilter + // --- + // summary: Gets all filter categories of a specific filter. + // produces: + // - application/json + // parameters: + // - name: id + // in: path + // description: ID of the filter. + // type: integer + // required: true + // responses: + // 200: + // description: Returns the filter categories. + // schema: + // type: array + // items: + // $ref: '#/definitions/FilterCategory' + // 404: + // description: Returns a string that there are no filter categories for the specified filter. + e.GET("/filter/:id/categories", getFilterCategoriesOfFilter) + // swagger:operation Post /filter/import filter importFilter // --- // summary: Imports filter from a file. @@ -602,6 +624,31 @@ func getFilterCases(c echo.Context) error { return c.JSON(http.StatusOK, cases) } +func getFilterCategoriesOfFilter(c echo.Context) error { + var filterCategories []alertmanager.FilterCategory + sb := sqlbuilder.MySQL.NewSelectBuilder() + sb.Select("*").From("filter_categories").Where(sb.Equal("filter_id", c.Param("id"))) + sql, args := sb.Build() + query, err := sqlbuilder.MySQL.Interpolate(sql, args) + if err != nil { + log.Error(). + AnErr("Error", err). + Msg("Could not build sql query") + return err + } + err = db.Select(&filterCategories, query) + if err != nil { + log.Error(). + AnErr("Error", err). + Msg("Could not search db") + return err + } + if filterCategories == nil { + return c.String(http.StatusNotFound, "No filter categories found!\n") + } + return c.JSON(http.StatusOK, filterCategories) +} + func importFilter(c echo.Context) error { requestFile, err := c.FormFile("filterConfig") if err != nil {