Ejemplo n.º 1
0
        public static async Task <ResponseResult <Friendship> > GetFriendship(string jwt, string email)
        {
            using (var request = new HttpRequestMessage(HttpMethod.Get, $"{URI}/GetFriendship"))
            {
                var viewModel = new AuthorizedEmailViewModel {
                    Email = email, JwtFrom = jwt
                };
                request.Content = new StringContent(JsonConvert.SerializeObject(viewModel), Encoding.UTF8, "application/json");

                var response = await _client.SendAsync(request);

                if (response.IsSuccessStatusCode)
                {
                    return(new ResponseResult <Friendship>(true, JsonConvert.DeserializeObject <Friendship>(await response.Content.ReadAsStringAsync())));
                }
                else if (await response.Content.ReadAsStringAsync() == "Does Not Exist")
                {
                    return(new ResponseResult <Friendship>(true, null));
                }
                else
                {
                    return(new ResponseResult <Friendship>(false, null));
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create(AuthorizedEmailViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                // Ensure that the JWT refers to a User in the Repository, using
                // the Accounts microservice
                var fromEmail =
                    await Services.AuthorizationServices.VerifyToken(_clientFactory, viewModel.JwtFrom);

                if (fromEmail != null)
                {
                    try
                    {
                        await friendshipRepo.AddUnconfirmedFriendshipAsync(fromEmail, viewModel.Email);

                        return(Ok());
                    }
                    catch (FriendshipAlreadyExistsException)
                    {
                        return(BadRequest("Friendship already exists"));
                    }
                }
            }

            return(BadRequest(ModelState));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> GetUser(AuthorizedEmailViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                // Verify the Jwt
                var fromEmail =
                    await AuthorizationServices.VerifyToken(clientFactory, viewModel.JwtFrom);

                // Return Unauthorized if the Jwt was not validated
                if (fromEmail == null)
                {
                    return(Unauthorized());
                }

                try
                {
                    return(Ok(JsonConvert.SerializeObject(await userRepo.GetUserAsync(viewModel.Email))));
                }
                catch (UserDoesNotExistException)
                {
                    return(BadRequest("The User does not exist"));
                }
            }
            return(BadRequest(ModelState));
        }
Ejemplo n.º 4
0
        public static async Task <ResponseResult> CreateFriendship(string jwt, string email)
        {
            using (var request = new HttpRequestMessage(HttpMethod.Post, $"{URI}/Create"))
            {
                var viewModel = new AuthorizedEmailViewModel {
                    JwtFrom = jwt, Email = email
                };

                request.Content = new StringContent(JsonConvert.SerializeObject(viewModel), Encoding.UTF8, "application/json");
                var response = await _client.SendAsync(request);

                if (response.IsSuccessStatusCode)
                {
                    return(new ResponseResult(true));
                }

                return(new ResponseResult(false));
            }
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> GetFriendship(AuthorizedEmailViewModel viewModel)
        {
            if (ModelState.IsValid)
            {
                var fromEmail =
                    await Services.AuthorizationServices.VerifyToken(_clientFactory, viewModel.JwtFrom);

                if (fromEmail != null)
                {
                    try
                    {
                        return(Ok(JsonConvert.SerializeObject(
                                      await friendshipRepo.GetFriendship(fromEmail, viewModel.Email)
                                      )));
                    }
                    catch (FriendshipDoesNotExistException)
                    {
                        return(BadRequest("Does Not Exist"));
                    }
                }
            }
            return(BadRequest());
        }