|
|
|
@ -83,6 +83,22 @@ namespace Blazor.Services
|
|
|
|
|
await _localStorage.SetItemAsync("data", currentData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Delete(int id)
|
|
|
|
|
{
|
|
|
|
|
// Get the current data
|
|
|
|
|
var currentData = await _localStorage.GetItemAsync<List<Chapter>>("data");
|
|
|
|
|
|
|
|
|
|
// Get the chapter int the list
|
|
|
|
|
var chapter = currentData.FirstOrDefault(w => w.Id == id);
|
|
|
|
|
|
|
|
|
|
// Delete chapter in
|
|
|
|
|
currentData.Remove(chapter);
|
|
|
|
|
|
|
|
|
|
// Save the data
|
|
|
|
|
await _localStorage.SetItemAsync("data", currentData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<int> Count()
|
|
|
|
|
{
|
|
|
|
@ -115,5 +131,98 @@ namespace Blazor.Services
|
|
|
|
|
|
|
|
|
|
return (await _localStorage.GetItemAsync<Chapter[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<Administrator> GetAdminById(int id)
|
|
|
|
|
{
|
|
|
|
|
// Get the current data
|
|
|
|
|
var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data");
|
|
|
|
|
|
|
|
|
|
// Get the chapter int the list
|
|
|
|
|
var admin = currentData.FirstOrDefault(w => w.Id == id);
|
|
|
|
|
|
|
|
|
|
// Check if chapter exist
|
|
|
|
|
if (admin == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Unable to found the item with ID: {id}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return admin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Update(int id, AdministratorsModel model)
|
|
|
|
|
{
|
|
|
|
|
// Get the current data
|
|
|
|
|
var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data");
|
|
|
|
|
|
|
|
|
|
// Get the admin int the list
|
|
|
|
|
var admin = currentData.FirstOrDefault(w => w.Id == id);
|
|
|
|
|
|
|
|
|
|
// Check if admin exist
|
|
|
|
|
if (admin == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Unable to found the item with ID: {id}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Modify the content of the adminnistrator
|
|
|
|
|
admin.Username = model.Username;
|
|
|
|
|
admin.hashedPassword = model.hashedPassword;
|
|
|
|
|
|
|
|
|
|
// Save the data
|
|
|
|
|
await _localStorage.SetItemAsync("data", currentData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task Add(AdministratorsModel model)
|
|
|
|
|
{
|
|
|
|
|
// Get the current data
|
|
|
|
|
var currentData = await _localStorage.GetItemAsync<List<Administrator>>("data");
|
|
|
|
|
|
|
|
|
|
// Simulate the Id
|
|
|
|
|
model.Id = currentData.Max(s => s.Id) + 1;
|
|
|
|
|
|
|
|
|
|
// Add the chapter to the current data
|
|
|
|
|
currentData.Add(new Administrator
|
|
|
|
|
{
|
|
|
|
|
Id = model.Id,
|
|
|
|
|
Username = model.Username,
|
|
|
|
|
hashedPassword = model.hashedPassword
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Save the data
|
|
|
|
|
await _localStorage.SetItemAsync("data", currentData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<int> CountAdmin()
|
|
|
|
|
{
|
|
|
|
|
// Load data from the local storage
|
|
|
|
|
var currentData = await _localStorage.GetItemAsync<Administrator[]>("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<Administrator[]>($"{_navigationManager.BaseUri}fake-administrator.json");
|
|
|
|
|
await _localStorage.SetItemAsync("data", originalData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (await _localStorage.GetItemAsync<Administrator[]>("data")).Length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<List<Administrator>> ListAdmin(int currentPage, int pageSize)
|
|
|
|
|
{
|
|
|
|
|
// Load data from the local storage
|
|
|
|
|
var currentData = await _localStorage.GetItemAsync<Administrator[]>("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<Administrator[]>($"{_navigationManager.BaseUri}fake-administrator.json");
|
|
|
|
|
await _localStorage.SetItemAsync("data", originalData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (await _localStorage.GetItemAsync<Administrator[]>("data")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|