<commit_before>package handlers

import (
	"github.com/gin-gonic/gin"
	"github.com/nunows/todo-api/go/models"
	"strconv"
)

type TodoHandler struct {
	Db models.TodoDb
}

func (th *TodoHandler) GetAll(c *gin.Context) {
	c.JSON(200, th.Db.GetAll())
}

func (th *TodoHandler) Get(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	todo, err := th.Db.Get(id)

	if err == nil {
		c.JSON(200, todo)
	} else {
		c.JSON(404, models.Status{Success: false, Msg: "Error: Todo not found."})
	}
}

func (th *TodoHandler) Create(c *gin.Context) {
	var todo models.Todo

	if c.BindJSON(&todo) == nil {
		th.Db.Insert(&todo)
		c.JSON(200, models.Status{Success: true, Msg: "Todo created."})
	} else {
		c.JSON(500, models.Status{Success: false, Msg: "Error: Missing required fields."})
	}
}

func (th *TodoHandler) Update(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var todo models.Todo

	if c.BindJSON(&todo) == nil {
		if th.Db.Update(id, &todo) {
			c.JSON(200, models.Status{Success: true, Msg: "Todo updated."})
		} else {
			c.JSON(500, models.Status{Success: false, Msg: "Error: Updating Todo."})
		}
	} else {
		c.JSON(500, models.Status{Success: false, Msg: "Error: Missing required fields."})
	}
}

func (th *TodoHandler) Delete(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))

	if th.Db.Delete(id) {
		c.JSON(200, models.Status{Success: true, Msg: "Todo updated."})
	} else {
		c.JSON(404, models.Status{Success: false, Msg: "Error: Todo not found."})
	}
}
<commit_msg>Minor fix on todo handler.<commit_after>package handlers

import (
	"github.com/gin-gonic/gin"
	"github.com/nunows/todo-api/go/models"
	"strconv"
)

type TodoHandler struct {
	Db models.TodoDb
}

func (th *TodoHandler) GetAll(c *gin.Context) {
	todos := th.Db.GetAll()
	if len(todos) > 0 {
		c.JSON(200, todos)
	} else {
		c.JSON(404, models.Status{Success: false, Msg: "Error: Empty."})
	}
}

func (th *TodoHandler) Get(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	todo, err := th.Db.Get(id)

	if err == nil {
		c.JSON(200, todo)
	} else {
		c.JSON(404, models.Status{Success: false, Msg: "Error: Todo not found."})
	}
}

func (th *TodoHandler) Create(c *gin.Context) {
	var todo models.Todo

	if c.BindJSON(&todo) == nil {
		th.Db.Insert(&todo)
		c.JSON(200, models.Status{Success: true, Msg: "Todo created."})
	} else {
		c.JSON(500, models.Status{Success: false, Msg: "Error: Missing required fields."})
	}
}

func (th *TodoHandler) Update(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var todo models.Todo

	if c.BindJSON(&todo) == nil {
		if th.Db.Update(id, &todo) {
			c.JSON(200, models.Status{Success: true, Msg: "Todo updated."})
		} else {
			c.JSON(500, models.Status{Success: false, Msg: "Error: Updating Todo."})
		}
	} else {
		c.JSON(500, models.Status{Success: false, Msg: "Error: Missing required fields."})
	}
}

func (th *TodoHandler) Delete(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))

	if th.Db.Delete(id) {
		c.JSON(200, models.Status{Success: true, Msg: "Todo updated."})
	} else {
		c.JSON(404, models.Status{Success: false, Msg: "Error: Todo not found."})
	}
}

{'fraction_non_alphanumeric': 0.18812199036918137, 'fraction_numerical': 0.020545746388443017, 'mean_word_length': 3.914826498422713, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '1159138', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1300, 'n_words': 280}