Skip to content

Commit d6294fd

Browse files
authored
Merge pull request #250 from DiyorMarket/Debts
Everything has been done regarding debts.
2 parents 931e60e + 7970b6c commit d6294fd

File tree

12 files changed

+300
-176
lines changed

12 files changed

+300
-176
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,63 @@
1-
using CheckDrive.Web.Stores.Debts;
1+
using System.ComponentModel.DataAnnotations;
2+
using System.Reflection;
3+
using CheckDrive.Web.Mappings;
4+
using CheckDrive.Web.Models.Enums;
5+
using CheckDrive.Web.Requests.Debt;
6+
using CheckDrive.Web.Stores.Debts;
27
using Microsoft.AspNetCore.Mvc;
8+
using Microsoft.AspNetCore.Mvc.Rendering;
39

410
namespace CheckDrive.Web.Controllers;
511

612
public class DebtsController : Controller
713
{
8-
private readonly IDebtsStore _store;
9-
public DebtsController(IDebtsStore debtsStore)
14+
private readonly IDebtsStore _debtStore;
15+
public DebtsController(IDebtsStore debtStore)
1016
{
11-
_store = debtsStore ?? throw new ArgumentNullException(nameof(debtsStore));
17+
_debtStore = debtStore ?? throw new ArgumentNullException(nameof(debtStore));
1218
}
13-
public ActionResult Index(string? searchText, string? status)
19+
public async Task<ActionResult> Index()
1420
{
15-
var debts = _store.GetAll(searchText,status);
21+
var debts = await _debtStore.GetAsync();
22+
23+
ViewBag.SelectedStatus = GetDebtStatuses();
1624

17-
ViewBag.Status = _store.GetEnum();
18-
ViewBag.SearchText = searchText;
19-
ViewBag.SelectedStatus = status;
2025
return View(debts);
2126
}
27+
28+
[HttpGet]
29+
public async Task<ActionResult> Edit(int id)
30+
{
31+
var debt = await _debtStore.GetByIdAsync(id);
32+
33+
var updateRequest = debt.ToUpdateViewModel();
34+
35+
return View(updateRequest);
36+
}
37+
38+
[HttpPost]
39+
public async Task<ActionResult> Edit(UpdateDebtRequest request)
40+
{
41+
request.Status = DebtStatus.Paid;
42+
43+
await _debtStore.UpdateAsync(request);
44+
45+
return RedirectToAction(nameof(Index));
46+
}
47+
48+
private static List<SelectListItem> GetDebtStatuses()
49+
{
50+
var excludedStatuses = new[] { DebtStatus.Paid, DebtStatus.Unpaid };
51+
var statuses = Enum.GetValues(typeof(DebtStatus))
52+
.Cast<DebtStatus>()
53+
.Where(e => excludedStatuses.Contains(e))
54+
.Select(e => new SelectListItem()
55+
{
56+
Value = e.ToString(),
57+
Text = e.GetType().GetField(e.ToString())?.GetCustomAttribute<DisplayAttribute>()?.Name ?? e.ToString()
58+
})
59+
.ToList();
60+
61+
return statuses;
62+
}
2263
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using CheckDrive.Web.Requests.Debt;
2+
using CheckDrive.Web.ViewModels.Debt;
3+
4+
namespace CheckDrive.Web.Mappings;
5+
6+
public static class DebtMappings
7+
{
8+
public static UpdateDebtRequest ToUpdateViewModel(this DebtViewModel viewModel) =>
9+
new()
10+
{
11+
Id = viewModel.Id,
12+
DriverFullName = viewModel.DriverFullName,
13+
CheckPointId = viewModel.CheckPointId,
14+
FuelAmount = viewModel.FuelAmount,
15+
PaidAmount = viewModel.PaidAmount,
16+
Status = viewModel.Status,
17+
};
18+
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
namespace CheckDrive.Web.Models.Enums;
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace CheckDrive.Web.Models.Enums;
24

35
public enum DebtStatus
46
{
5-
Paid,
6-
Unpaid
7+
[Display(Name = "To'langan")]
8+
Paid = 0,
9+
10+
[Display(Name = "To'lanmagan")]
11+
Unpaid = 1,
12+
13+
[Display(Name = "Qisman to'langan")]
14+
PartiallyPaid,
15+
16+
[Display(Name = "Menejerni kutilmoqda")]
17+
PendingManagerApproval
718
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using CheckDrive.Web.Models.Enums;
2+
3+
namespace CheckDrive.Web.Requests.Debt;
4+
5+
public class UpdateDebtRequest
6+
{
7+
public int Id { get; set; }
8+
public required string DriverFullName { get; set; }
9+
public decimal FuelAmount { get; set; }
10+
public decimal PaidAmount { get; set; }
11+
public int CheckPointId { get; set; }
12+
public DebtStatus Status { get; set; }
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
1-
using CheckDrive.Web.ViewModels.Debt;
2-
using Microsoft.AspNetCore.Mvc.Rendering;
1+
using CheckDrive.Web.Requests.Debt;
2+
using CheckDrive.Web.Services;
3+
using CheckDrive.Web.ViewModels.Debt;
34

45
namespace CheckDrive.Web.Stores.Debts;
56

6-
public class DebtsStore : IDebtsStore
7+
internal sealed class DebtsStore : IDebtsStore
78
{
8-
public List<DebtViewModel> GetAll(string? searchText, string? status)
9+
private static readonly string resourceUrl = "debts";
10+
private readonly CheckDriveApi _apiClient;
11+
public DebtsStore(CheckDriveApi apiClient)
912
{
10-
throw new NotImplementedException();
13+
_apiClient = apiClient ?? throw new ArgumentNullException(nameof(apiClient));
1114
}
1215

13-
public List<SelectListItem> GetEnum()
14-
{
15-
throw new NotImplementedException();
16-
}
16+
public Task<List<DebtViewModel>> GetAsync()
17+
=>_apiClient.GetAsync<List<DebtViewModel>>(resourceUrl);
18+
19+
public Task<DebtViewModel> GetByIdAsync(int id)
20+
=> _apiClient.GetAsync<DebtViewModel>($"{resourceUrl}/{id}");
21+
22+
public Task UpdateAsync(UpdateDebtRequest request)
23+
=>_apiClient.PutAsync($"{resourceUrl}/{request.Id}", request);
24+
25+
public Task DeleteAsync(int id)
26+
=> _apiClient.DeleteAsync($"{resourceUrl}/{id}");
1727
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
using CheckDrive.Web.ViewModels.Debt;
2-
using Microsoft.AspNetCore.Mvc.Rendering;
1+
using CheckDrive.Web.Requests.Debt;
2+
using CheckDrive.Web.ViewModels.Debt;
33

44
namespace CheckDrive.Web.Stores.Debts;
55

66
public interface IDebtsStore
77
{
8-
List<DebtViewModel> GetAll(string? searchText, string? status );
9-
List<SelectListItem> GetEnum();
8+
Task<List<DebtViewModel>> GetAsync();
9+
Task<DebtViewModel> GetByIdAsync(int id);
10+
Task UpdateAsync(UpdateDebtRequest viewModel);
11+
Task DeleteAsync(int id);
1012
}

CheckDrive.Web/CheckDrive.Web/ViewModels/Debt/DebtViewModel.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@ namespace CheckDrive.Web.ViewModels.Debt;
55
public class DebtViewModel
66
{
77
public int Id { get; set; }
8-
public string Driver { get; set; }
9-
public decimal DebtAmount { get; set; }
10-
public OilType Oil { get; set; }
11-
public string OilTypeText
12-
{
13-
get { return Oil.ToString(); }
14-
}
8+
public required string DriverFullName { get; set; }
9+
public decimal FuelAmount { get; set; }
10+
public decimal PaidAmount { get; set; }
11+
public int CheckPointId { get; set; }
1512
public DebtStatus Status { get; set; }
16-
1713
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
@model CheckDrive.Web.Requests.Debt.UpdateDebtRequest
2+
3+
@{
4+
ViewData["Title"] = "Update";
5+
Layout = "~/Views/Shared/_LayoutForModal.cshtml";
6+
}
7+
<h1 class="text-center mb-5 text-danger"><i class="fa fa-exclamation-circle" style="font-size:100px"></i></h1>
8+
<div class="d-flex justify-content-center align-items-center" style="height: 100%;">
9+
<h4 class="text-center text-secondary">
10+
Haqiqatan ham qarzni yopmoqchimisiz ?
11+
</h4>
12+
</div>
13+
14+
<div>
15+
<hr />
16+
<form asp-action="Edit" method="post">
17+
<div class="d-flex justify-content-center mt-5 m-2" style="gap: 10px;">
18+
<input type="hidden" name="id" value=@Model.Id>
19+
<input type="hidden" name="DriverFullName" value=@Model.DriverFullName>
20+
<input type="hidden" name="FuelAmount" value=@Model.FuelAmount>
21+
<input type="hidden" name="PaidAmount" value=@Model.PaidAmount>
22+
<input type="hidden" name="Status" value=@Model.Status>
23+
<input type="hidden" name="CheckPointId" value="@Model.CheckPointId">
24+
<button asp-action="Index" class="btn btn-secondary" style="width:120px">
25+
Yo'q
26+
</button>
27+
<button type="submit" asp-route-itemid="@Model.Id" class="btn btn-danger" style="width:120px">
28+
Ha
29+
</button>
30+
</div>
31+
</form>
32+
</div>
33+

0 commit comments

Comments
 (0)