You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.4 KiB
133 lines
4.4 KiB
using Blazored.LocalStorage;
|
|
using HeartTrack.Models;
|
|
using Microsoft.AspNetCore.Components;
|
|
using static MudBlazor.CategoryTypes;
|
|
|
|
namespace HeartTrack.Services
|
|
{
|
|
public class DataLocalService : IDataService
|
|
{
|
|
private readonly HttpClient _http;
|
|
private readonly ILocalStorageService _localStorage;
|
|
private readonly NavigationManager _navigationManager;
|
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
|
|
|
public DataLocalService(
|
|
ILocalStorageService localStorage,
|
|
HttpClient http,
|
|
IWebHostEnvironment webHostEnvironment,
|
|
NavigationManager navigationManager)
|
|
{
|
|
_localStorage = localStorage;
|
|
_http = http;
|
|
_webHostEnvironment = webHostEnvironment;
|
|
_navigationManager = navigationManager;
|
|
}
|
|
|
|
public async Task Add(UserModel model)
|
|
{
|
|
// Get the current data
|
|
var currentData = await _localStorage.GetItemAsync<List<User>>("data");
|
|
|
|
// Simulate the Id
|
|
model.Id = currentData.Max(s => s.Id) + 1;
|
|
|
|
// Add the item to the current data
|
|
currentData.Add(new User
|
|
{
|
|
Id = model.Id,
|
|
Username = model.Username,
|
|
Nom = model.FirstName,
|
|
Prenom = model.LastName,
|
|
Email = model.Email,
|
|
Password = model.Password,
|
|
Sexe = model.Sexe,
|
|
Taille = model.Taille,
|
|
Poids = model.Poids,
|
|
BirthDate = model.BirthDate
|
|
});
|
|
|
|
// Save the data
|
|
await _localStorage.SetItemAsync("data", currentData);
|
|
}
|
|
|
|
public async Task<int> Count()
|
|
{
|
|
// Load data from the local storage
|
|
var currentData = await _localStorage.GetItemAsync<User[]>("data");
|
|
|
|
// Check if data exist in the local storage
|
|
if (currentData == null)
|
|
{
|
|
// this code add in the local storage the fake data
|
|
var originalData = await _http.GetFromJsonAsync<User[]>($"{_navigationManager.BaseUri}fake-data.json");
|
|
await _localStorage.SetItemAsync("data", originalData);
|
|
}
|
|
|
|
return (await _localStorage.GetItemAsync<User[]>("data")).Length;
|
|
}
|
|
|
|
public async Task<List<User>> List(int currentPage, int pageSize)
|
|
{
|
|
// Load data from the local storage
|
|
var currentData = await _localStorage.GetItemAsync<User[]>("data");
|
|
|
|
// Check if data exist in the local storage
|
|
if (currentData == null)
|
|
{
|
|
// this code add in the local storage the fake data
|
|
var originalData = await _http.GetFromJsonAsync<User[]>($"{_navigationManager.BaseUri}fake-data.json");
|
|
await _localStorage.SetItemAsync("data", originalData);
|
|
}
|
|
|
|
return (await _localStorage.GetItemAsync<User[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
|
|
}
|
|
|
|
public async Task Close(int id)
|
|
{
|
|
// Get the current data
|
|
var currentData = await _localStorage.GetItemAsync<List<Ticket>>("data");
|
|
|
|
// Get the item int the list
|
|
var item = currentData.FirstOrDefault(w => w.Id == id);
|
|
|
|
// Update item status
|
|
item.isCheck = true;
|
|
|
|
// Save the data
|
|
await _localStorage.SetItemAsync("data", currentData);
|
|
}
|
|
|
|
public async Task Delete(int id)
|
|
{
|
|
// Get the current data
|
|
var currentData = await _localStorage.GetItemAsync<List<Ticket>>("data");
|
|
|
|
// Get the item int the list
|
|
var item = currentData.FirstOrDefault(w => w.Id == id);
|
|
|
|
// Delete item in
|
|
currentData.Remove(item);
|
|
|
|
// Save the data
|
|
await _localStorage.SetItemAsync("data", currentData);
|
|
}
|
|
|
|
public async Task<Ticket> GetByID(int id)
|
|
{
|
|
// Get the current data
|
|
var currentData = await _localStorage.GetItemAsync<List<Ticket>>("data");
|
|
|
|
// Get the item int the list
|
|
var item = currentData.FirstOrDefault(w => w.Id == id);
|
|
|
|
// Check if item exist
|
|
if (item == null)
|
|
{
|
|
throw new Exception($"Unable to found the item with ID: {id}");
|
|
}
|
|
|
|
return item;
|
|
}
|
|
}
|
|
} |