private async Task <Guid> CreateTrip(TripRequestModel model)
        {
            var drivers = await GetDrivers();

            var users = await GetUsers();

            var driverInfo = drivers.SingleOrDefault(x => x.Id == int.Parse(model.Driver));
            var userInfo   = users.SingleOrDefault(x => x.Id == int.Parse(model.User));

            var createRequestRresponse = await _httpInvoker.InvokeAsync(async() =>
            {
                var client  = new RestClient(_tripApiSettings.Value.BaseUrl);
                var request = new RestRequest(_tripApiSettings.Value.CreateUrl, Method.POST);
                request.AddJsonBody(new
                {
                    userId        = int.Parse(model.User),
                    driverId      = int.Parse(model.Driver),
                    from          = _originsAndDestinations.Values.SingleOrDefault(x => x.Description == model.From),
                    to            = _originsAndDestinations.Values.SingleOrDefault(x => x.Description == model.To),
                    plate         = driverInfo?.CurrentVehicle.Plate,
                    brand         = driverInfo?.CurrentVehicle.Brand,
                    model         = driverInfo?.CurrentVehicle.Model,
                    paymentMethod = userInfo?.PaymentMethod
                });

                return(await client.ExecuteTaskAsync(request));
            });

            if (createRequestRresponse.StatusCode != HttpStatusCode.Created)
            {
                throw new InvalidOperationException("There was an error with Trip service", createRequestRresponse.ErrorException);
            }

            return(JsonConvert.DeserializeObject <Guid>(createRequestRresponse.Content));
        }
Ejemplo n.º 2
0
        public async Task <PaymentInfo> ProcessPaymentAsync(int userId, string reference)
        {
            var response = await _httpInvoker.InvokeAsync(async() =>
            {
                var client  = new RestClient(_paymentServiceBaseUrl);
                var request = new RestRequest(ThirdPartyServices.Payment.PerformPayment(), Method.POST);
                request.AddUrlSegment(nameof(userId), userId);
                request.AddUrlSegment(nameof(reference), reference);

                return(await client.ExecuteTaskAsync(request));
            });

            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new InvalidOperationException("There was an error trying to perform the payment.", response.ErrorException);
            }

            return(PaymentInfoTranslator.Translate(response.Content));
        }