commencement add

master
Darius BERTRAND 3 years ago
parent fe5dda5465
commit 0e9a1c6a2f

@ -0,0 +1,61 @@
@page "/AddMusique"
<h3>AddMusique</h3>
<EditForm Model=@musiqueModele OnValidSubmit="@HandleValidSubmit">
<DataAnnotationsValidator />
<ValidationSummary />
<p>
<label for="titre">
Display Titre:
<InputText id="titre" @bind-Value="musiqueModele.titre" />
</label>
</p>
<p>
<label for="auteur">
Display Auteur:
<InputText id="auteur" @bind-Value="musiqueModele.auteur" />
</label>
</p>
<p>
<label for="duree">
Display Durée:
<InputNumber id="duree" @bind-Value="musiqueModele.duree" />
</label>
</p>
<p>
<label for="genre">
Display Genre:
<InputText id="genre" @bind-Value="musiqueModele.genre" />
</label>
</p>
<p>
<label for="image">
Display Image:
<InputFile id="image" OnChange="@LoadImage" accept=".png" />
</label>
</p>
<label for="display-name">
Display Name Image :
<InputText id="display-name" @bind-Value="musiqueModele.DisplayName" />
</label>
<p>
<label for="date">
Display Date:
<InputDate id="date" @bind-Value="musiqueModele.date" />
</label>
</p>
<button type="submit">Submit</button>
</EditForm>

@ -0,0 +1,106 @@
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components;
using ProjetBlazor.Services;
using ProjetBlazor.Modeles;
namespace ProjetBlazor.Pages
{
public partial class AddMusique
{
private MusiqueModel musiqueModele = new MusiqueModel();
[Inject]
public IWebHostEnvironment WebHostEnvironment { get; set; }
[Inject]
public NavigationManager NavigationManager { get; set; }
[Inject]
public IDataService DataService { get; set; }
private async void HandleValidSubmit()
{
var currentData = await LocalStorage.GetItemAsync<List<Item>>("data");
// Simulate the Id
itemModel.Id = currentData.Max(s => s.Id) + 1;
// Add the item to the current data
currentData.Add(new Item
{
Id = MusiqueModel.id,
Titre = MusiqueModel.DisplayName,
Auteur = MusiqueModel.Name,
Duree = MusiqueModel.RepairWith,
Genre = MusiqueModel.EnchantCategories,
Image = MusiqueModel.MaxDurability,
Date =
});
// Save the image
var imagePathInfo = new DirectoryInfo($"{WebHostEnvironment.WebRootPath}/images");
// Check if the folder "images" exist
if (!imagePathInfo.Exists)
{
imagePathInfo.Create();
}
// Determine the image name
var fileName = new FileInfo($"{imagePathInfo}/{itemModel.Name}.png");
// Write the file content
await File.WriteAllBytesAsync(fileName.FullName, itemModel.ImageContent);
// Save the data
await LocalStorage.SetItemAsync("data", currentData);
}
private async Task LoadImage(InputFileChangeEventArgs e)
{
// Set the content of the image to the model
using (var memoryStream = new MemoryStream())
{
await e.File.OpenReadStream().CopyToAsync(memoryStream);
itemModel.ImageContent = memoryStream.ToArray();
}
}
private void OnEnchantCategoriesChange(string item, object checkedValue)
{
if ((bool)checkedValue)
{
if (!itemModel.EnchantCategories.Contains(item))
{
itemModel.EnchantCategories.Add(item);
}
return;
}
if (itemModel.EnchantCategories.Contains(item))
{
itemModel.EnchantCategories.Remove(item);
}
}
private void OnRepairWithChange(string item, object checkedValue)
{
if ((bool)checkedValue)
{
if (!itemModel.RepairWith.Contains(item))
{
itemModel.RepairWith.Add(item);
}
return;
}
if (itemModel.RepairWith.Contains(item))
{
itemModel.RepairWith.Remove(item);
}
}
}
}

@ -7,7 +7,7 @@
<div>
<NavLink class="btn btn-primary" href="Add" Match="NavLinkMatch.All">
<NavLink class="btn btn-primary" href="AddMusique" Match="NavLinkMatch.All">
<i class="fa fa-plus"></i> Ajouter
</NavLink>
</div>

Loading…
Cancel
Save