<commit_before>package com.google.sps.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.gson.Gson;
import com.google.sps.Objects.response.MemberResponse;

import error.ErrorHandler;

@WebServlet("/group-member")
public class GroupMemberServlet extends AuthenticatedServlet {

  @Override
  // Adds a new member to a group
  public void doPost(String userId, HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String email = request.getParameter("email").replaceAll("\\s","");
    String newMemberId = request.getParameter("userId");
    Long groupId = Long.parseLong(request.getParameter("groupId"));

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    Entity memberEntity = null;
    if (email != null) {
      memberEntity = getMemberEntityFromEmail(email, response, datastore);
      if (memberEntity == null) return;
    } else if (newMemberId != null) {
      memberEntity = ServletHelper.getUserFromId(response, newMemberId, datastore);
    }

    Entity group = ServletHelper.getEntityFromId(response, groupId, datastore, "Group");
    if (group == null) return;

    doPost_maybeAddUserToGroup(response, datastore, groupId, memberEntity, group);
  }

  // If no account for email, return 'This email doesn't have an account'
  // If user already in group, return 'User already in group'
  // If user not it group, add user and return 'User added to group'
  private void doPost_maybeAddUserToGroup(
      HttpServletResponse response,
      DatastoreService datastore,
      Long groupId,
      Entity memberEntity,
      Entity group) {<commit_msg>fix add group member bug<commit_after>package com.google.sps.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.gson.Gson;
import com.google.sps.Objects.response.MemberResponse;

import error.ErrorHandler;

@WebServlet("/group-member")
public class GroupMemberServlet extends AuthenticatedServlet {

  @Override
  // Adds a new member to a group
  public void doPost(String userId, HttpServletRequest request, HttpServletResponse response)
      throws IOException {

    String email = request.getParameter("email");
    String newMemberId = request.getParameter("userId");
    Long groupId = Long.parseLong(request.getParameter("groupId"));

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();

    Entity memberEntity = null;
    if (email != null) {
      email = email.replaceAll("\\s","");
      memberEntity = getMemberEntityFromEmail(email, response, datastore);
      if (memberEntity == null) return;
    } else if (newMemberId != null) {
      memberEntity = ServletHelper.getUserFromId(response, newMemberId, datastore);
    }

    Entity group = ServletHelper.getEntityFromId(response, groupId, datastore, "Group");
    if (group == null) return;

    doPost_maybeAddUserToGroup(response, datastore, groupId, memberEntity, group);
  }

  // If no account for email, return 'This email doesn't have an account'
  // If user already in group, return 'User already in group'
  // If user not it group, add user and return 'User added to group'
  private void doPost_maybeAddUserToGroup(
      HttpServletResponse response,
      DatastoreService datastore,
      Long groupId,
      Entity memberEntity,
      Entity group) {
{'fraction_non_alphanumeric': 0.09702797202797203, 'fraction_numerical': 0.0, 'mean_word_length': 4.735588972431078, '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': '2893783', 'n_tokens_mistral': 1327, 'n_tokens_neox': 1316, 'n_words': 316}