Example #1
0
        public static void Main(string[] args)
        {
            IConfiguration Configuration;
            var            builder = new ConfigurationBuilder().AddJsonFile($"appsettings.json", true, true);

            Configuration        = builder.Build();
            _customConfiguration = new CustomConfiguration();
            Configuration.GetSection("CustomConfiguration").Bind(_customConfiguration);

            BikesHelper.Init(_customConfiguration);
            BillingHelper.Init(_customConfiguration);
            MongoHelper.Init(_customConfiguration);

            var host = WebHost.CreateDefaultBuilder <Startup>(args).Build();

            host.Run();
            LogUtility.Log("Reservation engine service running.");
        }
        public static async Task <BillingResponse> CreateInvoice(Guid requestId, Reservation reservationDetails, HttpRequest originRequest)
        {
            LogUtility.LogWithContext(requestId, "Creating an invoice");
            var bikeDetails = await BikesHelper.GetBike(requestId, reservationDetails.BikeId, originRequest);

            var    startTime = DateTime.ParseExact(reservationDetails.StartTime, DateTimeFormat, null);
            var    endTime   = DateTime.ParseExact(reservationDetails.EndTime, DateTimeFormat, null);
            double amount    = 0;

            if (endTime > startTime)
            {
                var totalHours = Math.Ceiling((endTime - startTime).TotalHours);
                amount = totalHours * bikeDetails.HourlyCost;
            }

            var createInvoiceUrl = $"http://{_billingService}/api/invoice";
            var invoice          = new Invoice
            {
                Amount        = (float)amount,
                BikeId        = reservationDetails.BikeId,
                CustomerId    = reservationDetails.UserId,
                VendorId      = bikeDetails.OwnerUserId,
                ReservationId = reservationDetails.ReservationId
            };

            var response = await HttpHelper.PostAsync(requestId, createInvoiceUrl, new StringContent(
                                                          JsonConvert.SerializeObject(invoice), Encoding.UTF8, "application/json"), originRequest);

            var result = new BillingResponse()
            {
                HttpResponse = response
            };

            if (response.IsSuccessStatusCode)
            {
                var obj = JObject.Parse(await response.Content.ReadAsStringAsync());
                result.InvoiceId = obj["id"].Value <string>();
            }
            return(result);
        }