using Newtonsoft.Json; using Serilog; using WorkFlowCheck.Common.DTO; using WorkFlowCheck.Web.Services.Interfaces; namespace WorkFlowCheck.Web.Services { public class UserService : BaseService, IUserService { public UserService(HttpClient httpClient, IConfiguration configuration, IHttpContextAccessor httpContextAccessor) : base(httpClient, configuration, httpContextAccessor) { } public async Task GetUser(int id) { string endpoint = $"{_httpClient.BaseAddress}api/User/GetUser/{id}"; var retVal = new UserDTO() { RoleDTO = new List() }; try { var response = await _httpClient.GetFromJsonAsync>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task> GetAllUsers() { string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllUsers"; var retVal = new List(); try { var response = await _httpClient.GetFromJsonAsync>>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task> UpdateUser(UserDTO userDTO) { try { string endpoint = $"{_httpClient.BaseAddress}api/user/updateUser"; using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, userDTO)) { 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> 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, }; 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> Authenticate2F1(string username, string password) { try { // Az API végpont meghatározása string endpoint = $"{_httpClient.BaseAddress}api/User/Authenticate2F1"; var userSimpleDTO = new UserSimpleDTO() { UserName = username, Password = password, }; 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> Authenticate2F2(string userName, string password, string code, string token2FA) { try { string endpoint = $"{_httpClient.BaseAddress}api/User/Authenticate2F2"; var userSimpleDTO = new User2FADTO() { UserName = userName, Password = password, Code = code, Token2FA = token2FA }; 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) { return new ApiResponseDTO { IsSuccess = false }; } } public async Task> Authenticate2F2ChangePassword(string username, string oldpassword, string newpassword, string code, string token2FA) { try { string endpoint = $"{_httpClient.BaseAddress}api/User/Authenticate2F2ChangePassword"; var userSimpleDTO = new UserChangePassword2FADTO() { UserName = username, OldPassword = oldpassword, NewPassword1 = newpassword, NewPassword2 = newpassword, Code = code, Token2FA = token2FA }; 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) { return new ApiResponseDTO { IsSuccess = false }; } } public async Task DeleteUser(int id) { string endpoint = $"{_httpClient.BaseAddress}api/User/DeleteUser/{id}"; var retVal = false; try { var response = await _httpClient.GetFromJsonAsync>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task GetRole(int id) { string endpoint = $"{_httpClient.BaseAddress}api/User/GetRole/{id}"; var retVal = new RoleDTO(); try { var response = await _httpClient.GetFromJsonAsync>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task> GetAllRoles() { string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllRoles"; var retVal = new List(); try { var response = await _httpClient.GetFromJsonAsync>>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task> UpdateRole(RoleDTO roleDTO) { try { string endpoint = $"{_httpClient.BaseAddress}api/user/updateRole"; using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, roleDTO)) { 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 DeleteRole(int id) { string endpoint = $"{_httpClient.BaseAddress}api/User/DeleteRole/{id}"; var retVal = false; try { var response = await _httpClient.GetFromJsonAsync>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task GetUserRole(int id) { string endpoint = $"{_httpClient.BaseAddress}api/user/GetUserRole/{id}"; var retVal = new UserRoleDTO(); try { var response = await _httpClient.GetFromJsonAsync>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task> GetAllUserRoles() { string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllUserRoles"; var retVal = new List(); try { var response = await _httpClient.GetFromJsonAsync>>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task> UpdateUserRole(UserRoleDTO userRoleDTO) { try { string endpoint = $"{_httpClient.BaseAddress}api/user/updateUserRole"; using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, userRoleDTO)) { 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 GetRoleCheckListTemplateHeader(int id) { string endpoint = $"{_httpClient.BaseAddress}api/user/GetRoleCheckListTemplateHeader/{id}"; var retVal = new RoleCheckListTemplateHeaderDTO(); try { var response = await _httpClient.GetFromJsonAsync>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task> GetAllRoleCheckListTemplates() { string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllRoleCheckListTemplateHeaders"; var retVal = new List(); try { var response = await _httpClient.GetFromJsonAsync>>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task> UpdateRoleCheckListTemplateHeader(RoleCheckListTemplateHeaderDTO roleCheckListTemplateHeaderDTO) { try { string endpoint = $"{_httpClient.BaseAddress}api/user/UpdateRoleCheckListTemplateHeader"; using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, roleCheckListTemplateHeaderDTO)) { 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 GetRoleCheckPoint(int id) { string endpoint = $"{_httpClient.BaseAddress}api/user/GetRoleCheckPoint/{id}"; var retVal = new RoleCheckPointDTO(); try { var response = await _httpClient.GetFromJsonAsync>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task> GetAllRoleCheckPoints() { string endpoint = $"{_httpClient.BaseAddress}api/User/GetAllRoleCheckPoints"; var retVal = new List(); try { var response = await _httpClient.GetFromJsonAsync>>(endpoint); if (response != null) { if (response.IsSuccess) { return response.Data; } } } catch (Exception ex) { Log.Error(ex.Message); } return retVal; } public async Task> UpdateRoleCheckPoint(RoleCheckPointDTO roleCheckPointDTO) { try { string endpoint = $"{_httpClient.BaseAddress}api/user/UpdateRoleCheckPoint"; using (HttpResponseMessage httpResponseMessage = await _httpClient.PostAsJsonAsync(endpoint, roleCheckPointDTO)) { 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 }; } } } }