Skip to content

Commit

Permalink
added getCategoriesOfOther
Browse files Browse the repository at this point in the history
  • Loading branch information
TheFireMike committed Apr 7, 2020
1 parent 58d7e81 commit ee3359e
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions cve-alert-restapi/request-handler/requestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,16 @@ func StartWebServer(database *sqlx.DB, port string) {
// 404: description: Returns a string that there are no categories in the DB.
e.GET("/category", getAllCategories)

// swagger:route GET /category/other category getCategoriesOfOther
// ---
// Lists all categories which aren't in a filter.
// produces:
// - application/json
// responses:
// 200: description: Returns all categories as an array.
// 404: description: Returns a string that there are no categories which aren't in a filter.
e.GET("/category/other", getCategoriesOfOther)

e.Logger.Fatal(e.Start(":" + port))
}

Expand Down Expand Up @@ -823,3 +833,28 @@ func getAllCategories(c echo.Context) error {
}
return c.JSON(http.StatusOK, categoryList)
}

func getCategoriesOfOther(c echo.Context) error {
var categories []alertmanager.Category
sb := sqlbuilder.MySQL.NewSelectBuilder()
sb.Select("category").From("case_categories").Where("case_categories.category NOT IN (SELECT category FROM filter_categories)").GroupBy("category")
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(&categories, query)
if err != nil {
log.Error().
AnErr("Error", err).
Msg("Could not search db")
return err
}
if categories == nil {
return c.String(http.StatusNotFound, "No categories found!\n")
}
return c.JSON(http.StatusOK, categories)
}

0 comments on commit ee3359e

Please sign in to comment.