60 lines
1.9 KiB
C#
60 lines
1.9 KiB
C#
using AutoMapper;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http;
|
|
using System.Net.Http.Json;
|
|
using System.Security.Cryptography.X509Certificates;
|
|
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, IMapper mapper) : base(httpClient, configuration, dbContext, mapper)
|
|
{
|
|
|
|
}
|
|
public async Task SyncDatas()
|
|
{
|
|
await SyncCheckPoints();
|
|
}
|
|
public async Task SyncCheckPoints()
|
|
{
|
|
try
|
|
{
|
|
// Az API végpont meghatározása
|
|
string endpoint = $"{_httpClient.BaseAddress}api/Sync/GetAllCheckPoints";
|
|
|
|
// HTTP GET kérés küldése
|
|
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<CheckPointListDTO>>(endpoint);
|
|
if (response != null)
|
|
{
|
|
if (response.IsSuccess)
|
|
{
|
|
var checkPointList = await _dbContext.CheckPoints.ToListAsync();
|
|
var missingItems = response.Data.CheckPointList.Where(f => !checkPointList.Any(s => s.Id == f.Id)).ToList();
|
|
|
|
var missingCheckPoint = _mapper.Map<List<CheckPoint>>(missingItems);
|
|
|
|
_dbContext.CheckPoints.AddRange(missingCheckPoint);
|
|
await _dbContext.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Hiba visszaadása
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|