diff --git a/PracticalDebuggingWeb/.config/dotnet-tools.json b/PracticalDebuggingWeb/.config/dotnet-tools.json new file mode 100644 index 0000000..5112d4b --- /dev/null +++ b/PracticalDebuggingWeb/.config/dotnet-tools.json @@ -0,0 +1,12 @@ +{ + "version": 1, + "isRoot": true, + "tools": { + "dotnet-ef": { + "version": "3.1.8", + "commands": [ + "dotnet-ef" + ] + } + } +} \ No newline at end of file diff --git a/PracticalDebuggingWeb/Controllers/PriceCalculatorController.cs b/PracticalDebuggingWeb/Controllers/PriceCalculatorController.cs new file mode 100644 index 0000000..d712ec9 --- /dev/null +++ b/PracticalDebuggingWeb/Controllers/PriceCalculatorController.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using PracticalDebuggingWeb.Controllers.Services; +using PracticalDebuggingWeb.Models; +using Serilog; + +namespace PracticalDebuggingWeb.Controllers +{ + public class PriceCalculatorController : Controller + { + public async Task Calculate(string price, string convertCurrency, string couponCode, string discount) + { + var result = await new PriceCalculator().Calculate(price, convertCurrency, couponCode, discount); + return View("Result", result); + } + + + + } +} diff --git a/PracticalDebuggingWeb/Controllers/Services/CouponService.cs b/PracticalDebuggingWeb/Controllers/Services/CouponService.cs new file mode 100644 index 0000000..e0c4d89 --- /dev/null +++ b/PracticalDebuggingWeb/Controllers/Services/CouponService.cs @@ -0,0 +1,38 @@ +using System; +using System.Net.Http; +using System.Threading; +using System.Threading.Tasks; + +namespace PracticalDebuggingWeb.Controllers.Services +{ + internal class CouponService + { + internal static double GetCouponDiscount(string couponCode) + { + if (couponCode == "Employee2423432") + { + return 25; + } + else if (couponCode == "VIP_2020_rmg") + { + return 15; + } + else + { + AutoResetEvent are = new AutoResetEvent(false); + double result = 0; + ThreadPool.QueueUserWorkItem((_) => + { + var httpClient = HttpClientFactory.Create(); + var res = httpClient.GetAsync("https://couponservice.com/api/Discount?couponCode=" + couponCode) + .GetAwaiter().GetResult(); + var content = res.Content.ReadAsStringAsync().GetAwaiter().GetResult(); + result = double.Parse(content); + are.Set(); + }); + are.WaitOne(); + return result; + } + } + } +} \ No newline at end of file diff --git a/PracticalDebuggingWeb/Controllers/Services/Currency.cs b/PracticalDebuggingWeb/Controllers/Services/Currency.cs new file mode 100644 index 0000000..833379d --- /dev/null +++ b/PracticalDebuggingWeb/Controllers/Services/Currency.cs @@ -0,0 +1,4 @@ +namespace PracticalDebuggingWeb.Controllers.Services +{ + public enum Currency { USD, EURO, GBP}; +} diff --git a/PracticalDebuggingWeb/Controllers/Services/CurrencyConverter.cs b/PracticalDebuggingWeb/Controllers/Services/CurrencyConverter.cs new file mode 100644 index 0000000..6637124 --- /dev/null +++ b/PracticalDebuggingWeb/Controllers/Services/CurrencyConverter.cs @@ -0,0 +1,22 @@ +using System; +using System.Net.Http; + +namespace PracticalDebuggingWeb.Controllers.Services +{ + internal class CurrencyConverter + { + internal static double ConvertToUsd(string price, Currency currency) + { + var exchangeRate = GetExchangeRate(currency, Currency.USD); + var pricenum = double.Parse(price); + return pricenum * exchangeRate; + } + + private static double GetExchangeRate(Currency from, Currency to) + { + var httpClient = new HttpClient(); + //TODO: Go to https://exchangeratesapi.io/ + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/PracticalDebuggingWeb/Controllers/Services/CurrencyService.cs b/PracticalDebuggingWeb/Controllers/Services/CurrencyService.cs new file mode 100644 index 0000000..0c3593b --- /dev/null +++ b/PracticalDebuggingWeb/Controllers/Services/CurrencyService.cs @@ -0,0 +1,13 @@ +using System; + +namespace PracticalDebuggingWeb.Controllers.Services +{ + internal class CurrencyService + { + internal static Currency GetCurrencyFromString(string convertCurrency) + { + var cur = Enum.Parse(convertCurrency, true); + return cur; + } + } +} \ No newline at end of file diff --git a/PracticalDebuggingWeb/Controllers/Services/PriceCalculator.cs b/PracticalDebuggingWeb/Controllers/Services/PriceCalculator.cs new file mode 100644 index 0000000..b4d5aa3 --- /dev/null +++ b/PracticalDebuggingWeb/Controllers/Services/PriceCalculator.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace PracticalDebuggingWeb.Controllers.Services +{ + + + public class PriceCalculator + { + + public async Task Calculate(string price, string convertCurrency, string couponCode, string discount) + { + var currency = CurrencyService.GetCurrencyFromString(convertCurrency); + double priceUsd; + if (currency == Currency.USD) + { + priceUsd = double.Parse(price); + } + else + { + priceUsd = CurrencyConverter.ConvertToUsd(price, currency); + } + + if (!string.IsNullOrEmpty(couponCode)) + { + double couponDiscountPercent = CouponService.GetCouponDiscount(couponCode); + var couponDiscount = GetDiscount(couponDiscountPercent, priceUsd); + priceUsd -= couponDiscount; + } + + var discountNum = GetDiscount(double.Parse(discount), priceUsd); + return priceUsd - discountNum; + } + + private double GetDiscount(double discount, double price) + { + return discount / 100.0 * price; + } + } +} diff --git a/PracticalDebuggingWeb/PracticalDebuggingWeb.csproj b/PracticalDebuggingWeb/PracticalDebuggingWeb.csproj index 62a245a..01501fa 100644 --- a/PracticalDebuggingWeb/PracticalDebuggingWeb.csproj +++ b/PracticalDebuggingWeb/PracticalDebuggingWeb.csproj @@ -9,6 +9,7 @@ + diff --git a/PracticalDebuggingWeb/Startup.cs b/PracticalDebuggingWeb/Startup.cs index fb3e624..b2ba30e 100644 --- a/PracticalDebuggingWeb/Startup.cs +++ b/PracticalDebuggingWeb/Startup.cs @@ -38,11 +38,11 @@ public void ConfigureServices(IServiceCollection services) // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { - if (env.IsDevelopment()) - { - app.UseDeveloperExceptionPage(); - } - else + //if (env.IsDevelopment()) + //{ + // //app.UseDeveloperExceptionPage(); + //} + //else { app.UseExceptionHandler("/Home/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. diff --git a/PracticalDebuggingWeb/Views/Home/Index.cshtml b/PracticalDebuggingWeb/Views/Home/Index.cshtml index d2d19bd..bb724d9 100644 --- a/PracticalDebuggingWeb/Views/Home/Index.cshtml +++ b/PracticalDebuggingWeb/Views/Home/Index.cshtml @@ -2,7 +2,33 @@ ViewData["Title"] = "Home Page"; } -
-

Welcome

-

Learn about building Web apps with ASP.NET Core.

-
+
+

Price Calculator

+
+
+
+
+

Price (USD)

+ +
+ Convert currency + + +
+ +

Coupon code

+ +

Additional Discount

+ + + + + +
+ + +
diff --git a/PracticalDebuggingWeb/Views/PriceCalculator/Result.cshtml b/PracticalDebuggingWeb/Views/PriceCalculator/Result.cshtml new file mode 100644 index 0000000..7cd802a --- /dev/null +++ b/PracticalDebuggingWeb/Views/PriceCalculator/Result.cshtml @@ -0,0 +1,6 @@ +@{ + ViewData["Title"] = "Result Page"; +} + +

Result price: @Model

+

Currency: USD

diff --git a/PracticalDebuggingWeb/Views/Shared/_Layout.cshtml b/PracticalDebuggingWeb/Views/Shared/_Layout.cshtml index ae28d34..ee6d09f 100644 --- a/PracticalDebuggingWeb/Views/Shared/_Layout.cshtml +++ b/PracticalDebuggingWeb/Views/Shared/_Layout.cshtml @@ -3,7 +3,7 @@ - @ViewData["Title"] - PracticalDebuggingWeb + Advanced Production Debugging Techniques @@ -21,7 +21,7 @@