<commit_before>package api

import (
	"encoding/json"
	"net/http"

	"github.com/anthonynsimon/parrot/model"
	"github.com/anthonynsimon/parrot/render"
	"github.com/pressly/chi"
)

func getUserProjects(w http.ResponseWriter, r *http.Request) {
	id, err := getUserID(r.Context())
	if err != nil {
		handleError(w, ErrBadRequest)
		return
	}

	projects, err := store.GetUserProjects(id)
	if err != nil {
		handleError(w, err)
		return
	}

	render.JSON(w, http.StatusOK, projects)
}

func getProjectUsers(w http.ResponseWriter, r *http.Request) {
	projectID := chi.URLParam(r, "projectID")
	if projectID == "" {
		handleError(w, ErrBadRequest)
		return
	}

	users, err := store.GetProjectUsers(projectID)
	if err != nil {
		handleError(w, err)
		return
	}

	render.JSON(w, http.StatusOK, users)
}

func assignProjectUser(w http.ResponseWriter, r *http.Request) {
	// TODO: decode and validate only required fields. Whitelisting?
	var pu model.ProjectUser
	if err := json.NewDecoder(r.Body).Decode(&pu); err != nil {
		handleError(w, ErrBadRequest)
		return
	}
	projectID := chi.URLParam(r, "projectID")
	if projectID == "" {
		handleError(w, ErrBadRequest)
		return
	}
	// Validate that the url of the request matches the body data
	if projectID != pu.ProjectID {
		handleError(w, ErrForbiden)
		return
	}
	// If neither email nor user id is provided, there's nothing we can do
	if pu.Email == "" && pu.UserID == "" {
		handleError(w, ErrBadRequest)
		return
	}

	// If email is provided, but no user id, find the user by email<commit_msg>Don't allow self role reassignment and remove from self from show project users<commit_after>package api

import (
	"encoding/json"
	"net/http"

	"github.com/anthonynsimon/parrot/model"
	"github.com/anthonynsimon/parrot/render"
	"github.com/pressly/chi"
)

func getUserProjects(w http.ResponseWriter, r *http.Request) {
	id, err := getUserID(r.Context())
	if err != nil {
		handleError(w, ErrBadRequest)
		return
	}

	projects, err := store.GetUserProjects(id)
	if err != nil {
		handleError(w, err)
		return
	}

	render.JSON(w, http.StatusOK, projects)
}

func getProjectUsers(w http.ResponseWriter, r *http.Request) {
	projectID := chi.URLParam(r, "projectID")
	if projectID == "" {
		handleError(w, ErrBadRequest)
		return
	}

	projectUsers, err := store.GetProjectUsers(projectID)
	if err != nil {
		handleError(w, err)
		return
	}

	// Remove self user from slice
	id, err := getUserID(r.Context())
	if err != nil {
		handleError(w, ErrBadRequest)
		return
	}

	result := make([]model.ProjectUser, 0)
	for _, pu := range projectUsers {
		if pu.UserID == id {
			continue
		}
		result = append(result, pu)
	}

	render.JSON(w, http.StatusOK, result)
}

func assignProjectUser(w http.ResponseWriter, r *http.Request) {
	projectID := chi.URLParam(r, "projectID")
	if projectID == "" {
		handleError(w, ErrBadRequest)
		return
	}

	// TODO: decode and validate only required fields. Whitelisting?
	var pu model.ProjectUser
	if err := json.NewDecoder(r.Body).Decode(&pu); err != nil {
		handleError(w, ErrBadRequest)
		return
	}

	// Don't allow self editing
	id, err := getUserID(r.Context())
	if err != nil {
		handleError(w, ErrBadRequest)
		return
	}
	if id == pu.UserID {
		handleError(w, ErrForbiden)
		return
	}

	// Validate that the url of the request matches the body data
	if projectID != pu.ProjectID {
		handleError(w, ErrForbiden)
		return
	}
	// If neither email nor user id is provided, there's nothing we can do
	if pu.Email == "" && pu.UserID == "" {
		handleError(w, ErrBadRequest)
		return
	}

	// If email is provided, but no user id, find the user by email
{'fraction_non_alphanumeric': 0.12738853503184713, 'fraction_numerical': 0.00027693159789531985, 'mean_word_length': 3.881081081081081, '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': '2183383', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1273, 'n_words': 338}