|
|
|
@ -27,24 +27,21 @@ namespace MyFirstBlazor.Services
|
|
|
|
|
|
|
|
|
|
public async Task Add(ItemModel model)
|
|
|
|
|
{
|
|
|
|
|
// Get the current data
|
|
|
|
|
var currentData = await _localStorage.GetItemAsync<List<Item>>("data");
|
|
|
|
|
|
|
|
|
|
// Save the image
|
|
|
|
|
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
|
|
|
|
|
|
|
|
|
|
// Check if the folder "images" exist
|
|
|
|
|
if (!imagePathInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
imagePathInfo.Create();
|
|
|
|
|
}
|
|
|
|
|
// Simulate the Id
|
|
|
|
|
model.Id = currentData.Max(s => s.Id) + 1;
|
|
|
|
|
|
|
|
|
|
// Determine the image name
|
|
|
|
|
var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
|
|
|
|
|
// Add the item to the current data
|
|
|
|
|
currentData.Add(ItemFactory.Create(model));
|
|
|
|
|
|
|
|
|
|
// Write the file content
|
|
|
|
|
await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
|
|
|
|
|
// Save the data
|
|
|
|
|
await _localStorage.SetItemAsync("data", currentData);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<int> Count()
|
|
|
|
|
{
|
|
|
|
|
return (await _localStorage.GetItemAsync<Item[]>("data")).Length;
|
|
|
|
@ -81,41 +78,27 @@ namespace MyFirstBlazor.Services
|
|
|
|
|
|
|
|
|
|
return item;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Update(int id, ItemModel model)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Get the current data
|
|
|
|
|
var currentData = await _localStorage.GetItemAsync<List<Item>>("data");
|
|
|
|
|
|
|
|
|
|
// Save the image
|
|
|
|
|
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
|
|
|
|
|
// Get the item int the list
|
|
|
|
|
var item = currentData.FirstOrDefault(w => w.Id == id);
|
|
|
|
|
|
|
|
|
|
// Check if the folder "images" exist
|
|
|
|
|
if (!imagePathInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
imagePathInfo.Create();
|
|
|
|
|
}
|
|
|
|
|
// Delete the previous image
|
|
|
|
|
if (item.Name != model.Name)
|
|
|
|
|
// Check if item exist
|
|
|
|
|
if (item == null)
|
|
|
|
|
{
|
|
|
|
|
var oldFileName = new FileInfo($"{imagePathInfo}/{item.Name}.png");
|
|
|
|
|
|
|
|
|
|
if (oldFileName.Exists)
|
|
|
|
|
{
|
|
|
|
|
File.Delete(oldFileName.FullName);
|
|
|
|
|
}
|
|
|
|
|
throw new Exception($"Unable to found the item with ID: {id}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine the image name
|
|
|
|
|
var fileName = new FileInfo($"{imagePathInfo}/{model.Name}.png");
|
|
|
|
|
|
|
|
|
|
// Write the file content
|
|
|
|
|
await File.WriteAllBytesAsync(fileName.FullName, model.ImageContent);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Modify the content of the item
|
|
|
|
|
ItemFactory.Update(item, model);
|
|
|
|
|
|
|
|
|
|
// Save the data
|
|
|
|
|
await _localStorage.SetItemAsync("data", currentData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task Delete(int id)
|
|
|
|
|
{
|
|
|
|
|
// Get the current data
|
|
|
|
@ -127,15 +110,6 @@ namespace MyFirstBlazor.Services
|
|
|
|
|
// Delete item in
|
|
|
|
|
currentData.Remove(item);
|
|
|
|
|
|
|
|
|
|
// Delete the image
|
|
|
|
|
var imagePathInfo = new DirectoryInfo($"{_webHostEnvironment.WebRootPath}/images");
|
|
|
|
|
var fileName = new FileInfo($"{imagePathInfo}/{item.Name}.png");
|
|
|
|
|
|
|
|
|
|
if (fileName.Exists)
|
|
|
|
|
{
|
|
|
|
|
File.Delete(fileName.FullName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Save the data
|
|
|
|
|
await _localStorage.SetItemAsync("data", currentData);
|
|
|
|
|
}
|
|
|
|
|