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> GetUserAsync(int id) { try { await PrepareAuthenticatedRequestAsync(); string endpoint = $"{_httpClient.BaseAddress}api/GetUser/{id}"; var response = await _httpClient.GetFromJsonAsync>(endpoint); return response ?? new ApiResponseDTO { IsSuccess = false, }; } catch (Exception ex) { // Hiba visszaadása return new ApiResponseDTO { IsSuccess = false }; } } public async Task>> GetAllUserAsync() { try { await PrepareAuthenticatedRequestAsync(); string endpoint = $"{_httpClient.BaseAddress}api/GetAllUser"; var response = await _httpClient.GetFromJsonAsync>>(endpoint); return response ?? new ApiResponseDTO> { IsSuccess = false, }; } catch (Exception ex) { return new ApiResponseDTO> { IsSuccess = false }; } } public async Task> 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>(jsonString); return response ?? new ApiResponseDTO { IsSuccess = false, }; } } catch (Exception ex) { // Hiba visszaadása return new ApiResponseDTO { IsSuccess = false }; } } public async Task> SyncUsers_Up(Action reportProgress) { var retVal = new ApiResponseDTO(); try { await PrepareAuthenticatedRequestAsync(); var userList = await _dbContext.Users.ToListAsync(); var userListDTO = _mapper.Map>(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>(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; } }