<commit_before>package catalog

import (
	"fmt"
	"net/http"

	"github.com/estafette/estafette-ci-api/auth"
	"github.com/estafette/estafette-ci-api/clients/cockroachdb"
	"github.com/estafette/estafette-ci-api/config"
	"github.com/estafette/estafette-ci-api/helpers"
	contracts "github.com/estafette/estafette-ci-contracts"
	"github.com/gin-gonic/gin"
	"github.com/rs/zerolog/log"
)

// NewHandler returns a new rbac.Handler
func NewHandler(config *config.APIConfig, service Service, cockroachdbClient cockroachdb.Client) Handler {
	return Handler{
		config:            config,
		service:           service,
		cockroachdbClient: cockroachdbClient,
	}
}

type Handler struct {
	config            *config.APIConfig
	service           Service
	cockroachdbClient cockroachdb.Client
}

func (h *Handler) GetCatalogEntities(c *gin.Context) {

	// // ensure the request has the correct permission
	// if !auth.RequestTokenHasPermission(c, auth.PermissionCatalogEntitiesList) {
	// 	c.JSON(http.StatusForbidden, gin.H{"code": http.StatusText(http.StatusForbidden), "message": "JWT is invalid or request does not have correct permission"})
	// 	return
	// }

	pageNumber, pageSize, filters, sortings := helpers.GetQueryParameters(c)

	// ensure the request has the correct permission
	if !auth.RequestTokenHasPermission(c, auth.PermissionClientsList) {
		c.JSON(http.StatusForbidden, gin.H{"code": http.StatusText(http.StatusForbidden), "message": "JWT is invalid or request does not have correct permission"})
		return
	}

	ctx := c.Request.Context()

	response, err := helpers.GetPagedListResponse(
		func() ([]interface{}, error) {
			catalogEntities, err := h.cockroachdbClient.GetCatalogEntities(ctx, pageNumber, pageSize, filters, sortings)
			if err != nil {
				return nil, err
			}

			// convert typed array to interface array O(n)
			items := make([]interface{}, len(catalogEntities))
			for i := range catalogEntities {
				items[i] = catalogEntities[i]
			}

			return items, nil
		},
		func() (int, error) {
			return h.cockroachdbClient.GetCatalogEntitiesCount(ctx, filters)
		},
		pageNumber,
		pageSize)

	if err != nil {
		log.Error().Err(err).Msg("Failed retrieving catalog entities from db")
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusText(http.StatusInternalServerError)})<commit_msg>remove check for incorrect permission in GetCatalogEntities<commit_after>package catalog

import (
	"fmt"
	"net/http"

	"github.com/estafette/estafette-ci-api/auth"
	"github.com/estafette/estafette-ci-api/clients/cockroachdb"
	"github.com/estafette/estafette-ci-api/config"
	"github.com/estafette/estafette-ci-api/helpers"
	contracts "github.com/estafette/estafette-ci-contracts"
	"github.com/gin-gonic/gin"
	"github.com/rs/zerolog/log"
)

// NewHandler returns a new rbac.Handler
func NewHandler(config *config.APIConfig, service Service, cockroachdbClient cockroachdb.Client) Handler {
	return Handler{
		config:            config,
		service:           service,
		cockroachdbClient: cockroachdbClient,
	}
}

type Handler struct {
	config            *config.APIConfig
	service           Service
	cockroachdbClient cockroachdb.Client
}

func (h *Handler) GetCatalogEntities(c *gin.Context) {

	// // ensure the request has the correct permission
	// if !auth.RequestTokenHasPermission(c, auth.PermissionCatalogEntitiesList) {
	// 	c.JSON(http.StatusForbidden, gin.H{"code": http.StatusText(http.StatusForbidden), "message": "JWT is invalid or request does not have correct permission"})
	// 	return
	// }

	pageNumber, pageSize, filters, sortings := helpers.GetQueryParameters(c)

	ctx := c.Request.Context()

	response, err := helpers.GetPagedListResponse(
		func() ([]interface{}, error) {
			catalogEntities, err := h.cockroachdbClient.GetCatalogEntities(ctx, pageNumber, pageSize, filters, sortings)
			if err != nil {
				return nil, err
			}

			// convert typed array to interface array O(n)
			items := make([]interface{}, len(catalogEntities))
			for i := range catalogEntities {
				items[i] = catalogEntities[i]
			}

			return items, nil
		},
		func() (int, error) {
			return h.cockroachdbClient.GetCatalogEntitiesCount(ctx, filters)
		},
		pageNumber,
		pageSize)

	if err != nil {
		log.Error().Err(err).Msg("Failed retrieving catalog entities from db")
		c.JSON(http.StatusInternalServerError, gin.H{"code": http.StatusText(http.StatusInternalServerError)})
{'fraction_non_alphanumeric': 0.12186788154897495, 'fraction_numerical': 0.0, 'mean_word_length': 4.2398568019093075, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5083269', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1438, 'n_words': 306}