diff --git a/src/WorkFlowCheck.API/Controllers/SyncController.cs b/src/WorkFlowCheck.API/Controllers/SyncController.cs index 8985703..8bdedbb 100644 --- a/src/WorkFlowCheck.API/Controllers/SyncController.cs +++ b/src/WorkFlowCheck.API/Controllers/SyncController.cs @@ -1,4 +1,5 @@ using Microsoft.AspNetCore.Mvc; +using Serilog; using WorkFlowCheck.BL.Services; using WorkFlowCheck.BL.Services.Interfaces; using WorkFlowCheck.Common.DTO; @@ -14,7 +15,7 @@ namespace WorkFlowCheck.API.Controllers { _syncService = syncService; } - + [HttpGet("GetAllCheckPoints")] public async Task>> GetAllCheckpointsAsync() { @@ -26,9 +27,9 @@ namespace WorkFlowCheck.API.Controllers if (checkPointListDTO != null) { - - retVal.IsSuccess = true; - retVal.Data = checkPointListDTO; + + retVal.IsSuccess = true; + retVal.Data = checkPointListDTO; } else { @@ -38,7 +39,7 @@ namespace WorkFlowCheck.API.Controllers return retVal; } - + [HttpGet("GetCheckPoints/{id}")] public async Task> GetAllCheckpointsAsync(int id) { @@ -134,5 +135,27 @@ namespace WorkFlowCheck.API.Controllers return retVal; } + + [HttpPost("authenticate")] + public async Task> UpdateCheckPoint([FromBody] CheckPointDTO checkPointDTO) + { + var retVal = new ApiResponseDTO() + { + IsSuccess = true + }; + + try + { + var result = await _syncService.UpdateCheckPointAsync(checkPointDTO); + retVal.Data = result; + + } + catch (Exception ex) + { + retVal.IsSuccess = false; + Log.Error(ex.Message); + } + return retVal; + } } } diff --git a/src/WorkFlowCheck.BL/Services/Interfaces/ISyncService.cs b/src/WorkFlowCheck.BL/Services/Interfaces/ISyncService.cs index 50ab50e..620c83e 100644 --- a/src/WorkFlowCheck.BL/Services/Interfaces/ISyncService.cs +++ b/src/WorkFlowCheck.BL/Services/Interfaces/ISyncService.cs @@ -7,9 +7,10 @@ namespace WorkFlowCheck.BL.Services.Interfaces { Task> GetAllCheckPointAsync(); Task GetCheckPointAsync(int id); + Task UpdateCheckPointAsync(CheckPointDTO checkPointDTO); Task> GetAllLocationAsync(); Task> GetAllEquipmentAsync(); - + Task> GetAllCheckListTemplateAsync(); } } diff --git a/src/WorkFlowCheck.BL/Services/SyncService.cs b/src/WorkFlowCheck.BL/Services/SyncService.cs index 57b4d55..a0d9ede 100644 --- a/src/WorkFlowCheck.BL/Services/SyncService.cs +++ b/src/WorkFlowCheck.BL/Services/SyncService.cs @@ -66,6 +66,41 @@ namespace WorkFlowCheck.BL.Services return retVal; } + public async Task UpdateCheckPointAsync(CheckPointDTO checkPointDTO) + { + var retVal = new CheckPointDTO(); + + try + { + if (checkPointDTO.Id == 0) //Hozzáadás, ha Id == 0 + { + var checkPoint = _mapper.Map(checkPointDTO); + _dbContext.CheckPoints.Add(checkPoint); + await _dbContext.SaveChangesAsync(); + + retVal = _mapper.Map(checkPoint); + } + else + { + var res = await _dbContext.CheckPoints.Where(w => w.Id == checkPointDTO.Id).FirstOrDefaultAsync(); + if (res != null) + { + res.ShortName = checkPointDTO.ShortName; + res.Code = checkPointDTO.Code; + + await _dbContext.SaveChangesAsync(); + + retVal = _mapper.Map(res); + } + } + } + catch (Exception ex) + { + Log.Error(ex.Message); + } + + return retVal; + } public async Task> GetAllLocationAsync() { var retVal = new List(); diff --git a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml index bf2f677..ae1d20a 100644 --- a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml +++ b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml @@ -1,36 +1,44 @@ @page @model WorkFlowCheck.Web.Pages.BaseStock.CheckPoints.CheckPointEditPageModel +@using Microsoft.AspNetCore.Antiforgery +@inject IAntiforgery Antiforgery @{ ViewData["Title"] = "Ellenőrzési pont"; var checkpointId = Model.CheckPoint.Id; var url = Url.Page("./CheckPointEditPage", "LoadCheckListTemplateRows", new { id = checkpointId }); + var urlPost = Url.Page("./CheckPointEditPage", "Save"); }

@ViewData["Title"]

+ + +
+
+ -
- -
+
-
+
- +
+ +
-
- -
-
+
+
+ +
@@ -97,5 +105,34 @@ const row = table.row($(this).closest('tr')).data(); }); + $("#saveCheckPoint").click(function () { + const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); + var formData = getFormAsNestedObject('#checkPointForm'); + + console.log(formData); + console.log(JSON.stringify(formData.CheckPoint)); + + $.ajax({ + url: $('#checkPointEditPostUrl').val(), + type: 'POST', + headers: { + 'X-CSRF-TOKEN': csrfToken, + 'Content-Type': 'application/json' + }, + data: JSON.stringify(formData.CheckPoint), + success: function (response) { + // Sikeres válasz esetén + alert("Sikeres mentés!"); + // opcionálisan pl. form reset, redirect stb. + }, + error: function (xhr, status, error) { + // Hiba esetén + console.error("Hiba: ", error); + alert("Mentés nem sikerült."); + } + }); + + }); + } diff --git a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs index dc7206a..a81a42c 100644 --- a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs +++ b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointEditPage.cshtml.cs @@ -1,12 +1,14 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; +using Serilog; using WorkFlowCheck.Common.DTO; using WorkFlowCheck.Web.Services.Interfaces; namespace WorkFlowCheck.Web.Pages.BaseStock.CheckPoints { [Authorize] + [ValidateAntiForgeryToken] public class CheckPointEditPageModel : PageModel { private readonly ILogger _logger; @@ -30,5 +32,21 @@ namespace WorkFlowCheck.Web.Pages.BaseStock.CheckPoints CheckPoint = await _baseStockService.GetCheckPoint(id); return new JsonResult(new { data = CheckPoint.CheckListTemplateRowDTO }); } + + public async Task OnPostSave([FromBody] CheckPointDTO checkPointDTO) + { + + try + { + var result = await _baseStockService.UpdateCheckPoint(checkPointDTO); + return new JsonResult(new { success = true }); + } + catch (Exception ex) + { + Log.Error(ex.Message); + } + + return new JsonResult(new { success = false }); + } } } diff --git a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml index 71c14d2..785062e 100644 --- a/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml +++ b/src/WorkFlowCheck.Web/Pages/BaseStock/CheckPoints/CheckPointsPage.cshtml @@ -4,10 +4,11 @@ ViewData["Title"] = "Ellenőrzési pontok"; }

@ViewData["Title"]

-
- -
-
+ +
+
+ +
diff --git a/src/WorkFlowCheck.Web/Program.cs b/src/WorkFlowCheck.Web/Program.cs index 1f5e4d4..ff24eee 100644 --- a/src/WorkFlowCheck.Web/Program.cs +++ b/src/WorkFlowCheck.Web/Program.cs @@ -26,6 +26,10 @@ builder.Host.UseSerilog(); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddSingleton(); +builder.Services.AddAntiforgery(options => +{ + options.HeaderName = "X-CSRF-TOKEN"; // JS API hívásokhoz +}); var jwtSettings = builder.Configuration.GetSection("Jwt"); diff --git a/src/WorkFlowCheck.Web/wwwroot/js/site.js b/src/WorkFlowCheck.Web/wwwroot/js/site.js index 0937657..c98a6d8 100644 --- a/src/WorkFlowCheck.Web/wwwroot/js/site.js +++ b/src/WorkFlowCheck.Web/wwwroot/js/site.js @@ -2,3 +2,30 @@ // for details on configuring this project to bundle and minify static web assets. // Write your JavaScript code. +function getFormAsNestedObject(formSelector) { + var formArray = $(formSelector).serializeArray(); + var result = {}; + + formArray.forEach(function (item) { + var keys = item.name.split('.'); + var value = item.value; + var current = result; + + for (var i = 0; i < keys.length; i++) { + var key = keys[i]; + + // Ha utolsó kulcs, értéket rendelünk hozzá + if (i === keys.length - 1) { + current[key] = value; + } else { + // Ha a következÅ‘ szint nincs meg, hozzuk létre + if (!current[key]) { + current[key] = {}; + } + current = current[key]; + } + } + }); + + return result; +} \ No newline at end of file