<commit_before>ACCT_NAME = 'Site Admin'
GROUP_CAT_NAME = 'groupset1'

class ManagerController < ApplicationController
  before_filter :require_user

  def test
    render json: { message: 'Hello' }
  end

  def get_test
    render json: { message: 'Hello' }
  end

  def login
    # the method by which to authenticate and get a token that will allow
    # us to call the REST api
  end

  def logout
    # destroy the session
  end

  # List all groups in the special group set that belongs to the
  # special account.
  # TODO: Only expose fields needed by client.
  def list_groups
    group_cat = GroupCategory.find_by_name(GROUP_CAT_NAME)

    if @current_user.account.site_admin?
      groups = group_cat.groups
               .where("groups.workflow_state != 'deleted'")
               .eager_load(:users)
    else
      # find all groups owned by the student
      groups = group_cat.groups
               .where("groups.workflow_state != 'deleted' and leader_id = ?", @current_user.id)
               .eager_load(:users)
    end

    # After looking at all the options of restricting json output and taking
    # into consideration that I include user-count in the result,
    # this code seemed the clearest.
    response = []
    groups.each do |g|
      response << { id: g.id,
                    name: g.name,
                    leader_id: g.leader_id,
                    created_at: g.created_at,
                    description: g.description,
                    size: g.users.count }
    end

    render json: { count: groups.count,
                   groups: response },
           status: :ok
  end

  # Create a group.
  # If called by a non-admin user then the user will be made leader of the group. 
  # Any specified leader is ignored.
  # If called by an admin a leader may be specified.
  # leader is canvas id
  def create_group
    group_cat = GroupCategory.find_by_name(GROUP_CAT_NAME)
    acct = Account.find_by_name(ACCT_NAME)

    name_param = params[:name]
    leader_id_param = params[:leader_id]
    # string, either 'free_to_join' or 'invite_only'
    join_type_param = params[:join_type]
    desc_param = params[:desc]

    if @current_user.account.site_admin?
      if !leader_id_param.nil? && !leader_id_param.blank?
        leader = User.find_by_id(leader_id_param)
      else
        leader = nil
      end
    else
      # if a non-admin is creating the group then
      # the leader is that user and that id is used to determine who "owns"
      # the group and can make changes to it<commit_msg>Added some validation to create_group.<commit_after>ACCT_NAME = 'Site Admin'
GROUP_CAT_NAME = 'groupset1'

class ManagerController < ApplicationController
  before_filter :require_user

  def test
    render json: { message: 'Hello' }
  end

  def get_test
    render json: { message: 'Hello' }
  end

  def login
    # the method by which to authenticate and get a token that will allow
    # us to call the REST api
  end

  def logout
    # destroy the session
  end

  # List all groups in the special group set that belongs to the
  # special account.
  # TODO: Only expose fields needed by client.
  def list_groups
    group_cat = GroupCategory.find_by_name(GROUP_CAT_NAME)

    if @current_user.account.site_admin?
      groups = group_cat.groups
               .where("groups.workflow_state != 'deleted'")
               .eager_load(:users)
    else
      # find all groups owned by the student
      groups = group_cat.groups
               .where("groups.workflow_state != 'deleted' and leader_id = ?", @current_user.id)
               .eager_load(:users)
    end

    # After looking at all the options of restricting json output and taking
    # into consideration that I include user-count in the result,
    # this code seemed the clearest.
    response = []
    groups.each do |g|
      response << { id: g.id,
                    name: g.name,
                    leader_id: g.leader_id,
                    created_at: g.created_at,
                    description: g.description,
                    size: g.users.count }
    end

    render json: { count: groups.count,
                   groups: response },
           status: :ok
  end

  # Create a group.
  # If called by a non-admin user then the user will be made leader of the group. 
  # Any specified leader is ignored.
  # If called by an admin a leader may be specified.
  # leader is canvas id
  def create_group
    group_cat = GroupCategory.find_by_name(GROUP_CAT_NAME)
    acct = Account.find_by_name(ACCT_NAME)

    name_param = params[:name]
    leader_id_param = params[:leader_id]
    # string, either 'free_to_join' or 'invite_only'
    join_type_param = params[:join_type]
    desc_param = params[:desc]

    if name_param.nil? || name_param.blank?
      render json: { error: 'No name specified.' }, status: :bad_request
      return
    end

    if join_type_param.nil? || join_type_param.blank?
      render json: { error: 'No join_type specified.' }, status: :bad_request
      return
    end

    if @current_user.account.site_admin?
      if !leader_id_param.nil? && !leader_id_param.blank?
        leader = User.find_by_id(leader_id_param)
        if leader.nil?
          render json: { error: "Can't find user specified for leader." }, status: :bad_request
          return
        end
      else
        leader = nil
      end
    else
      # if a non-admin is creating the group then
      # the leader is that user and that id is used to determine who "owns"
      # the group and can make changes to it
{'fraction_non_alphanumeric': 0.07498197548666186, 'fraction_numerical': 0.0003604902667627974, 'mean_word_length': 2.3488231744115873, 'pattern_counts': {'":': 0, '<': 9, '<?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': '5547534', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1607, 'n_words': 550}