Skip to content

Commit 410f6dd

Browse files
authored
Merge pull request #256 from DiyorMarket/CheckPoint-Details-PopUp
Check point details pop up
2 parents ac9c546 + 9844f61 commit 410f6dd

24 files changed

+432
-251
lines changed

CheckDrive.Web/CheckDrive.Web/CheckDrive.Web.csproj

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
<None Remove="Controllers\**" />
1414
<Content Remove="wwwroot\css\LogOutButtun.css" />
1515
<_ContentIncludedByDefault Remove="appsettings.Production.json" />
16-
<Compile Include="Controllers\AuthController.cs" />
1716
<Compile Include="Controllers\CarsController.cs" />
1817
<Compile Include="Controllers\CheckPointController.cs" />
1918
<Compile Include="Controllers\DebtsController.cs" />

CheckDrive.Web/CheckDrive.Web/Controllers/AuthController.cs

-24
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
using CheckDrive.Web.Stores.CheckPoint;
22
using Microsoft.AspNetCore.Mvc;
33

4-
namespace CheckDrive.Web.Controllers
4+
namespace CheckDrive.Web.Controllers;
5+
6+
public class CheckPointController(ICheckPointStore checkPointStore) : Controller
57
{
6-
public class CheckPointController : Controller
8+
public async Task<ActionResult> Index(int id)
9+
{
10+
var checkPoint = await checkPointStore.GetByIdAsync(id);
11+
12+
return View(checkPoint);
13+
}
14+
15+
public async Task<ActionResult> Details(int id)
716
{
8-
private readonly ICheckPointStore _checkPointStore;
9-
public CheckPointController(ICheckPointStore checkPointStore)
10-
{
11-
_checkPointStore = checkPointStore ?? throw new ArgumentNullException(nameof(checkPointStore));
12-
}
13-
public async Task<ActionResult> Index(int id)
14-
{
15-
var checkPoint = await _checkPointStore.GetCheckPointByIdAsync(id);
17+
var checkPoint = await checkPointStore.GetByIdAsync(id);
1618

17-
return View(checkPoint);
18-
}
19+
return View(checkPoint);
1920
}
2021
}

CheckDrive.Web/CheckDrive.Web/Controllers/ErrorController.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ public IActionResult Index()
1010
return View();
1111
}
1212

13-
[Route("Error/{statusCode:int}")]
13+
[Route("{statusCode:int}")]
1414
public IActionResult ErrorPage(int statusCode)
1515
{
1616
return statusCode switch
1717
{
18-
401 => View(nameof(Unauthorized)),
18+
401 => RedirectToAction("Login", "Home"),
1919
403 => View(nameof(Forbidden)),
2020
404 => View(nameof(NotFound)),
2121
409 => RedirectToAction(nameof(Error409)),
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
1-
using CheckDrive.Web.Stores.Dashboard;
1+
using CheckDrive.Web.Requests.Auth;
2+
using CheckDrive.Web.Stores.Auth;
3+
using CheckDrive.Web.Stores.Dashboard;
24
using Microsoft.AspNetCore.Mvc;
35

46
namespace CheckDrive.Web.Controllers;
57

6-
public class HomeController : Controller
8+
public class HomeController(IAuthStore authStore, IDashboardStore dashboardStore) : Controller
79
{
8-
private readonly IDashboardStore _dashboardStore;
10+
public async Task<IActionResult> Index()
11+
{
12+
var result = await dashboardStore.GetDashboardAsync();
13+
14+
return View(result);
15+
}
916

10-
public HomeController(IDashboardStore dashboardStore)
17+
[HttpGet, Route("login")]
18+
public IActionResult Login() => View();
19+
20+
[HttpPost, Route("login")]
21+
public async Task<IActionResult> Login(LoginRequest request)
1122
{
12-
_dashboardStore = dashboardStore ?? throw new ArgumentNullException(nameof(dashboardStore));
23+
if (!ModelState.IsValid)
24+
{
25+
return View(request);
26+
}
27+
28+
await authStore.LoginAsync(request);
29+
30+
return RedirectToAction("Index", "Home");
1331
}
1432

15-
public async Task<IActionResult> Index()
33+
[HttpGet, Route("logout")]
34+
public IActionResult Logout()
1635
{
17-
var result = await _dashboardStore.GetDashboardAsync();
36+
authStore.Logout();
1837

19-
return View(result);
38+
return RedirectToAction(nameof(Login));
2039
}
2140
}

CheckDrive.Web/CheckDrive.Web/Helpers/AuthorizationHandler.cs

+13-15
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
namespace CheckDrive.Web.Helpers;
99

1010
// Initializes CookieHandler & AuthStore via ServiceProvider due to circular dependency
11-
// AuthorizationHandler -> AuthStore -> CheckDriveApi -> HttpClient -> AuthorizationHandler,
11+
// AuthorizationHandler -> AuthStore -> ApiClient -> HttpClient -> AuthorizationHandler,
1212
// which means before initialization of AuthorizationHandler we cannot resolve AuthStore.
13-
// Resolving CookieHandler via ServiceProvider as well to keep it consistent and easier to follow.
13+
// Resolving CookieHandler via ServiceProvider as well, to keep it consistent and easier to follow.
1414
internal sealed class AuthorizationHandler(IServiceProvider serviceProvider) : DelegatingHandler
1515
{
1616
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
@@ -20,33 +20,31 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
2020
return await base.SendAsync(request, cancellationToken);
2121
}
2222

23-
await AddAuthorizationHeaders(request);
23+
await TryAddAuthorizationHeadersAsync(request);
2424

2525
return await base.SendAsync(request, cancellationToken);
2626
}
2727

28-
private async Task AddAuthorizationHeaders(HttpRequestMessage request)
29-
{
30-
var accessToken = await GetAccessToken();
31-
32-
request.Headers.Authorization = new AuthenticationHeaderValue(HeaderConstants.AuthenticationSchema, accessToken);
33-
}
34-
35-
private async Task<string> GetAccessToken()
28+
private async Task TryAddAuthorizationHeadersAsync(HttpRequestMessage request)
3629
{
3730
using var scope = serviceProvider.CreateScope();
3831
var cookieHandler = scope.ServiceProvider.GetRequiredService<ICookieHandler>();
3932
var authStore = scope.ServiceProvider.GetRequiredService<IAuthStore>();
4033

4134
var (accessToken, refreshToken) = cookieHandler.GetTokens();
4235

43-
if (!JwtHelper.IsTokenValid(accessToken) && !string.IsNullOrWhiteSpace(refreshToken))
36+
if (JwtHelper.IsValid(accessToken))
4437
{
45-
var response = await authStore.RefreshTokenAsync(new RefreshTokenRequest(refreshToken));
38+
request.Headers.Authorization = new AuthenticationHeaderValue(HeaderConstants.AuthenticationSchema, accessToken);
4639

47-
return response.AccessToken;
40+
return;
4841
}
4942

50-
return accessToken ?? string.Empty;
43+
if (!string.IsNullOrWhiteSpace(refreshToken))
44+
{
45+
var response = await authStore.RefreshTokenAsync(new RefreshTokenRequest(refreshToken));
46+
47+
request.Headers.Authorization = new AuthenticationHeaderValue(HeaderConstants.AuthenticationSchema, response.AccessToken);
48+
}
5149
}
5250
}

CheckDrive.Web/CheckDrive.Web/Helpers/JwtHelper.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using Microsoft.IdentityModel.Tokens;
1+
using System.Diagnostics.CodeAnalysis;
22
using System.IdentityModel.Tokens.Jwt;
33
using System.Security.Claims;
4+
using Microsoft.IdentityModel.Tokens;
45

56
namespace CheckDrive.Web.Helpers;
67

78
internal static class JwtHelper
89
{
9-
public static bool IsTokenValid(string? token)
10+
public static bool IsValid([NotNullWhen(true)] string? token)
1011
{
1112
if (string.IsNullOrEmpty(token))
1213
{

CheckDrive.Web/CheckDrive.Web/Stores/Auth/AuthStore.cs

+2
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,6 @@ public async Task<TokenResponse> RefreshTokenAsync(RefreshTokenRequest request)
2424

2525
return response;
2626
}
27+
28+
public void Logout() => cookieHandler.ClearTokens();
2729
}

CheckDrive.Web/CheckDrive.Web/Stores/Auth/IAuthStore.cs

+1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public interface IAuthStore
77
{
88
Task<TokenResponse> LoginAsync(LoginRequest request);
99
Task<TokenResponse> RefreshTokenAsync(RefreshTokenRequest request);
10+
void Logout();
1011
}

CheckDrive.Web/CheckDrive.Web/Stores/CheckPoint/CheckPointStore.cs

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ internal sealed class CheckPointStore(CheckDriveApi apiClient) : ICheckPointStor
1212
public Task<List<CheckPointViewModel>> GetAllAsync()
1313
=> apiClient.GetAsync<List<CheckPointViewModel>>(_resourceUrl);
1414

15-
public async Task<CheckPointViewModel> GetCheckPointByIdAsync(int id)
15+
public async Task<CheckPointViewModel> GetByIdAsync(int id)
1616
{
17-
var checkPoints = await apiClient.GetAsync<List<CheckPointViewModel>>(_resourceUrl);
17+
var checkPoint = await apiClient.GetAsync<CheckPointViewModel>($"{_resourceUrl}/{id}");
1818

19-
var result = checkPoints.FirstOrDefault(x => x.Id == id);
20-
21-
return result;
19+
return checkPoint;
2220
}
2321

2422
public List<SelectListItem> GetEnumValues<TEnum>() where TEnum : Enum

CheckDrive.Web/CheckDrive.Web/Stores/CheckPoint/ICheckPointStore.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ namespace CheckDrive.Web.Stores.CheckPoint;
66
public interface ICheckPointStore
77
{
88
Task<List<CheckPointViewModel>> GetAllAsync();
9-
Task<CheckPointViewModel> GetCheckPointByIdAsync(int id);
9+
Task<CheckPointViewModel> GetByIdAsync(int id);
1010
List<SelectListItem> GetEnumValues<TEnum>() where TEnum : Enum;
1111
}

CheckDrive.Web/CheckDrive.Web/ViewModels/CheckPoint/CheckPointViewModel.cs

+8
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,25 @@ public class CheckPointViewModel
99
public string Driver { get; set; }
1010
public string CarModel { get; set; }
1111
public decimal CurrentMileage { get; set; }
12+
public string FormattedCurrentMileage => CurrentMileage.ToString("N1");
1213
public decimal CurrentFuelAmount { get; set; }
14+
public string FormattedCurrentFuelAmount => CurrentFuelAmount.ToString("N1");
1315
public string Mechanic { get; set; }
1416
public decimal InitialMillage { get; set; }
17+
public string FormattedInitialMillage => InitialMillage.ToString("N1");
1518
public decimal FinalMileage { get; set; }
19+
public string FormattedFinalMileage => FinalMileage.ToString("N1");
1620
public string Operator { get; set; }
1721
public decimal InitialOilAmount { get; set; }
22+
public string FormattedInitialOilAmount => InitialOilAmount.ToString("N1");
1823
public decimal OilRefillAmount { get; set; }
24+
public string FormattedOilRefillAmount => OilRefillAmount.ToString("N1");
1925
public string Oil { get; set; }
2026
public string Dispatcher { get; set; }
2127
public decimal FuelConsumptionAdjustment { get; set; }
28+
public string FormattedFuelConsumptionAdjustment => FuelConsumptionAdjustment.ToString("N1");
2229
public decimal DebtAmount { get; set; }
30+
public string FormattedDebtAmount => DebtAmount.ToString("N1");
2331
public CheckPointStage Stage { get; set; }
2432
public CheckPointStatus Status { get; set; }
2533
}

0 commit comments

Comments
 (0)