User továbbgondolása

This commit is contained in:
2025-03-17 12:10:41 +01:00
parent 0e06382eee
commit 8c1e60889d
11 changed files with 306 additions and 44 deletions
@@ -26,15 +26,15 @@
<form method="post">
<div class="mb-3">
<label asp-for="UserDTO.UserName" class="form-label">Felhasználónév</label>
<input asp-for="UserDTO.UserName" class="form-control" />
<span asp-validation-for="UserDTO.UserName" class="text-danger small"></span>
<label asp-for="UserSimpleDTO.UserName" class="form-label">Felhasználónév</label>
<input asp-for="UserSimpleDTO.UserName" class="form-control" />
<span asp-validation-for="UserSimpleDTO.UserName" class="text-danger small"></span>
</div>
<div class="mb-3">
<label asp-for="UserDTO.Password" class="form-label">Jelszó</label>
<input asp-for="UserDTO.Password" type="password" class="form-control" />
<span asp-validation-for="UserDTO.Password" class="text-danger small"></span>
<label asp-for="UserSimpleDTO.Password" class="form-label">Jelszó</label>
<input asp-for="UserSimpleDTO.Password" type="password" class="form-control" />
<span asp-validation-for="UserSimpleDTO.Password" class="text-danger small"></span>
</div>
<div class="d-grid">
@@ -12,7 +12,7 @@ namespace WorkFlowCheck.Web.Pages.Account
private readonly IUserService _userService;
[BindProperty]
public UserDTO UserDTO { get; set; }
public UserDTO UserSimpleDTO { get; set; }
public string ErrorMessage { get; set; }
public LoginModel(IUserService userService)
@@ -25,7 +25,7 @@ namespace WorkFlowCheck.Web.Pages.Account
}
public async Task<IActionResult> OnPostAsync()
{
var response = await _userService.Authenticate(UserDTO.UserName, UserDTO.Password);
var response = await _userService.Authenticate(UserSimpleDTO.UserName, UserSimpleDTO.Password);
if (response.IsSuccess)
{
@@ -141,7 +141,11 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/pdfmake.min.js" integrity="sha384-VFQrHzqBh5qiJIU0uGU5CIW3+OWpdGGJM9LBnGbuIH2mkICcFZ7lPd/AAtI7SNf7" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.2.7/vfs_fonts.js" integrity="sha384-/RlQG9uf0M2vcTw3CX7fbqgbj/h8wKxw7C3zu9/GxcBPRKOEcESxaxufwRXqzq6n" crossorigin="anonymous"></script>
<script src="https://cdn.datatables.net/v/bs5/jq-3.7.0/jszip-3.10.1/dt-2.2.2/af-2.7.0/b-3.2.2/b-colvis-3.2.2/b-html5-3.2.2/b-print-3.2.2/cr-2.0.4/date-1.5.5/fc-5.0.4/fh-4.0.1/kt-2.12.1/r-3.0.4/rg-1.5.1/rr-1.5.0/sc-2.4.3/sb-1.8.2/sp-2.3.3/sl-3.0.0/sr-1.4.1/datatables.min.js" integrity="sha384-10kTwhFyUU637a6/7q0kLBdo8jQWjxteg63DT/K8Sdq/nCDaDAkH+Nq/MIrsp8wc" crossorigin="anonymous"></script>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation-unobtrusive@4.0.0/dist/jquery.validate.unobtrusive.min.js"></script>
@await RenderSectionAsync("Scripts", required: false)
<partial name="_ValidationScriptsPartial" />
</body>
</html>
@@ -1,4 +1,83 @@
@page
@model WorkFlowCheck.Web.Pages.UserAndRole.UserEditPageModel
@using Microsoft.AspNetCore.Antiforgery
@inject IAntiforgery Antiforgery
@{
ViewData["Title"] = "Ellenőrzési pont";
var UserId = Model.User.Id;
var urlPost = Url.Page("./UserEditPage", "Save");
}
<h1>@ViewData["Title"]</h1>
<input type="hidden" id="UserEditPostUrl" value="@urlPost" />
<div class="card shadow p-4">
<form method="post" id="UserForm">
<meta name="csrf-token" content="@Antiforgery.GetAndStoreTokens(HttpContext).RequestToken" />
<input type="hidden" asp-for="User.Id" />
<div class="mb-3">
<label asp-for="User.UserName"></label>
<input asp-for="User.UserName" class="form-control" />
<span asp-validation-for="User.UserName" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="User.FirstName"></label>
<input asp-for="User.FirstName" class="form-control" />
<span asp-validation-for="User.FirstName" class="text-danger"></span>
</div>
<div class="mb-3">
<label asp-for="User.LastName"></label>
<input asp-for="User.LastName" class="form-control" />
<span asp-validation-for="User.LastName" class="text-danger"></span>
</div>
<div class="mb-3 d-flex justify-content-between">
<button type="button" id="saveUser" class="btn btn-primary">Mentés</button>
<button type="button" class="btn btn-secondary" onclick="history.back()">Mégsem</button>
</div>
</form>
</div>
@section Scripts {
<script>
$("#saveUser").click(function () {
const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');
var formData = getFormAsNestedObject('#UserForm');
const $form = $('#UserForm');
console.log($form);
if ($form[0].checkValidity())
{
$.ajax({
url: $('#UserEditPostUrl').val(),
type: 'POST',
headers: {
'X-CSRF-TOKEN': csrfToken,
'Content-Type': 'application/json'
},
data: JSON.stringify(formData.User),
success: function (response) {
showMessageModal({
title: 'Figyelmem!',
message: 'Sikeres mentés.',
okText: 'Értettem'
});
},
error: function (xhr, status, error) {
// Hiba esetén
console.error("Hiba: ", error);
showMessageModal({
title: 'Hiba!',
message: 'A mentés NEM sikerült!',
okText: 'Értettem'
});
}
});
} else
{
$form[0].reportValidity();
}
});
</script>
}
@@ -1,12 +1,27 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;
namespace WorkFlowCheck.Web.Pages.UserAndRole
{
public class UserEditPageModel : PageModel
{
public void OnGet()
private readonly ILogger<IndexModel> _logger;
private readonly IUserService _userService;
[BindProperty]
public UserDTO User { get; set; }
public UserEditPageModel(ILogger<IndexModel> logger, IUserService userService)
{
_userService = userService;
_logger = logger;
}
public async Task OnGet(int id)
{
User = await _userService.GetUser(id);
}
}
}
@@ -1,4 +1,99 @@
@page
@model WorkFlowCheck.Web.Pages.UserAndRole.UserPageModel
@{
ViewData["Title"] = "Felhasználók";
}
<h1>@ViewData["Title"]</h1>
<div class="card shadow p-4">
<div class="d-flex justify-content-end">
<button id="newUserBtn" class="btn btn-primary float-right new-btn">Új elem hozzáadása</button>
</div>
<table id="tbUsersPage" class="table table-bordered table-hover table-sm" style="width:100%">
<thead class="table-primary">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>E-mail</th>
<th class="text-center">Action</th>
</tr>
</thead>
<tfoot class="table-light">
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>User Name</th>
<th>E-mail</th>
<th>Action</th>
</tr>
</tfoot>
</table>
</div>
@section Scripts {
<script>
const table = new DataTable('#tbUsersPage', {
ajax: {
url: "@Url.Page("./UserPage", "LoadUsers")",
type: "GET",
dataSrc : "data"
},
columns: [
{ data: "id" },
{ data: "firstName" },
{ data: "lastName" },
{ data: "userName" },
{ data: "email" },
{ data: null, render: function (data, type, row) {
return `<button class="btn btn-primary edit-btn" data-id="${row.id}">Módosítás</button>
<button class="btn btn-danger delete-btn" data-id="${row.id}">Törlés</button>
`;
}}
],
columnDefs: [
{
"targets": 0,
"visible": false
},
{
"targets": 5,
"className": "text-center",
"width": "15%"
}
],
processing:true
});
$('#newUserBtn').on('click', function ()
{
console.log('New button clicked!"');
window.location.href = `@Url.Page("./UserEditPage")?id=0`;
});
$('#tbUsersPage').on('click', '.edit-btn', function ()
{
const row = table.row($(this).closest('tr')).data();
window.location.href = `@Url.Page("./UserEditPage")?id=${row.id}`;
});
$('#tbUsersPage').on('click', '.delete-btn', function ()
{
const row = table.row($(this).closest('tr')).data();
console.log(row);
showConfirmModal({
title: 'Törlés megerősítése',
message: 'Biztosan törölni szeretnéd ezt az elemet?',
okText: 'Törlés',
cancelText: 'Mégsem'
}).then(function(result) {
if(result === 'ok') {
console.log('Törlés végrehajtva');
}
});
});
</script>
}
@@ -1,12 +1,26 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using WorkFlowCheck.Web.Services;
using WorkFlowCheck.Web.Services.Interfaces;
namespace WorkFlowCheck.Web.Pages.UserAndRole
{
public class UserPageModel : PageModel
{
public void OnGet()
private readonly ILogger<IndexModel> _logger;
private readonly IUserService _userService;
public UserPageModel(ILogger<IndexModel> logger, IUserService userService)
{
_logger = logger;
_userService = userService;
}
public async Task OnGet()
{
}
public async Task<JsonResult> OnGetLoadUsers()
{
var results = await _userService.GetAllUsers();
return new JsonResult(new { data = results });
}
}
}
@@ -4,18 +4,18 @@ namespace WorkFlowCheck.Web.Services.Interfaces
{
public interface IUserService
{
Task<ApiRequestDTO<UserDTO>> GetUser(int id);
Task<ApiRequestDTO<List<UserDTO>>> GetAllUsers();
Task<ApiResponseDTO<UserDTO>> UpdateUser(UserDTO user);
Task<UserDTO> GetUser(int id);
Task<List<UserDTO>> GetAllUsers();
Task<UserDTO> UpdateUser(UserDTO user);
Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password);
Task<ApiRequestDTO<RoleDTO>> GetRole(int id);
Task<ApiRequestDTO<List<RoleDTO>>> GetAllRoles();
Task<ApiResponseDTO<RoleDTO>> UpdateRole(RoleDTO role);
Task<RoleDTO> GetRole(int id);
Task<List<RoleDTO>> GetAllRoles();
Task<RoleDTO> UpdateRole(RoleDTO role);
Task<ApiRequestDTO<UserRoleDTO>> GetUserRole(int id);
Task<ApiRequestDTO<List<UserDTO>>> GetAllUserRoles();
Task<ApiResponseDTO<UserRoleDTO>> UpdateUserRole(UserRoleDTO userRole);
Task<UserRoleDTO> GetUserRole(int id);
Task<List<UserDTO>> GetAllUserRoles();
Task<UserRoleDTO> UpdateUserRole(UserRoleDTO userRole);
}
+59 -11
View File
@@ -1,5 +1,6 @@
using Newtonsoft.Json;
using Serilog;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.Web.Services.Interfaces;
@@ -15,23 +16,17 @@ namespace WorkFlowCheck.Web.Services
try
{
// Az API végpont meghatározása
string endpoint = $"{_httpClient.BaseAddress}api/user/authenticate";
string endpoint = $"{_httpClient.BaseAddress}api/User/Authenticate";
var userDTO = new UserDTO()
var userSimpleDTO = new UserSimpleDTO()
{
Email = "",
FirstName = "",
LastName = "",
Id = 0,
UserName = username,
Password = password,
JwtToken = "",
RoleDTO = new List<RoleDTO>(),
};
// HTTP POST kérés küldése
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, userDTO))
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, userSimpleDTO))
{
httpResponseMessage.EnsureSuccessStatusCode();
@@ -53,5 +48,58 @@ namespace WorkFlowCheck.Web.Services
};
}
}
public Task<ApiRequestDTO<List<RoleDTO>>> GetAllRoles() => throw new NotImplementedException();
public Task<ApiRequestDTO<List<UserDTO>>> GetAllUserRoles() => throw new NotImplementedException();
public async Task<List<UserDTO>> GetAllUsers()
{
string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllUsers";
var retVal = new List<UserDTO>();
try
{
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<UserDTO>>>(endpoint);
if (response != null)
{
if (response.IsSuccess)
{
return response.Data;
}
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
public Task<RoleDTO> GetRole(int id) => throw new NotImplementedException();
public async Task<UserDTO> GetUser(int id)
{
string endpoint = $"{_httpClient.BaseAddress}api/User/GetUser/{id}";
var retVal = new UserDTO() { RoleDTO = new List<RoleDTO>() };
try
{
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<UserDTO>>(endpoint);
if (response != null)
{
if (response.IsSuccess)
{
return response.Data;
}
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
public Task<UserRoleDTO> GetUserRole(int id) => throw new NotImplementedException();
public Task<RoleDTO> UpdateRole(RoleDTO role) => throw new NotImplementedException();
public Task<UserDTO> UpdateUser(UserDTO user) => throw new NotImplementedException();
public Task<UserRoleDTO> UpdateUserRole(UserRoleDTO userRole) => throw new NotImplementedException();
Task<List<RoleDTO>> IUserService.GetAllRoles() => throw new NotImplementedException();
Task<List<UserDTO>> IUserService.GetAllUserRoles() => throw new NotImplementedException();
}
}