TourIAm/TIAMWebApp/Client/Services/UserDataService.cs

148 lines
4.3 KiB
C#

using Microsoft.EntityFrameworkCore.Infrastructure;
using Newtonsoft.Json;
using System.Net.Http.Json;
using System.Text;
using TIAMWebApp.Shared.Application.Interfaces;
using TIAMWebApp.Shared.Application.Models;
using TIAMWebApp.Shared.Application.Models.PageModels;
namespace TIAMWebApp.Client.Services
{
public class UserDataService : IUserDataService
{
private readonly HttpClient http;
public User? User { get; set; } = new User("","","");
public Dictionary<int, string> userRoleTypes { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
public UserDataService(HttpClient http)
{
this.http = http;
}
public List<RoleType> roleTypes = new List<RoleType>
{
new RoleType { Id = 1, RoleName = "Login" },
new RoleType { Id = 2, RoleName = "Member" },
new RoleType { Id = 4, RoleName = "Vip" },
new RoleType { Id = 8, RoleName = "Uvip" },
new RoleType { Id = 16, RoleName = "Volunteer" },
new RoleType { Id = 32, RoleName = "Guide" },
new RoleType { Id = 64, RoleName = "Protector" },
new RoleType { Id = 128, RoleName = "Admin" },
new RoleType { Id = 256, RoleName = "SuperAdmin" },
new RoleType { Id = 512, RoleName = "God" }
};
public async Task<User> IsLoggedInAsync()
{
if (User == null)
{
User = new User("","","");
User.IsLoggedIn = false;
User.UserType = UserType.User;
return User;
}
else
{
return User;
}
}
//Mock method for now
public async Task<User> AuthorizeUserAsync(int userType)
{
if (User == null)
{
User = new User("", "", "");
}
//simply return true for now
User.IsLoggedIn = true;
User.UserType = (UserType)userType;
return User;
}
public async Task<string> TestUserApi(int Param) {
var url = APIUrls.UserTest;
var response = await http.PostAsJsonAsync(url, Param);
var result = await response.Content.ReadAsStringAsync();
return result;
}
public async Task<string> AuthenticateUser(LoginModel loginModel)
{
string result = string.Empty;
var url = APIUrls.AuthenticateUser;
var response = await http.PostAsJsonAsync(url, loginModel);
if(response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsStringAsync();
}
else
{
result = await response.Content.ReadAsStringAsync();
}
//result = await response.Content.ReadAsStringAsync();
return result;
}
public async Task<(bool isSuccess, string ErrorMessage)> CreateUser(RegistrationModel regModel)
{
bool isSuccess = true;
string result = string.Empty;
var url = APIUrls.CreateUser;
var response = await http.PostAsJsonAsync(url, regModel);
result = await response.Content.ReadAsStringAsync();
/*if (response.IsSuccessStatusCode)
{
isSuccess = true;
result = await response.Content.ReadAsStringAsync();
}
else
{
isSuccess = false;
result = await response.Content.ReadAsStringAsync();
}*/
return (isSuccess, result);
}
public Task<Dictionary<int, string>> GetUserRolesAsync(User user)
{
//get the user's roles
int role = User.UserRoles;
foreach (var roleType in roleTypes)
{
if ((role & roleType.Id) == roleType.Id)
{
//add the role to the dictionary
userRoleTypes.Add(roleType.Id, roleType.RoleName);
}
}
return Task.FromResult(userRoleTypes);
}
}
}