|
| 1 | +using CheckDrive.ApiContracts.Account; |
| 2 | +using CheckDrive.ApiContracts.Role; |
| 3 | +using CheckDrive.Web.Stores.Accounts; |
| 4 | +using CheckDrive.Web.Stores.Roles; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using Microsoft.AspNetCore.Mvc.Rendering; |
| 7 | + |
| 8 | +namespace CheckDrive.Web.Controllers |
| 9 | +{ |
| 10 | + public class AccountsController : Controller |
| 11 | + { |
| 12 | + private readonly IAccountDataStore _accountDataStore; |
| 13 | + private readonly IRoleDataStore _roleStore; |
| 14 | + public AccountsController(IAccountDataStore accountDataStore, IRoleDataStore roleDataStore) |
| 15 | + { |
| 16 | + _roleStore = roleDataStore; |
| 17 | + _accountDataStore = accountDataStore; |
| 18 | + } |
| 19 | + |
| 20 | + public async Task<IActionResult> Index(string? searchString, int? roleId, DateTime? birthDate, int? pageNumber) |
| 21 | + { |
| 22 | + var accounts = await _accountDataStore.GetAccountsAsync(searchString, roleId, birthDate, pageNumber); |
| 23 | + |
| 24 | + var roles = await GETRoles(); |
| 25 | + |
| 26 | + roles.Insert(0, new RoleDto |
| 27 | + { |
| 28 | + Id = 0, |
| 29 | + Name = "Barcha ishchilar", |
| 30 | + }); |
| 31 | + var selectedRole = roles[0]; |
| 32 | + |
| 33 | + if (roleId.HasValue && roleId != 0) |
| 34 | + { |
| 35 | + selectedRole = roles.FirstOrDefault(x => x.Id == roleId); |
| 36 | + } |
| 37 | + |
| 38 | + ViewBag.Accounts = accounts.Data; |
| 39 | + ViewBag.Roles = roles; |
| 40 | + |
| 41 | + ViewBag.PageSize = accounts.PageSize; |
| 42 | + ViewBag.PageCount = accounts.TotalPages; |
| 43 | + ViewBag.TotalCount = accounts.TotalCount; |
| 44 | + ViewBag.CurrentPage = accounts.PageNumber; |
| 45 | + ViewBag.HasPreviousPage = accounts.HasPreviousPage; |
| 46 | + ViewBag.HasNextPage = accounts.HasNextPage; |
| 47 | + |
| 48 | + ViewBag.SearchString = searchString; |
| 49 | + ViewBag.CurrentRoleId = roleId; |
| 50 | + ViewBag.SelectedRole = selectedRole; |
| 51 | + |
| 52 | + return View(); |
| 53 | + } |
| 54 | + public async Task<IActionResult> Details(int id) |
| 55 | + { |
| 56 | + var account = await _accountDataStore.GetAccountAsync(id); |
| 57 | + if (account == null) |
| 58 | + { |
| 59 | + return NotFound(); |
| 60 | + } |
| 61 | + return View(account); |
| 62 | + } |
| 63 | + public async Task<IActionResult> Create() |
| 64 | + { |
| 65 | + var roles = await GETRoles(); |
| 66 | + ViewBag.Roles = new SelectList(roles, "Id", "Name"); |
| 67 | + return View(); |
| 68 | + } |
| 69 | + [HttpPost] |
| 70 | + [ValidateAntiForgeryToken] |
| 71 | + public async Task<IActionResult> Create([Bind("Login,Password,PhoneNumber,FirstName,LastName,Bithdate,RoleId")] |
| 72 | + AccountForCreateDto account) |
| 73 | + { |
| 74 | + if (ModelState.IsValid) |
| 75 | + { |
| 76 | + await _accountDataStore.CreateAccountAsync(account); |
| 77 | + return RedirectToAction(nameof(Index)); |
| 78 | + } |
| 79 | + var roles = await GETRoles(); |
| 80 | + ViewBag.Roles = new SelectList(roles, "Id", "Name"); |
| 81 | + return View(account); |
| 82 | + } |
| 83 | + |
| 84 | + public async Task<IActionResult> Edit(int id) |
| 85 | + { |
| 86 | + var account = await _accountDataStore.GetAccountAsync(id); |
| 87 | + if (account == null) |
| 88 | + { |
| 89 | + return NotFound(); |
| 90 | + } |
| 91 | + |
| 92 | + return View(account); |
| 93 | + } |
| 94 | + |
| 95 | + [HttpPost] |
| 96 | + [ValidateAntiForgeryToken] |
| 97 | + public async Task<IActionResult> Edit(int id, [Bind("Id,Login,Password,PhoneNumber,FirstName,LastName,Bithdate,RoleId")] |
| 98 | + AccountForUpdateDto account) |
| 99 | + { |
| 100 | + if (ModelState.IsValid) |
| 101 | + { |
| 102 | + await _accountDataStore.UpdateAccountAsync(id, account); |
| 103 | + return RedirectToAction(nameof(Index)); |
| 104 | + } |
| 105 | + return View(account); |
| 106 | + } |
| 107 | + |
| 108 | + public async Task<IActionResult> Delete(int id) |
| 109 | + { |
| 110 | + var account = await _accountDataStore.GetAccountAsync(id); |
| 111 | + if (account == null) |
| 112 | + { |
| 113 | + return NotFound(); |
| 114 | + } |
| 115 | + return View(account); |
| 116 | + } |
| 117 | + |
| 118 | + [HttpPost, ActionName("Delete")] |
| 119 | + [ValidateAntiForgeryToken] |
| 120 | + public async Task<IActionResult> DeleteConfirmed(int id) |
| 121 | + { |
| 122 | + await _accountDataStore.DeleteAccountAsync(id); |
| 123 | + return RedirectToAction(nameof(Index)); |
| 124 | + } |
| 125 | + |
| 126 | + private async Task<List<RoleDto>> GETRoles() |
| 127 | + { |
| 128 | + var roleResponse = await _roleStore.GetRoles(); |
| 129 | + var roles = roleResponse.Data.ToList(); |
| 130 | + return roles; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments