Fix affichage des listes de vocabulaire

blazor
Antoine JOURDAIN 1 year ago
parent ff6bda4f2a
commit 0104e14baa

@ -13,10 +13,10 @@ namespace adminBlazor.Factories
{ {
return new VocabularyListModel return new VocabularyListModel
{ {
id = voc.id, Id = voc.Id,
name = voc.name, Name = voc.Name,
image = voc.image, Image = voc.Image,
aut = voc.aut Aut = voc.Aut
}; };
} }
@ -24,19 +24,19 @@ namespace adminBlazor.Factories
{ {
return new VocabularyList return new VocabularyList
{ {
id = voc.id, Id = voc.Id,
name = voc.name, Name = voc.Name,
image = voc.image, Image = voc.Image,
aut = voc.aut Aut = voc.Aut
}; };
} }
public static void Update(VocabularyList item, VocabularyListModel voc) public static void Update(VocabularyList item, VocabularyListModel voc)
{ {
if (!string.IsNullOrEmpty(voc.name)) if (!string.IsNullOrEmpty(voc.Name))
item.name = voc.name; item.Name = voc.Name;
if (!string.IsNullOrEmpty(voc.image)) if (!string.IsNullOrEmpty(voc.Image))
item.image = voc.image; item.Image = voc.Image;
} }
} }

@ -3,10 +3,10 @@ namespace adminBlazor.Models
{ {
public class VocabularyList public class VocabularyList
{ {
public int id; public int Id { get; set; }
public string name; public string Name { get; set; }
public string image; public string Image { get; set; }
public int? aut; public int? Aut { get; set; }
} }
} }

@ -6,13 +6,13 @@ namespace adminBlazor.Models
public class VocabularyListModel public class VocabularyListModel
{ {
[Required] [Required]
public int id { get; set; } public int Id { get; set; }
public string name { get; set; } public string Name { get; set; }
public string image { get; set; } public string Image { get; set; }
public int? aut { get; set; } public int? Aut { get; set; }
} }
} }

@ -11,14 +11,16 @@
<p> <p>
<label for="name"> <label for="name">
Name: Name:
<InputText id="name" @bind-Value="voc.name" /> <InputText id="name" @bind-Value="voc.Name" />
<ValidationMessage For="@(() => voc.Name)" />
</label> </label>
</p> </p>
<p> <p>
<label for="image"> <label for="image">
Image: Image:
<InputText id="image" @bind-Value="voc.image" /> <InputText id="image" @bind-Value="voc.Image" />
<ValidationMessage For="@(() => voc.Image)" />
</label> </label>
</p> </p>

@ -16,16 +16,16 @@
ShowPager ShowPager
Responsive> Responsive>
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.id)" Caption="id" /> <DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.Id)" Caption="id" />
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.name)" Caption="Name" /> <DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.Name)" Caption="Name" />
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.image)" Caption="Image" /> <DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.Image)" Caption="Image" />
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.aut)" Caption="Author ID"> <DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.Aut)" Caption="Author ID">
<DisplayTemplate> <DisplayTemplate>
@if (context is VocabularyList voc) @if (context is VocabularyList voc)
{ {
@if (voc.aut != null) @if (voc.Aut != null)
{ {
@voc.aut @voc.Aut
} }
else else
{ {
@ -35,9 +35,9 @@
</DisplayTemplate> </DisplayTemplate>
</DataGridColumn> </DataGridColumn>
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.id)" Caption="Modifier"> <DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.Id)" Caption="Modifier">
<DisplayTemplate> <DisplayTemplate>
<a href="editVoc/@(context.id)" class="btn btn-primary"><i class="fa fa-edit"></i> Editer</a> <a href="editVoc/@(context.Id)" class="btn btn-primary"><i class="fa fa-edit"></i> Editer</a>
</DisplayTemplate> </DisplayTemplate>
</DataGridColumn> </DataGridColumn>
</DataGrid> </DataGrid>

@ -31,7 +31,7 @@ namespace adminBlazor.Pages
public IVocListService VocListService { get; set; } public IVocListService VocListService { get; set; }
protected async Task OnAfterRenderAsync(bool firstRender) /*protected async Task OnAfterRenderAsync(bool firstRender)
{ {
// Do not treat this action if is not the first render // Do not treat this action if is not the first render
if (firstRender) if (firstRender)
@ -48,7 +48,7 @@ namespace adminBlazor.Pages
var originalData = Http.GetFromJsonAsync<VocabularyList[]>($"{NavigationManager.BaseUri}voc.json").Result; var originalData = Http.GetFromJsonAsync<VocabularyList[]>($"{NavigationManager.BaseUri}voc.json").Result;
await LocalStorage.SetItemAsync("voc", originalData); await LocalStorage.SetItemAsync("voc", originalData);
} }
} }*/
private async Task OnReadData(DataGridReadDataEventArgs<VocabularyList> e) private async Task OnReadData(DataGridReadDataEventArgs<VocabularyList> e)
{ {
@ -59,7 +59,7 @@ namespace adminBlazor.Pages
// When you use a real API, we use this follow code // When you use a real API, we use this follow code
//var response = await Http.GetJsonAsync<Data[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" ); //var response = await Http.GetJsonAsync<Data[]>( $"http://my-api/api/data?page={e.Page}&pageSize={e.PageSize}" );
//var response = (await LocalStorage.GetItemAsync<User[]>("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList(); //var response = (await LocalStorage.GetItemAsync<VocabularyList[]>("voc")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
if (!e.CancellationToken.IsCancellationRequested) if (!e.CancellationToken.IsCancellationRequested)
{ {

@ -34,7 +34,7 @@ namespace adminBlazor.Services
{ {
var currentList = await _localStorage.GetItemAsync<List<VocabularyList>>("voc"); var currentList = await _localStorage.GetItemAsync<List<VocabularyList>>("voc");
model.id = currentList.Max(s => s.id) + 1; model.Id = currentList.Max(s => s.Id) + 1;
currentList.Add(VocListFactory.Create(model)); currentList.Add(VocListFactory.Create(model));
@ -74,14 +74,40 @@ namespace adminBlazor.Services
public async Task<VocabularyList> GetById(int id) public async Task<VocabularyList> GetById(int id)
{ {
var currentLists = await _localStorage.GetItemAsync<List<VocabularyList>>("voc"); var currentLists = await _localStorage.GetItemAsync<List<VocabularyList>>("voc");
return new VocabularyList(); var list = currentLists.FirstOrDefault(w => w.Id == id);
if (list == null)
{
throw new Exception($"Unable to found the item with ID: {id}");
}
return list;
} }
public Task Update(int id, VocabularyListModel model) public async Task Update(int id, VocabularyListModel model)
{ {
throw new NotImplementedException(); // Get the current data
var currentList = await _localStorage.GetItemAsync<List<VocabularyList>>("voc");
var voc = currentList.FirstOrDefault(w => w.Id == id);
if (voc == null)
{
throw new Exception($"Unable to found the item with ID: {id}");
}
// Save the image
//
VocListFactory.Update(voc, model);
// Modify the content of the item
// Save the data
await _localStorage.SetItemAsync("voc", currentList);
} }
public Task Delete(int id) public Task Delete(int id)

Loading…
Cancel
Save