<commit_before>package api

import (
	"database/sql"
	"net/http"
	"strconv"

	"github.com/danjac/kanban/models"
	"github.com/gin-gonic/gin"
)

func addTaskList(c *gin.Context) {

	list := &models.TaskList{}

	if err := c.Bind(list); err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	if err := getDB(c).TaskLists.Create(list); err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	c.JSON(http.StatusOK, list)

}

func getTaskLists(c *gin.Context) {
	taskLists, err := getDB(c).TaskLists.Get()
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}
	c.JSON(http.StatusOK, gin.H{"lists": taskLists})
}

func deleteTaskList(c *gin.Context) {

	listID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}
	if err := getDB(c).TaskLists.Delete(listID); err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	c.String(http.StatusOK, statusOK)
}

/*
MoveHandler changes the position of a task list
*/
func moveTaskList(c *gin.Context) {
	listID, err := strconv.Atoi(c.Param("id"))

	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	targetListID, err := strconv.Atoi(c.Param("target_list_id"))

	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	if err := getDB(c).TaskLists.Move(listID, targetListID); err != nil {
		if err == sql.ErrNoRows {
			c.AbortWithStatus(http.StatusNotFound)
		} else {
			c.AbortWithError(http.StatusInternalServerError, err)
		}
		return
	}

	c.String(http.StatusOK, statusOK)

}

/*
UpdateHandler changes the details of a task list
*/
func updateTaskList(c *gin.Context) {

	listID, err := strconv.Atoi(c.Param("id"))

	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	s := &struct {
		Name string `json:"name" binding:"required,max=60"`
	}{}

	if err := c.Bind(s); err != nil {
		return
	}<commit_msg>Correct binding<commit_after>package api

import (
	"database/sql"
	"net/http"
	"strconv"

	"github.com/danjac/kanban/models"
	"github.com/gin-gonic/gin"
)

func addTaskList(c *gin.Context) {

	list := &models.TaskList{}

	if err := c.Bind(list); err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	if err := getDB(c).TaskLists.Create(list); err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	c.JSON(http.StatusOK, list)

}

func getTaskLists(c *gin.Context) {
	taskLists, err := getDB(c).TaskLists.Get()
	if err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}
	c.JSON(http.StatusOK, gin.H{"lists": taskLists})
}

func deleteTaskList(c *gin.Context) {

	listID, err := strconv.Atoi(c.Param("id"))
	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}
	if err := getDB(c).TaskLists.Delete(listID); err != nil {
		c.AbortWithError(http.StatusInternalServerError, err)
		return
	}

	c.String(http.StatusOK, statusOK)
}

/*
MoveHandler changes the position of a task list
*/
func moveTaskList(c *gin.Context) {
	listID, err := strconv.Atoi(c.Param("id"))

	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	targetListID, err := strconv.Atoi(c.Param("target_list_id"))

	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	if err := getDB(c).TaskLists.Move(listID, targetListID); err != nil {
		if err == sql.ErrNoRows {
			c.AbortWithStatus(http.StatusNotFound)
		} else {
			c.AbortWithError(http.StatusInternalServerError, err)
		}
		return
	}

	c.String(http.StatusOK, statusOK)

}

/*
UpdateHandler changes the details of a task list
*/
func updateTaskList(c *gin.Context) {

	listID, err := strconv.Atoi(c.Param("id"))

	if err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}

	s := &struct {
		Name string `json:"name" binding:"required,max=60"`
	}{}

	if err := c.Bind(s); err != nil {
		c.AbortWithError(http.StatusBadRequest, err)
		return
	}
{'fraction_non_alphanumeric': 0.15128782195548887, 'fraction_numerical': 0.001000250062515629, 'mean_word_length': 4.115089514066496, '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': '2526908', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1533, 'n_words': 293}