Mapper NETMAUI-ba

This commit is contained in:
Iván Szabó
2025-02-06 16:06:09 +01:00
parent b233347a7d
commit ac80a8a9bb
6 changed files with 39 additions and 11 deletions
@@ -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<CheckPoint, CheckPointDTO>();
CreateMap<CheckPointDTO, CheckPoint>();
}
}
}
+2
View File
@@ -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
@@ -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);
@@ -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<List<CheckPoint>>(missingItems);
_dbContext.CheckPoints.AddRange(missingCheckPoint);
await _dbContext.SaveChangesAsync();
}
}
}
catch (Exception ex)
{
@@ -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<ApiResponseDTO<UserDTO>> GetUserAsync()
@@ -57,6 +57,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="AutoMapper" Version="13.0.1" />
<PackageReference Include="CommunityToolkit.Maui" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.12">
<PrivateAssets>all</PrivateAssets>