Exemple #1
0
        public async Task <IActionResult> GetRequestsForUser([FromBody] RequestsForUserRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            CultureInfo cultureInfo = new CultureInfo(request.CultureInfo);

            Resource.Culture = cultureInfo;

            UserEntity userEntity = await _context.Users
                                    .FirstOrDefaultAsync(u => u.Id == request.UserId.ToString());

            if (userEntity == null)
            {
                return(BadRequest(Resource.UserDoesntExists));
            }

            List <RequestEntity> requestEntities = await _context.Requests.Include(r => r.Type).
                                                   Include(r => r.User).
                                                   Where(r => r.User.Id == request.UserId.ToString()).ToListAsync();

            List <RequestResponse> requestResponses = new List <RequestResponse>();

            foreach (RequestEntity requestEntity in requestEntities)
            {
                requestResponses.Add(_converterHelper.ToRequestResponse(requestEntity));
            }

            return(Ok(requestResponses));
        }
Exemple #2
0
        public async Task <Response> GetRequestsForUserAsync(string urlBase, string servicePrefix, string controller, RequestsForUserRequest requestsForUserRequest, string tokenType, string accessToken)
        {
            try
            {
                string        request = JsonConvert.SerializeObject(requestsForUserRequest);
                StringContent content = new StringContent(request, Encoding.UTF8, "application/json");
                HttpClient    client  = new HttpClient
                {
                    BaseAddress = new Uri(urlBase)
                };

                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(tokenType, accessToken);
                string url = $"{servicePrefix}{controller}";
                HttpResponseMessage response = await client.PostAsync(url, content);

                string result = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return(new Response
                    {
                        IsSuccess = false,
                        Message = result,
                    });
                }

                List <RequestResponse> list = JsonConvert.DeserializeObject <List <RequestResponse> >(result);
                return(new Response
                {
                    IsSuccess = true,
                    Result = list
                });
            }
            catch (Exception ex)
            {
                return(new Response
                {
                    IsSuccess = false,
                    Message = ex.Message
                });
            }
        }