159 lines
5.4 KiB
C#
159 lines
5.4 KiB
C#
using System.Net.Http;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Net.Http.Json;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using WorkFlowCheck.Common.DTO;
|
|
using System.Text.Json.Serialization;
|
|
using Newtonsoft.Json;
|
|
using WorkFlowCheck.MAUI.DataLayer;
|
|
using AutoMapper;
|
|
using WorkFlowCheck.MAUI.Services.Interfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using WorkFlowCheck.MAUI.Handlers;
|
|
|
|
namespace WorkFlowCheck.MAUI.Services
|
|
{
|
|
public class UserService : BaseService, IUserService
|
|
{
|
|
public UserDTO? CurrentUser { get; private set; }
|
|
public UserService(HttpClient httpClient, IConfiguration configuration, AppDbContext dbContext, IMapper mapper) : base(httpClient, configuration, dbContext, mapper)
|
|
{
|
|
}
|
|
|
|
public async Task<ApiResponseDTO<UserDTO>> GetUserAsync(int id)
|
|
{
|
|
try
|
|
{
|
|
await PrepareAuthenticatedRequestAsync();
|
|
|
|
string endpoint = $"{_httpClient.BaseAddress}api/GetUser/{id}";
|
|
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<UserDTO>>(endpoint);
|
|
|
|
return response ?? new ApiResponseDTO<UserDTO>
|
|
{
|
|
IsSuccess = false,
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Hiba visszaadása
|
|
return new ApiResponseDTO<UserDTO>
|
|
{
|
|
IsSuccess = false
|
|
};
|
|
}
|
|
}
|
|
public async Task<ApiResponseDTO<List<UserDTO>>> GetAllUserAsync()
|
|
{
|
|
try
|
|
{
|
|
await PrepareAuthenticatedRequestAsync();
|
|
|
|
string endpoint = $"{_httpClient.BaseAddress}api/GetAllUser";
|
|
var response = await _httpClient.GetFromJsonAsync<ApiResponseDTO<List<UserDTO>>>(endpoint);
|
|
|
|
return response ?? new ApiResponseDTO<List<UserDTO>>
|
|
{
|
|
IsSuccess = false,
|
|
};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
return new ApiResponseDTO<List<UserDTO>>
|
|
{
|
|
IsSuccess = false
|
|
};
|
|
}
|
|
}
|
|
public async Task<ApiResponseDTO<UserDTO>> Authenticate(string username, string password)
|
|
{
|
|
try
|
|
{
|
|
// Az API végpont meghatározása
|
|
string endpoint = $"{_httpClient.BaseAddress}api/user/authenticate";
|
|
|
|
var userSimpleDTO = new UserSimpleDTO()
|
|
{
|
|
UserName = username,
|
|
Password = password,
|
|
|
|
};
|
|
|
|
// HTTP POST kérés küldése
|
|
|
|
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, userSimpleDTO))
|
|
{
|
|
httpResponseMessage.EnsureSuccessStatusCode();
|
|
|
|
var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
|
|
var response = JsonConvert.DeserializeObject<ApiResponseDTO<UserDTO>>(jsonString);
|
|
|
|
return response ?? new ApiResponseDTO<UserDTO>
|
|
{
|
|
IsSuccess = false,
|
|
};
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Hiba visszaadása
|
|
return new ApiResponseDTO<UserDTO>
|
|
{
|
|
IsSuccess = false
|
|
};
|
|
}
|
|
}
|
|
public async Task<ApiResponseDTO<string>> SyncUsers_Up(Action<double, string> reportProgress)
|
|
{
|
|
var retVal = new ApiResponseDTO<string>();
|
|
try
|
|
{
|
|
await PrepareAuthenticatedRequestAsync();
|
|
|
|
var userList = await _dbContext.Users.ToListAsync();
|
|
|
|
var userListDTO = _mapper.Map<List<UserDTO>>(userList);
|
|
var endpoint = $"{_httpClient.BaseAddress}api/Sync/UpdateAllUser";
|
|
|
|
var json = JsonConvert.SerializeObject(userListDTO);
|
|
var jsonBytes = Encoding.UTF8.GetBytes(json);
|
|
var jsonStream = new MemoryStream(jsonBytes);
|
|
|
|
var progressContent = new ProgressableStreamContent(jsonStream, 8192, (uploaded, total) =>
|
|
{
|
|
double percentage = (double)uploaded / total * 100;
|
|
reportProgress(percentage, "Beküldés...");
|
|
});
|
|
|
|
progressContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");
|
|
|
|
using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsync(endpoint, progressContent))
|
|
{
|
|
httpResponseMessage.EnsureSuccessStatusCode();
|
|
var jsonString = await httpResponseMessage.Content.ReadAsStringAsync();
|
|
retVal = JsonConvert.DeserializeObject<ApiResponseDTO<string>>(jsonString);
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
retVal.IsSuccess = false;
|
|
retVal.Errors.Add(ex.Message);
|
|
}
|
|
return retVal;
|
|
}
|
|
public void SetCurrentUser(UserDTO user)
|
|
{
|
|
CurrentUser = user;
|
|
}
|
|
|
|
public UserDTO? GetCurrentUser() => CurrentUser;
|
|
}
|
|
}
|