Exemplo n.º 1
0
        public ActionResult Suggestions(string commonName)
        {
            GoogleRequestModel gr   = new GoogleRequestModel();
            GoogleRepository   grep = new GoogleRepository();

            gr = grep.GetRequests(commonName);
            ViewBag.commonName = commonName;
            return(PartialView("~/Views/Shared/_RequestsGoogle.cshtml", gr));
        }
Exemplo n.º 2
0
        public async Task <long> GetSearchTotalCountAsync(string query)
        {
            if (string.IsNullOrEmpty(query))
            {
                throw new ArgumentNullException($"The {nameof(query)} you provided is null or empty");
            }

            using var result = await _httpClient.GetAsync(GoogleRequestModel.GetUrl(query));

            if (!result.IsSuccessStatusCode)
            {
                throw new HttpRequestException($"Something went wrong with request, statusCode: {result.StatusCode}");
            }
            var response = JsonConvert.DeserializeObject <GoogleResponseModel>(await result.Content.ReadAsStringAsync());

            return(response.SearchInformation.TotalResults);
        }
        public async Task <IHttpActionResult> Google([FromBody] GoogleRequestModel model)
        {
            try
            {
                if (!ModelState.IsValid || model == null)
                {
                    return(BadRequest());
                }

                var body = new List <KeyValuePair <string, string> >
                {
                    new KeyValuePair <string, string>("grant_type", "authorization_code"),
                    new KeyValuePair <string, string>("code", model.Code),
                    new KeyValuePair <string, string>("redirect_uri", "http://localhost/signin-google"),
                    new KeyValuePair <string, string>("client_id", model.ClientId)
                };

                var client        = new HttpClient();
                var url           = new Uri(Constants.TokenEndpoint);
                var form          = new FormUrlEncodedContent(body);
                var tokenResponse = await client.PostAsync(url, form);

                if (tokenResponse.IsSuccessStatusCode == false)
                {
                    return(BadRequest());
                }

                string text = await tokenResponse.Content.ReadAsStringAsync();

                JObject response    = JObject.Parse(text);
                string  accessToken = response.Value <string>("access_token");
                string  tokenType   = response.Value <string>("token_type");

                if (string.IsNullOrEmpty(accessToken))
                {
                    return(NotFound());
                }

                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, Constants.UserInfoEndpoint);
                request.Headers.Authorization = new AuthenticationHeaderValue(tokenType, accessToken);
                HttpResponseMessage userInfoResponse = await client.SendAsync(request);

                if (userInfoResponse.IsSuccessStatusCode == false)
                {
                    return(BadRequest());
                }

                text = await userInfoResponse.Content.ReadAsStringAsync();

                JObject userObj = JObject.Parse(text);

                string userEmail = _responseValuesGetter.TryGetValue(userObj, "email");
                string googleId  = _responseValuesGetter.TryGetValue(userObj, "id");

                User user = await _userRepository.FindByEmailAsync(userEmail);

                if (user == null)
                {
                    var info = new PublicInfo
                    {
                        NickName   = _responseValuesGetter.TryGetValue(userObj, "name"),
                        PictureUrl = _responseValuesGetter.TryGetValue(userObj, "picture")
                    };

                    user = new User
                    {
                        Email      = userEmail,
                        Gender     = _responseValuesGetter.GetGender(_responseValuesGetter.TryGetValue(userObj, "gender")),
                        PublicInfo = info
                    };

                    user.ExternalKeys.Add(new ExternalKey
                    {
                        Key  = googleId,
                        Type = ExternalSystemType.Google
                    });

                    await _userRepository.AddAsync(user);
                }

                List <User> userFriends = await _userRepository.FindUsersByIds(user.FriendsIds);

                string sendToken = _tokenManager.CreateSendToken(user);

                var responseModel = new GoogleResponseModel
                {
                    User = new UserResponseModel
                    {
                        ServerId    = user.Id.ToString(),
                        Email       = user.Email,
                        PublicInfo  = user.PublicInfo,
                        BirthDate   = user.BirthDate,
                        Gender      = user.Gender,
                        LookingFor  = user.LookingFor,
                        Seconds     = user.Seconds,
                        FriendsList = userFriends.Select(x => new UserResponseModel
                        {
                            ServerId   = x.Id.ToString(),
                            Email      = x.Email,
                            PublicInfo = x.PublicInfo,
                            BirthDate  = x.BirthDate,
                            Gender     = x.Gender,
                            LookingFor = x.LookingFor,
                            Seconds    = x.Seconds,
                        }).ToList()
                    },
                    Token = sendToken
                };
                return(Ok(responseModel));
            }
            catch (Exception)
            {
                return(InternalServerError());
            }
        }