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.
61 lines
1.3 KiB
61 lines
1.3 KiB
![]()
2 years ago
|
---
|
||
|
sidebar_position: 2
|
||
|
title: Changing the data service
|
||
|
---
|
||
|
|
||
|
In order to take into account the deletion of an element, we will first add this functionality to our data service.
|
||
|
|
||
|
## Add method to interface
|
||
|
|
||
|
Open the `Services/IDataService.cs` file and add the highlighted item:
|
||
|
|
||
|
```csharp title="Services/IDataService.cs"
|
||
|
public interface IDataService
|
||
|
{
|
||
|
Task Add(ItemModel model);
|
||
|
|
||
|
Task<int> Count();
|
||
|
|
||
|
Task<List<Item>> List(int currentPage, int pageSize);
|
||
|
|
||
|
Task<Item> GetById(int id);
|
||
|
|
||
|
Task Update(int id, ItemModel model);
|
||
|
|
||
|
// highlight-start
|
||
|
Task Delete(int id);
|
||
|
// highlight-end
|
||
|
}
|
||
|
```
|
||
|
|
||
|
## Add method to implementation
|
||
|
|
||
|
Open the `Services/DataLocalService.cs` file and add the following method:
|
||
|
|
||
|
```csharp title="Services/DataLocalService.cs"
|
||
|
...
|
||
|
|
||
|
public async Task Delete(int id)
|
||
|
{
|
||
|
// Get the current data
|
||
|
var currentData = await _localStorage.GetItemAsync<List<Item>>("data");
|
||
|
|
||
|
// Get the item int the list
|
||
|
var item = currentData.FirstOrDefault(w => w.Id == id);
|
||
|
|
||
|
// 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);
|
||
|
}
|
||
|
```
|