<commit_before>package api

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

	"github.com/backstage/backstage/account"
	"github.com/backstage/backstage/errors"
	"github.com/gorilla/mux"
)

func teamCreate(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	team := account.Team{}
	if err := json.NewDecoder(r.Body).Decode(&team); err != nil {
		handleError(rw, errors.ErrBadRequest)
		return
	}

	if err := team.Create(*user); err != nil {
		handleError(rw, err)
		return
	}

	Created(rw, team)
}

func teamList(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	teams, _ := user.Teams()
	Ok(rw, CollectionSerializer{Items: teams, Count: len(teams)})
}

func teamDelete(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	team, err := account.FindTeamByAlias(mux.Vars(r)["alias"])
	if err != nil {
		handleError(rw, err)
		return
	}

	if err = team.Delete(*user); err != nil {
		handleError(rw, err)
		return
	}

	Ok(rw, team)
}

func teamInfo(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	team, err := account.FindTeamByAlias(mux.Vars(r)["alias"])
	if err != nil {
		handleError(rw, err)
		return
	}
	if _, err := team.ContainsUser(user); err != nil {
		handleError(rw, err)
		return
	}

	Ok(rw, team)
}

func teamAddUsers(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	team, err := account.FindTeamByAlias(mux.Vars(r)["alias"])
	if err != nil {
		handleError(rw, err)
		return
	}
	if _, err := team.ContainsUser(user); err != nil {
		handleError(rw, err)
		return
	}

	var t *account.Team
	if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
		handleError(rw, errors.ErrBadRequest)
		return
	}

	err = team.AddUsers(t.Users)
	if err != nil {
		handleError(rw, err)
		return
	}

	Ok(rw, team)
}

func teamRemoveUsers(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	team, err := account.FindTeamByAlias(mux.Vars(r)["alias"])
	if err != nil {
		handleError(rw, err)
		return
	}
	if _, err := team.ContainsUser(user); err != nil {
		handleError(rw, err)
		return
	}

	var t *account.Team
	if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
		handleError(rw, errors.ErrBadRequest)
		return
	}

	err = team.RemoveUsers(t.Users)
	if err != nil {
		handleError(rw, err)
		return
	}

	Ok(rw, team)
}
<commit_msg>Extract similar code to a func.<commit_after>package api

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

	"github.com/backstage/backstage/account"
	"github.com/backstage/backstage/errors"
	"github.com/gorilla/mux"
)

func teamCreate(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	team := account.Team{}
	if err := json.NewDecoder(r.Body).Decode(&team); err != nil {
		handleError(rw, errors.ErrBadRequest)
		return
	}

	if err := team.Create(*user); err != nil {
		handleError(rw, err)
		return
	}

	Created(rw, team)
}

func teamList(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	teams, _ := user.Teams()
	Ok(rw, CollectionSerializer{Items: teams, Count: len(teams)})
}

func teamDelete(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	team, err := account.FindTeamByAlias(mux.Vars(r)["alias"])
	if err != nil {
		handleError(rw, err)
		return
	}

	if err = team.Delete(*user); err != nil {
		handleError(rw, err)
		return
	}

	Ok(rw, team)
}

func teamInfo(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	team, err := account.FindTeamByAlias(mux.Vars(r)["alias"])
	if err != nil {
		handleError(rw, err)
		return
	}
	if _, err := team.ContainsUser(user); err != nil {
		handleError(rw, err)
		return
	}

	Ok(rw, team)
}

func teamAddUsers(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	team, err := findTeamByAlias(mux.Vars(r)["alias"], user)
	if err != nil {
		handleError(rw, err)
		return
	}

	var t *account.Team
	if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
		handleError(rw, errors.ErrBadRequest)
		return
	}

	err = team.AddUsers(t.Users)
	if err != nil {
		handleError(rw, err)
		return
	}

	Ok(rw, team)
}

func teamRemoveUsers(rw http.ResponseWriter, r *http.Request) {
	user, err := GetCurrentUser(r)
	if err != nil {
		handleError(rw, err)
		return
	}

	team, err := findTeamByAlias(mux.Vars(r)["alias"], user)
	if err != nil {
		handleError(rw, err)
		return
	}

	var t *account.Team
	if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
		handleError(rw, errors.ErrBadRequest)
		return
	}
	err = team.RemoveUsers(t.Users)
	if err != nil {
		handleError(rw, err)
		return
	}

	Ok(rw, team)
}

func findTeamByAlias(alias string, user *account.User) (*account.Team, error) {
	team, err := account.FindTeamByAlias(alias)
	if err != nil {
		return nil, err
	}
	if _, err := team.ContainsUser(user); err != nil {
		return nil, err
	}

	return team, nil
}

{'fraction_non_alphanumeric': 0.1543942992874109, 'fraction_numerical': 0.0, 'mean_word_length': 3.776614310645724, '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': '168977', 'n_tokens_mistral': 2200, 'n_tokens_neox': 2092, 'n_words': 484}