<commit_before>package handlers

import (
	"database/sql"
	d "github.com/Lanciv/GoGradeAPI/database"
	m "github.com/Lanciv/GoGradeAPI/model"
	"github.com/gorilla/mux"
	"net/http"
	"strconv"
)

// PersonResponse will be used when returning People or a Person.
// type PersonAPI struct {
// 	Person         m.Person         `json:"person"`
// 	StudentProfile m.StudentProfile `json:"studentProfile,omitempty"`
// }

// CreatePerson allows you to create a Person.
func CreatePerson(w http.ResponseWriter, r *http.Request) {
	var p m.PersonProfile
	if readJson(r, &p) {
		// Create Person.

		err := d.CreatePerson(p.Person)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		if p.StudentProfile != nil {
			err = d.CreateStudentProfile(p.Person.Id, p.StudentProfile)
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}

	} else {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	writeJson(w, p)
}

// GetPerson will return a Person with all of their Profiles.
func GetPerson(w http.ResponseWriter, r *http.Request) {
	var res m.PersonProfile

	vars := mux.Vars(r)

	pID, ok := vars["id"]

	if !ok {
		http.Error(w, "error", http.StatusBadRequest)
	}

	id, err := strconv.Atoi(pID)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	res.Person, err = d.GetPerson(id)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	res.StudentProfile, err = d.StudentProfileForPerson(id)
	if err != nil && err != sql.ErrNoRows {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	writeJson(w, res)
}

// GetAllPeople returns all people without their profiles.
func GetAllPeople(w http.ResponseWriter, r *http.Request) {

	people, err := d.GetAllPeople()
	if err != nil {

		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	writeJson(w, people)
}<commit_msg>Removed commented code. Also fixed API to create a person with a StudentProfile.<commit_after>package handlers

import (
	"database/sql"
	d "github.com/Lanciv/GoGradeAPI/database"
	m "github.com/Lanciv/GoGradeAPI/model"
	"github.com/gorilla/mux"
	"net/http"
	"strconv"
)

// CreatePerson allows you to create a Person.
func CreatePerson(w http.ResponseWriter, r *http.Request) {
	var p m.PersonProfile

	if readJson(r, &p) {
		// Person should exist before trying to do anything.
		if p.Person == nil {
			writeError(w, "Person required", 400)
			return
		}

		err := d.CreatePerson(p.Person)
		if err != nil {
			writeError(w, "Error creating Person", 500)
			return
		}
		// If there is a StudentProfile then create it.
		if p.StudentProfile != nil {
			err = d.CreateStudentProfile(p.Person.Id, p.StudentProfile)
			if err != nil {
				writeError(w, "Error creating Student Profile", 500)
				return
			}
		}

	} else {
		writeError(w, "Error parsing JSON", 400)
		return
	}

	writeJson(w, p)
	return
}

// GetPerson will return a Person with all of their Profiles.
func GetPerson(w http.ResponseWriter, r *http.Request) {
	var res m.PersonProfile

	vars := mux.Vars(r)

	pID, ok := vars["id"]

	if !ok {
		writeError(w, "Invalid Person ID", 400)
		return
	}

	id, err := strconv.Atoi(pID)
	if err != nil {
		writeError(w, "Invalid Person ID", 400)
		return
	}

	res.Person, err = d.GetPerson(id)
	if err == sql.ErrNoRows {
		writeError(w, "Person not found", 404)
		return
	} else if err != nil {
		writeError(w, err.Error(), 400)
		return
	}
	res.StudentProfile, err = d.StudentProfileForPerson(id)
	if err != nil && err != sql.ErrNoRows {
		writeError(w, err.Error(), 500)
		return
	}

	writeJson(w, res)
	return
}

// GetAllPeople returns all people without their profiles.
func GetAllPeople(w http.ResponseWriter, r *http.Request) {

	people, err := d.GetAllPeople()
	if err != nil {

		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	writeJson(w, people)
	return
}
{'fraction_non_alphanumeric': 0.13194620654656178, 'fraction_numerical': 0.006851053032225323, 'mean_word_length': 3.5258323765786455, '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': '3781892', 'n_tokens_mistral': 1522, 'n_tokens_neox': 1388, 'n_words': 369}