From ac80a8a9bb8b71c307c2ece6818df02ad4a4c19e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Szab=C3=B3?= Date: Thu, 6 Feb 2025 16:06:09 +0100 Subject: [PATCH] Mapper NETMAUI-ba --- .../Mapper/MapperProfile.cs | 21 +++++++++++++++++++ src/WorkFlowCheck.MAUI/MauiProgram.cs | 2 ++ .../Services/BaseService.cs | 7 +++++-- .../Services/SyncService.cs | 16 +++++++------- .../Services/UserService.cs | 3 ++- .../WorkFlowCheck.MAUI.csproj | 1 + 6 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 src/WorkFlowCheck.MAUI/Mapper/MapperProfile.cs diff --git a/src/WorkFlowCheck.MAUI/Mapper/MapperProfile.cs b/src/WorkFlowCheck.MAUI/Mapper/MapperProfile.cs new file mode 100644 index 0000000..6fa42d2 --- /dev/null +++ b/src/WorkFlowCheck.MAUI/Mapper/MapperProfile.cs @@ -0,0 +1,21 @@ +using AutoMapper; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using WorkFlowCheck.Common.DTO; +using WorkFlowCheck.MAUI.DataLayer.Entities; + +namespace WorkFlowCheck.MAUI.Mapper +{ + public class MapperProfile:Profile + { + public MapperProfile() + { + CreateMap(); + CreateMap(); + + } + } +} diff --git a/src/WorkFlowCheck.MAUI/MauiProgram.cs b/src/WorkFlowCheck.MAUI/MauiProgram.cs index 7331a03..36a1f0a 100644 --- a/src/WorkFlowCheck.MAUI/MauiProgram.cs +++ b/src/WorkFlowCheck.MAUI/MauiProgram.cs @@ -5,6 +5,7 @@ using Microsoft.Extensions.Http; using Microsoft.Extensions.DependencyInjection; using CommunityToolkit.Maui; using WorkFlowCheck.MAUI.DataLayer; +using WorkFlowCheck.MAUI.Mapper; namespace WorkFlowCheck.MAUI { @@ -13,6 +14,7 @@ namespace WorkFlowCheck.MAUI public static MauiApp CreateMauiApp() { var builder = MauiApp.CreateBuilder(); + builder.Services.AddAutoMapper(typeof(MapperProfile)); // appsettings.json beállítása string jsonFilePath = Path.Combine(FileSystem.AppDataDirectory, "appsettings.json"); // Másold a fájlt a Raw erőforrásokból az AppDataDirectory-ba, ha még nincs ott diff --git a/src/WorkFlowCheck.MAUI/Services/BaseService.cs b/src/WorkFlowCheck.MAUI/Services/BaseService.cs index e9bc425..a7166ae 100644 --- a/src/WorkFlowCheck.MAUI/Services/BaseService.cs +++ b/src/WorkFlowCheck.MAUI/Services/BaseService.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Configuration; +using AutoMapper; +using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; @@ -14,12 +15,14 @@ namespace WorkFlowCheck.MAUI.Services public readonly HttpClient _httpClient; public readonly AppDbContext _dbContext; public readonly IConfiguration _configuration; + public readonly IMapper _mapper; - public BaseService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext) + public BaseService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext, IMapper mapper) { _httpClient = httpClient; _dbContext = dbContext; _configuration = configuration; + _mapper = mapper; var apiBaseUrl = configuration["ApiBaseUrl"]; // API-cím elérése a konfigurációból _httpClient.BaseAddress = new Uri(apiBaseUrl); diff --git a/src/WorkFlowCheck.MAUI/Services/SyncService.cs b/src/WorkFlowCheck.MAUI/Services/SyncService.cs index 76115c8..85fa797 100644 --- a/src/WorkFlowCheck.MAUI/Services/SyncService.cs +++ b/src/WorkFlowCheck.MAUI/Services/SyncService.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using AutoMapper; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; @@ -10,12 +11,13 @@ using System.Text; using System.Threading.Tasks; using WorkFlowCheck.Common.DTO; using WorkFlowCheck.MAUI.DataLayer; +using WorkFlowCheck.MAUI.DataLayer.Entities; namespace WorkFlowCheck.MAUI.Services { public class SyncService : BaseService, ISyncService { - public SyncService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext) : base(httpClient, configuration, dbContext) + public SyncService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext, IMapper mapper) : base(httpClient, configuration, dbContext, mapper) { } @@ -39,14 +41,12 @@ namespace WorkFlowCheck.MAUI.Services var checkPointList = await _dbContext.CheckPoints.ToListAsync(); var missingItems = response.Data.CheckPointList.Where(f => !checkPointList.Any(s => s.Id == f.Id)).ToList(); - foreach (var item in missingItems) { - - } + var missingCheckPoint = _mapper.Map>(missingItems); + + _dbContext.CheckPoints.AddRange(missingCheckPoint); + await _dbContext.SaveChangesAsync(); } } - - - } catch (Exception ex) { diff --git a/src/WorkFlowCheck.MAUI/Services/UserService.cs b/src/WorkFlowCheck.MAUI/Services/UserService.cs index c5e8684..b3af58f 100644 --- a/src/WorkFlowCheck.MAUI/Services/UserService.cs +++ b/src/WorkFlowCheck.MAUI/Services/UserService.cs @@ -11,13 +11,14 @@ using WorkFlowCheck.Common.DTO; using System.Text.Json.Serialization; using Newtonsoft.Json; using WorkFlowCheck.MAUI.DataLayer; +using AutoMapper; namespace WorkFlowCheck.MAUI.Services { public class UserService : BaseService, IUserService { public UserDTO? CurrentUser { get; private set; } - public UserService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext) : base(httpClient, configuration, dbContext) + public UserService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext, IMapper mapper) : base(httpClient, configuration, dbContext, mapper) { } public async Task> GetUserAsync() diff --git a/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj b/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj index 8ba26c8..f6961e5 100644 --- a/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj +++ b/src/WorkFlowCheck.MAUI/WorkFlowCheck.MAUI.csproj @@ -57,6 +57,7 @@ + all