Retail_manager/RMWPFInterfaceLibrary/Api/UserEndPoint.cs

80 lines
2.4 KiB
C#
Raw Normal View History

using RMWPFInterfaceLibrary.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace RMWPFInterfaceLibrary.Api
{
public class UserEndPoint : IUserEndPoint
{
private readonly IAPIHelper _apiHelper;
public UserEndPoint(IAPIHelper apiHelper)
{
_apiHelper = apiHelper;
}
public async Task<List<UserModel>> GetAll()
{
using (HttpResponseMessage response = await _apiHelper.ApiClient.GetAsync("/api/User/Admin/GetAllUsers"))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<List<UserModel>>();
return result;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
2022-08-22 00:12:27 +02:00
public async Task<Dictionary<string,string>> GetAllRoles()
{
using (HttpResponseMessage response = await _apiHelper.ApiClient.GetAsync("/api/User/Admin/GetAllRoles"))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<Dictionary<string,string>>();
return result;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
public async Task AddUserToRole(string userId, string roleName)
{
var data = new { userId, roleName };
using (HttpResponseMessage response = await _apiHelper.ApiClient.PostAsJsonAsync("/api/User/Admin/AddRole", data))
{
if (response.IsSuccessStatusCode == false)
{
throw new Exception(response.ReasonPhrase);
}
}
}
public async Task RemoveUserFromRole(string userId, string roleName)
{
var data = new { userId, roleName };
using (HttpResponseMessage response = await _apiHelper.ApiClient.PostAsJsonAsync("/api/User/Admin/RemoveRole", data))
{
if (response.IsSuccessStatusCode == false)
{
throw new Exception(response.ReasonPhrase);
}
}
}
}
}