<commit_before>package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"

	"github.com/techjanitor/pram-libs/audit"
	"github.com/techjanitor/pram-libs/auth"
	e "github.com/techjanitor/pram-libs/errors"
	"github.com/techjanitor/pram-libs/perms"
	"github.com/techjanitor/pram-libs/redis"

	"github.com/techjanitor/pram-admin/models"
)

// update tag input
type updateTagForm struct {
	Id       uint   `json:"id" binding:"required"`
	Ib       uint   `json:"ib" binding:"required"`
	Tag      string `json:"name" binding:"required"`
	Type     uint   `json:"type" binding:"required"`
	Antispam string `json:"askey" binding:"required"`
}

// UpdateTagController will delete a tag
func UpdateTagController(c *gin.Context) {
	var err error
	var utf updateTagForm

	// Get parameters from validate middleware
	params := c.MustGet("params").([]uint)

	// get userdata from session middleware
	userdata := c.MustGet("userdata").(auth.User)

	err = c.Bind(&utf)
	if err != nil {
		c.JSON(e.ErrorMessage(e.ErrInvalidParam))
		c.Error(err)
		return
	}

	// Set parameters to UpdateTagModel
	m := models.UpdateTagModel{
		Id:      utf.Id,
		Ib:      ntf.Ib,
		Tag:     ntf.Tag,
		TagType: ntf.Type,
	}

	// Test for antispam key from Prim
	antispam := utf.Antispam
	if antispam != config.Settings.Antispam.AntispamKey {
		c.JSON(http.StatusBadRequest, gin.H{"error_message": e.ErrInvalidKey.Error()})
		c.Error(e.ErrInvalidKey)
		return
	}

	// check to see if user is allowed to perform action
	allowed, err := perms.Check(userdata.Id, m.Ib)
	if err != nil {
		c.JSON(e.ErrorMessage(e.ErrInternalError))
		c.Error(err)
		return
	}

	// if not allowed reject request
	if !allowed {
		c.JSON(e.ErrorMessage(e.ErrForbidden))
		c.Error(e.ErrForbidden)
		return
	}

	// Validate input parameters
	err = m.ValidateInput()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error_message": err.Error()})
		c.Error(err)<commit_msg>add update tag<commit_after>package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"

	"github.com/techjanitor/pram-libs/audit"
	"github.com/techjanitor/pram-libs/auth"
	"github.com/techjanitor/pram-libs/config"
	e "github.com/techjanitor/pram-libs/errors"
	"github.com/techjanitor/pram-libs/perms"
	"github.com/techjanitor/pram-libs/redis"

	"github.com/techjanitor/pram-admin/models"
)

// update tag input
type updateTagForm struct {
	Id       uint   `json:"id" binding:"required"`
	Ib       uint   `json:"ib" binding:"required"`
	Tag      string `json:"name" binding:"required"`
	Type     uint   `json:"type" binding:"required"`
	Antispam string `json:"askey" binding:"required"`
}

// UpdateTagController will delete a tag
func UpdateTagController(c *gin.Context) {
	var err error
	var utf updateTagForm

	// Get parameters from validate middleware
	params := c.MustGet("params").([]uint)

	// get userdata from session middleware
	userdata := c.MustGet("userdata").(auth.User)

	err = c.Bind(&utf)
	if err != nil {
		c.JSON(e.ErrorMessage(e.ErrInvalidParam))
		c.Error(err)
		return
	}

	// Set parameters to UpdateTagModel
	m := models.UpdateTagModel{
		Id:      utf.Id,
		Ib:      utf.Ib,
		Tag:     utf.Tag,
		TagType: utf.Type,
	}

	// Test for antispam key from Prim
	antispam := utf.Antispam
	if antispam != config.Settings.Antispam.AntispamKey {
		c.JSON(http.StatusBadRequest, gin.H{"error_message": e.ErrInvalidKey.Error()})
		c.Error(e.ErrInvalidKey)
		return
	}

	// check to see if user is allowed to perform action
	allowed, err := perms.Check(userdata.Id, m.Ib)
	if err != nil {
		c.JSON(e.ErrorMessage(e.ErrInternalError))
		c.Error(err)
		return
	}

	// if not allowed reject request
	if !allowed {
		c.JSON(e.ErrorMessage(e.ErrForbidden))
		c.Error(e.ErrForbidden)
		return
	}

	// Validate input parameters
	err = m.ValidateInput()
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error_message": err.Error()})
		c.Error(err)
{'fraction_non_alphanumeric': 0.140992835209826, 'fraction_numerical': 0.0, 'mean_word_length': 3.721014492753623, 'pattern_counts': {'":': 4, '<': 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': '2192733', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1438, 'n_words': 296}