<commit_before>package api

import (
	"github.com/raintank/raintank-apps/task-server/model"
	"github.com/raintank/raintank-apps/task-server/sqlstore"
)

func GetAgents(ctx *Context, query model.GetAgentsQuery) {
	query.Owner = ctx.Owner
	agents, err := sqlstore.GetAgents(&query)
	if err != nil {
		log.Error(err)
		ctx.JSON(500, err)
		return
	}
	ctx.JSON(200, agents)
}

func GetAgentById(ctx *Context) {
	id := ctx.ParamsInt64(":id")
	owner := ctx.Owner
	agent, err := sqlstore.GetAgentById(id, owner)
	if err != nil {
		log.Error(err)
		ctx.JSON(500, err)
		return
	}
	if agent == nil {
		ctx.JSON(404, "agent not found")
		return
	}
	ctx.JSON(200, agent)
}

func AddAgent(ctx *Context, agent model.AgentDTO) {
	if !agent.ValidName() {
		ctx.JSON(400, "invalde agent Name. must match /^[0-9a-Z_-]+$/")
		return
	}
	//need to add suport for middelware context with AUTH/
	agent.Owner = ctx.Owner
	err := sqlstore.UpdateAgent(&agent)
	if err != nil {
		log.Error(err)
		ctx.JSON(500, err)
		return
	}
	ctx.JSON(200, agent)
}

func UpdateAgent(ctx *Context, agent model.AgentDTO) {
	if !agent.ValidName() {
		ctx.JSON(400, "invalde agent Name. must match /^[0-9a-Z_-]+$/")
		return
	}
	//need to add suport for middelware context with AUTH/
	agent.Owner = ctx.Owner
	err := sqlstore.UpdateAgent(&agent)
	if err != nil {<commit_msg>ensure agent creation cant be performed via the update api endpoint<commit_after>package api

import (
	"github.com/raintank/raintank-apps/task-server/model"
	"github.com/raintank/raintank-apps/task-server/sqlstore"
)

func GetAgents(ctx *Context, query model.GetAgentsQuery) {
	query.Owner = ctx.Owner
	agents, err := sqlstore.GetAgents(&query)
	if err != nil {
		log.Error(err)
		ctx.JSON(500, err)
		return
	}
	ctx.JSON(200, agents)
}

func GetAgentById(ctx *Context) {
	id := ctx.ParamsInt64(":id")
	owner := ctx.Owner
	agent, err := sqlstore.GetAgentById(id, owner)
	if err != nil {
		log.Error(err)
		ctx.JSON(500, err)
		return
	}
	if agent == nil {
		ctx.JSON(404, "agent not found")
		return
	}
	ctx.JSON(200, agent)
}

func AddAgent(ctx *Context, agent model.AgentDTO) {
	if !agent.ValidName() {
		ctx.JSON(400, "invalde agent Name. must match /^[0-9a-Z_-]+$/")
		return
	}
	agent.Id = 0
	//need to add suport for middelware context with AUTH/
	agent.Owner = ctx.Owner
	err := sqlstore.UpdateAgent(&agent)
	if err != nil {
		log.Error(err)
		ctx.JSON(500, err)
		return
	}
	ctx.JSON(200, agent)
}

func UpdateAgent(ctx *Context, agent model.AgentDTO) {
	if !agent.ValidName() {
		ctx.JSON(400, "invalde agent Name. must match /^[0-9a-Z_-]+$/")
		return
	}
	if agent.Id == 0 {
		ctx.JSON(400, "agent ID not set.")
		return
	}
	//need to add suport for middelware context with AUTH/
	agent.Owner = ctx.Owner
	err := sqlstore.UpdateAgent(&agent)
	if err != nil {
{'fraction_non_alphanumeric': 0.15066047840057123, 'fraction_numerical': 0.02534808996786862, 'mean_word_length': 3.693467336683417, 'pattern_counts': {'":': 2, '<': 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': '7187636', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1078, 'n_words': 261}