<commit_before>package api

import (
	"fmt"
	"github.com/labstack/echo"
	"github.com/nerdzeu/nerdz-api/nerdz"
	"net/http"
	"strconv"
)

//UserPosts handles the request and returns all the posts written
//by the specified user
func UserPosts(c *echo.Context) error {
	var id uint64
	var e error
	if id, e = strconv.ParseUint(c.Param("id"), 10, 64); e != nil {
		return c.JSON(http.StatusBadRequest, &nerdz.Response{
			HumanMessage: "Invalid user identifier specified",
			Message:      e.Error(),
			Status:       http.StatusBadRequest,
			Success:      false,
		})
	}

	var user *nerdz.User
	if user, e = nerdz.NewUser(id); e != nil {
		return c.JSON(http.StatusBadRequest, &nerdz.Response{
			HumanMessage: "User does not exists",
			Message:      e.Error(),
			Status:       http.StatusBadRequest,
			Success:      false,
		})
	}

	var options *nerdz.PostlistOptions
	if options, e = NewPostlistOptions(c); e != nil {
		return c.JSON(http.StatusBadRequest, &nerdz.Response{
			HumanMessage: e.Error(),
			Message:      "NewPostlistOptions error",
			Status:       http.StatusBadRequest,
			Success:      false,
		})
	}

	posts := user.Postlist(options)

	if posts == nil {
		return c.JSON(http.StatusBadRequest, &nerdz.Response{
			HumanMessage: "Unable to fetch post list for the specified user",
			Message:      "user.Postlist error",
			Status:       http.StatusBadRequest,
			Success:      false,
		})
	}

	postsApi := make([]nerdz.UserPostTO, 0, 0)

	for _, p := range *posts {
		// posts contains ExistingPost elements
		// we need to convert back to a UserPost in order to
		// get a correct UserPostTO
		if userPost := p.(*nerdz.UserPost); userPost != nil {
			fmt.Printf("%+v\n", userPost)
			postsApi = append(postsApi, userPost.GetTO().(nerdz.UserPostTO))
		} else {
			fmt.Println("NIL")
		}
	}

	out, err := SelectFields(postsApi, c)
	if err == nil {
		return c.JSON(http.StatusOK, &nerdz.Response{
			Data:         out,
			HumanMessage: "Correctly fetched post list for the specified user",
			Message:      "user.Postlist ok",
			Status:       http.StatusOK,
			Success:      true,
		})
	}

	return c.JSON(http.StatusBadRequest, &nerdz.Response{
		HumanMessage: "Error selecting required fields",
		Message:      err.Error(),
		Status:       http.StatusBadRequest,
		Success:      false,
	})
<commit_msg>Completed decoupling of API's types and database's types<commit_after>package api

import (
	"fmt"
	"github.com/labstack/echo"
	"github.com/nerdzeu/nerdz-api/nerdz"
	"net/http"
	"strconv"
)

//UserPosts handles the request and returns all the posts written
//by the specified user
func UserPosts(c *echo.Context) error {
	var id uint64
	var e error
	if id, e = strconv.ParseUint(c.Param("id"), 10, 64); e != nil {
		return c.JSON(http.StatusBadRequest, &nerdz.Response{
			HumanMessage: "Invalid user identifier specified",
			Message:      e.Error(),
			Status:       http.StatusBadRequest,
			Success:      false,
		})
	}

	var user *nerdz.User
	if user, e = nerdz.NewUser(id); e != nil {
		return c.JSON(http.StatusBadRequest, &nerdz.Response{
			HumanMessage: "User does not exists",
			Message:      e.Error(),
			Status:       http.StatusBadRequest,
			Success:      false,
		})
	}

	var options *nerdz.PostlistOptions
	if options, e = NewPostlistOptions(c); e != nil {
		return c.JSON(http.StatusBadRequest, &nerdz.Response{
			HumanMessage: e.Error(),
			Message:      "NewPostlistOptions error",
			Status:       http.StatusBadRequest,
			Success:      false,
		})
	}

	posts := user.Postlist(options)

	if posts == nil {
		return c.JSON(http.StatusBadRequest, &nerdz.Response{
			HumanMessage: "Unable to fetch post list for the specified user",
			Message:      "user.Postlist error",
			Status:       http.StatusBadRequest,
			Success:      false,
		})
	}

	postsAPI := make([]nerdz.UserPostTO, 0, 0)

	for _, p := range *posts {
		// posts contains ExistingPost elements
		// we need to convert back to a UserPost in order to
		// get a correct UserPostTO
		if userPost := p.(*nerdz.UserPost); userPost != nil {
			postsAPI = append(postsAPI, userPost.GetTO().(nerdz.UserPostTO))
		}
	}

	out, err := SelectFields(postsAPI, c)
	if err == nil {
		return c.JSON(http.StatusOK, &nerdz.Response{
			Data:         out,
			HumanMessage: "Correctly fetched post list for the specified user",
			Message:      "user.Postlist ok",
			Status:       http.StatusOK,
			Success:      true,
		})
	}

	return c.JSON(http.StatusBadRequest, &nerdz.Response{
		HumanMessage: "Error selecting required fields",
		Message:      err.Error(),
		Status:       http.StatusBadRequest,
		Success:      false,
	})

{'fraction_non_alphanumeric': 0.12329656067488644, 'fraction_numerical': 0.0034609560891196195, 'mean_word_length': 3.1248884924174845, 'pattern_counts': {'":': 0, '<': 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': '3542618', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1528, 'n_words': 411}