travail api

blazor
Patrick BRUGIERE 1 year ago
commit ff6bda4f2a

@ -1,14 +1,23 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:51530",
"sslPort": 44598
}
},
"profiles": {
"Minecraft.Crafting.Api": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7234;http://localhost:5234",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7234;http://localhost:5234"
"dotnetRunMessages": true
},
"IIS Express": {
"commandName": "IISExpress",
@ -22,17 +31,9 @@
"commandName": "Docker",
"launchBrowser": true,
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
"publishAllPorts": true,
"useSSL": true
}
},
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:51530",
"sslPort": 44598
"environmentVariables": {},
"useSSL": true,
"publishAllPorts": true
}
}
}

@ -6,9 +6,10 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup Condition=" '$(RunConfiguration)' == 'Minecraft.Crafting.Api' " />
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>

@ -0,0 +1,45 @@
using adminBlazor.Models;
using Blazorise;
using Blazorise.Extensions;
using System.Data;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace adminBlazor.Factories
{
public static class VocListFactory
{
public static VocabularyListModel ToModel(VocabularyList voc)
{
return new VocabularyListModel
{
id = voc.id,
name = voc.name,
image = voc.image,
aut = voc.aut
};
}
public static VocabularyList Create(VocabularyListModel voc)
{
return new VocabularyList
{
id = voc.id,
name = voc.name,
image = voc.image,
aut = voc.aut
};
}
public static void Update(VocabularyList item, VocabularyListModel voc)
{
if (!string.IsNullOrEmpty(voc.name))
item.name = voc.name;
if (!string.IsNullOrEmpty(voc.image))
item.image = voc.image;
}
}
}

@ -0,0 +1,12 @@
using System;
namespace adminBlazor.Models
{
public class Translation
{
private int id;
private string word1;
private string word2;
private int listVoc;
}
}

@ -0,0 +1,12 @@
using System;
namespace adminBlazor.Models
{
public class VocabularyList
{
public int id;
public string name;
public string image;
public int? aut;
}
}

@ -0,0 +1,18 @@
using System;
using System.ComponentModel.DataAnnotations;
namespace adminBlazor.Models
{
public class VocabularyListModel
{
[Required]
public int id { get; set; }
public string name { get; set; }
public string image { get; set; }
public int? aut { get; set; }
}
}

@ -0,0 +1,26 @@
@page "/editVoc/{Id:int}"
@using adminBlazor.Models
<h3>Edit</h3>
<h4>Voc id : @Id</h4>
<EditForm Model="@voc" OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<p>
<label for="name">
Name:
<InputText id="name" @bind-Value="voc.name" />
</label>
</p>
<p>
<label for="image">
Image:
<InputText id="image" @bind-Value="voc.image" />
</label>
</p>
<button type="submit">Submit</button>
</EditForm>

@ -0,0 +1,44 @@
using adminBlazor.Factories;
using adminBlazor.Models;
using adminBlazor.Services;
using Blazored.LocalStorage;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
namespace adminBlazor.Pages
{
public partial class EditVoc
{
[Parameter]
public int Id { get; set; }
[Inject]
public IVocListService VocListService { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
public VocabularyList currVoc;
private VocabularyListModel voc = new VocabularyListModel();
private async void HandleValidSubmit()
{
await VocListService.Update(Id, voc);
NavigationManager.NavigateTo("list");
}
protected async Task OnInitializedAsync()
{
var item = await VocListService.GetById(Id);
voc = VocListFactory.ToModel(item);
}
}
}

@ -0,0 +1,43 @@
@page "/voc"
@using adminBlazor.Models
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.4/css/all.css">
<link href="_content/Blazorise/blazorise.css" rel="stylesheet" />
<link href="_content/Blazorise.Bootstrap/blazorise.bootstrap.css" rel="stylesheet" />
<h3>Vocabulary Lists</h3>
<DataGrid TItem="VocabularyList"
Data="@_vocList"
ReadData="@OnReadData"
TotalItems="@totalVocList"
PageSize="10"
ShowPager
Responsive>
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.id)" Caption="id" />
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.name)" Caption="Name" />
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.image)" Caption="Image" />
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.aut)" Caption="Author ID">
<DisplayTemplate>
@if (context is VocabularyList voc)
{
@if (voc.aut != null)
{
@voc.aut
}
else
{
<span>No author</span>
}
}
</DisplayTemplate>
</DataGridColumn>
<DataGridColumn TItem="VocabularyList" Field="@nameof(VocabularyList.id)" Caption="Modifier">
<DisplayTemplate>
<a href="editVoc/@(context.id)" class="btn btn-primary"><i class="fa fa-edit"></i> Editer</a>
</DisplayTemplate>
</DataGridColumn>
</DataGrid>

@ -0,0 +1,71 @@
using adminBlazor.Modals;
using adminBlazor.Models;
using adminBlazor.Services;
using Blazored.LocalStorage;
using Blazored.Modal;
using Blazored.Modal.Services;
using Blazorise.DataGrid;
using Microsoft.AspNetCore.Components;
namespace adminBlazor.Pages
{
public partial class Voc
{
private List<VocabularyList> _vocList;
private int totalVocList;
[Inject]
public HttpClient Http { get; set; }
[Inject]
public ILocalStorageService LocalStorage { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[CascadingParameter]
public IModalService Modal { get; set; }
[Inject]
public IVocListService VocListService { get; set; }
protected async Task OnAfterRenderAsync(bool firstRender)
{
// Do not treat this action if is not the first render
if (firstRender)
{
return;
}
var currentData = await LocalStorage.GetItemAsync<VocabularyList[]>("voc");
// Check if data exist in the local storage
if (currentData == null)
{
// this code add in the local storage the fake data (we load the data sync for initialize the data before load the OnReadData method)
var originalData = Http.GetFromJsonAsync<VocabularyList[]>($"{NavigationManager.BaseUri}voc.json").Result;
await LocalStorage.SetItemAsync("voc", originalData);
}
}
private async Task OnReadData(DataGridReadDataEventArgs<VocabularyList> e)
{
if (e.CancellationToken.IsCancellationRequested)
{
return;
}
// 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 LocalStorage.GetItemAsync<User[]>("data")).Skip((e.Page - 1) * e.PageSize).Take(e.PageSize).ToList();
if (!e.CancellationToken.IsCancellationRequested)
{
_vocList = await VocListService.List(e.Page, e.PageSize);
totalVocList = await VocListService.Count();// an actual data for the current page
}
}
}
}

@ -29,7 +29,12 @@ builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddScoped<IDataService, DataLocalService>();
<<<<<<< HEAD
builder.Services.AddScoped<IDataService, DataApiService>();
=======
builder.Services.AddScoped<IVocListService, VocListLocalService>();
//builder.Services.AddScoped<IDataService, DataApiService>();
>>>>>>> f62a4611bcbbfe0ed449a38e75f10df8ce6a5bbe
builder.Services.AddHttpClient();
builder.Services.AddBlazoredLocalStorage();

@ -0,0 +1,25 @@
using adminBlazor.Models;
using Microsoft.AspNetCore.Hosting;
using Blazored.LocalStorage;
using adminBlazor.Factories;
using Microsoft.AspNetCore.Components;
namespace adminBlazor.Services
{
public interface IVocListService
{
Task Add(VocabularyListModel model);
Task<int> Count();
Task<List<VocabularyList>> List(int currentPage, int pageSize);
Task<VocabularyList> GetById(int id);
Task Update(int id, VocabularyListModel model);
Task Delete(int id);
}
}

@ -0,0 +1,92 @@
using adminBlazor.Factories;
using adminBlazor.Models;
using Blazored.LocalStorage;
using Blazorise.Extensions;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Hosting;
namespace adminBlazor.Services
{
public class VocListLocalService : IVocListService
{
private readonly HttpClient _http;
private readonly ILocalStorageService _localStorage;
private readonly NavigationManager _navigationManager;
private readonly IWebHostEnvironment _webHostEnvironment;
public VocListLocalService(
ILocalStorageService localStorage,
HttpClient http,
IWebHostEnvironment webHostEnvironment,
NavigationManager navigationManager)
{
_localStorage = localStorage;
_http = http;
_webHostEnvironment = webHostEnvironment;
_navigationManager = navigationManager;
}
public VocListLocalService(ILocalStorageService localStorage)
{
_localStorage = localStorage; // Assure-toi que LocalStorage est initialisé correctement ici
}
public async Task Add(VocabularyListModel model)
{
var currentList = await _localStorage.GetItemAsync<List<VocabularyList>>("voc");
model.id = currentList.Max(s => s.id) + 1;
currentList.Add(VocListFactory.Create(model));
await _localStorage.SetItemAsync("voc", currentList);
}
public async Task<int> Count()
{
var currentList = await _localStorage.GetItemAsync<VocabularyList[]>("voc");
// Check if data exist in the local storage
if (currentList == null)
{
// this code add in the local storage the fake data
var originalList = await _http.GetFromJsonAsync<VocabularyList[]>($"{_navigationManager.BaseUri}voc.json");
await _localStorage.SetItemAsync("voc", originalList);
}
return (await _localStorage.GetItemAsync<VocabularyList[]>("voc")).Length;
}
public async Task<List<VocabularyList>> List(int currentPage, int pageSize)
{
var currentList = await _localStorage.GetItemAsync<VocabularyList[]>("voc");
// Check if data exist in the local storage
if (currentList == null)
{
// this code add in the local storage the fake data
var originalData = await _http.GetFromJsonAsync<VocabularyList[]>($"{_navigationManager.BaseUri}voc.json");
await _localStorage.SetItemAsync("voc", originalData);
}
return (await _localStorage.GetItemAsync<VocabularyList[]>("voc")).Skip((currentPage - 1) * pageSize).Take(pageSize).ToList();
}
public async Task<VocabularyList> GetById(int id)
{
var currentLists = await _localStorage.GetItemAsync<List<VocabularyList>>("voc");
return new VocabularyList();
}
public Task Update(int id, VocabularyListModel model)
{
throw new NotImplementedException();
}
public Task Delete(int id)
{
throw new NotImplementedException();
}
}
}

@ -0,0 +1,14 @@
[
{
"id": 1,
"name": "Entreprise",
"image": "link",
"aut": null
},
{
"id": 2,
"name": "Animaux",
"image": "link",
"aut" : 1
}
]
Loading…
Cancel
Save