Пример #1
0
        public HttpResponseMessage <Earthwatcher> LoginWithApi(ApiEw ew, HttpRequestMessage <ApiEw> request)
        {
            var    localization = new LocalizationService();
            string locale       = localization.GetLocale();

            if (!string.IsNullOrEmpty(ew.UserId))
            {
                ApiEw apiEw = earthwatcherRepository.GetApiEw(ew.Api, ew.UserId);

                if (apiEw == null)  //Si no existe en la tabla ApiEwLogin lo inserta y ademas Crea el EARTHWATCHER
                {
                    //INSERTA EL ApiEw en la tabla ApiEwLogin
                    ApiEw newApiEw = earthwatcherRepository.CreateApiEwLogin(ew);

                    if (ew.Api == "Facebook")
                    {
                        Earthwatcher ewIdemFbMail = earthwatcherRepository.GetEarthwatcher(ew.Mail, false);

                        if (ewIdemFbMail != null)  //Si en FB tiene el mismo mail que en guardianes lo relaciona
                        {
                            //Relaciona al ApiEw de facebook con el Earthwatcher del mismo mail
                            earthwatcherRepository.LinkApiAndEarthwatcher(newApiEw.Id, ewIdemFbMail.Id);

                            //Le pasa el UserId y AccessToken para conectarse
                            ewIdemFbMail.UserId      = ew.UserId;
                            ewIdemFbMail.AccessToken = ew.AccessToken;

                            //Devuelve el Ew que YA EXISTE relacionado con ese mail de FB
                            return(new HttpResponseMessage <Earthwatcher>(ewIdemFbMail)
                            {
                                StatusCode = HttpStatusCode.OK
                            });
                        }
                        else
                        {
                            //INSERTA EL EARTHWATCHER
                            var earthwatcher = new Earthwatcher();
                            if (!string.IsNullOrEmpty(ew.Mail))
                            {
                                earthwatcher.Name = ew.Mail;   //Ingreso el mail valido de ese Ew
                            }
                            else
                            {
                                earthwatcher.Name = ew.NickName + ew.UserId.ToString();                               //Ingreso un customMail para ese EW
                            }
                            earthwatcher.Password       = ew.NickName + earthwatcher.NickName + ew.UserId.ToString(); //Pass = Doble nombre mas userId
                            earthwatcher.NickName       = ew.NickName;
                            earthwatcher.Language       = (locale != null) ? locale.Substring(0, 5) : "en-CA";
                            earthwatcher.Country        = (locale != null) ? locale.Substring(3, 2) : "es-AR";
                            earthwatcher.PlayingRegion  = (locale != null) ? Convert.ToInt32(locale.Substring(6, 1)) : 1;
                            earthwatcher.PlayingCountry = (regionRepository.GetById(earthwatcher.PlayingRegion)).CountryCode;
                            earthwatcher.Guid           = Guid.NewGuid();

                            Earthwatcher newEarthwatcher = earthwatcherRepository.CreateEarthwatcher(earthwatcher);

                            //Relaciona al ApiEw con el Earthwatcher
                            earthwatcherRepository.LinkApiAndEarthwatcher(newApiEw.Id, newEarthwatcher.Id);

                            //ASIGNO UNA LAND AL NUEVO EW
                            var  newLand    = earthwatcherRepository.AssignLandToEarthwatcher(earthwatcher.Id, string.Empty);
                            Land newLandObj = null;
                            Console.WriteLine("Me paso la new land " + newLand.Id + " " + newLand.LandId);
                            if (newLand == null) //Si la region esta completa le asigno la land del tutor
                            {
                                newLandObj = landRepository.GetTutorLand(earthwatcher.PlayingRegion);
                            }
                            if (newLandObj == null) //Si no esta completa la region
                            {
                                newLandObj = landRepository.GetLandByGeoHexKey(newLand.GeohexKey);
                            }
                            earthwatcher.Lands = new List <Land>();
                            earthwatcher.Lands.Add(newLandObj);
                            //Comunico a los usuarios conectados si es que la nueva land es de un usuario existente
                            NotificateUsers(newLand, earthwatcher.Id);

                            //Le pasa el D:\Dev\Greenpeace\Guardianes\Earthwatchers.UI\Requests\OpengeocoderRequests.csUserId y AccessToken para conectarse
                            newEarthwatcher.UserId      = ew.UserId;
                            newEarthwatcher.AccessToken = ew.AccessToken;

                            //Devuelve el ew NUEVO relacionado con esa nueva cuenta de FB
                            return(new HttpResponseMessage <Earthwatcher>(newEarthwatcher)
                            {
                                StatusCode = HttpStatusCode.OK
                            });
                        }
                    }
                    if (ew.Api != "Facebook")
                    {
                        //INSERTA EL EARTHWATCHER
                        var earthwatcher = new Earthwatcher();
                        earthwatcher.Name           = ew.NickName + ew.UserId.ToString();
                        earthwatcher.Password       = ew.NickName + earthwatcher.NickName + ew.UserId.ToString(); //Pass = Doble nombre mas userId
                        earthwatcher.NickName       = ew.NickName;
                        earthwatcher.Language       = (locale != null) ? locale.Substring(0, 5) : "en-CA";
                        earthwatcher.Country        = (locale != null) ? locale.Substring(3, 2) : "es-AR";
                        earthwatcher.PlayingRegion  = (locale != null) ? Convert.ToInt32(locale.Substring(6, 1)) : 1;
                        earthwatcher.PlayingCountry = (regionRepository.GetById(earthwatcher.PlayingRegion)).CountryCode;
                        earthwatcher.Guid           = Guid.NewGuid();

                        Earthwatcher newEarthwatcher = earthwatcherRepository.CreateEarthwatcher(earthwatcher);

                        //Relaciona al ApiEw con el Earthwatcher
                        earthwatcherRepository.LinkApiAndEarthwatcher(newApiEw.Id, newEarthwatcher.Id);

                        var  newLand    = earthwatcherRepository.AssignLandToEarthwatcher(earthwatcher.Id, string.Empty);
                        Land newLandObj = null;
                        Console.WriteLine("Me paso la new land " + newLand.Id + " " + newLand.LandId);
                        if (newLand == null) //Si la region esta completa le asigno la land del tutor
                        {
                            newLandObj = landRepository.GetTutorLand(earthwatcher.PlayingRegion);
                        }
                        if (newLandObj == null) //Si no esta completa la region
                        {
                            newLandObj = landRepository.GetLandByGeoHexKey(newLand.GeohexKey);
                        }
                        earthwatcher.Lands = new List <Land>();
                        earthwatcher.Lands.Add(newLandObj);
                        //Comunico a los usuarios conectados si es que la nueva land es de un usuario existente
                        NotificateUsers(newLand, earthwatcher.Id);

                        //Le pasa el UserId y AccessToken para conectarse
                        newEarthwatcher.UserId      = ew.UserId;
                        newEarthwatcher.AccessToken = ew.AccessToken;

                        return(new HttpResponseMessage <Earthwatcher>(newEarthwatcher)
                        {
                            StatusCode = HttpStatusCode.OK
                        });
                    }
                }
                else
                {
                    //Si ya existe en mi tabla ApiLogin Le updateo el accessToken
                    if (ew.AccessToken != apiEw.AccessToken)
                    {
                        earthwatcherRepository.UpdateAccessToken(apiEw.Id, ew.AccessToken);
                    }
                    //Lo busco por el Id del EW relacionado
                    Earthwatcher earthwatcher = earthwatcherRepository.GetEarthwatcher(apiEw.EarthwatcherId);

                    //Le Agrega el UserId al Earthwatcher y lo devuelve
                    earthwatcher.UserId      = apiEw.UserId;
                    earthwatcher.AccessToken = ew.AccessToken;
                    return(new HttpResponseMessage <Earthwatcher>(earthwatcher)
                    {
                        StatusCode = HttpStatusCode.OK
                    });
                }
            }
            return(new HttpResponseMessage <Earthwatcher>(null)
            {
                StatusCode = HttpStatusCode.BadRequest
            });
        }