travail sur l'api normalement ça fonctionne

blazor
Patrick BRUGIERE 1 year ago
parent 1013a4cf8f
commit 37cf1dbd4d

@ -1,143 +0,0 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="InventoryController.cs" company="UCA Clermont-Ferrand">
// Copyright (c) UCA Clermont-Ferrand All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Minecraft.Crafting.Api.Controllers
{
using Microsoft.AspNetCore.Mvc;
using Minecraft.Crafting.Api.Models;
using System.Text.Json;
using System.Text.Json.Serialization;
/// <summary>
/// The inventory controller.
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class InventoryController : ControllerBase
{
/// <summary>
/// The json serializer options.
/// </summary>
private readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};
/// <summary>
/// Adds to inventory.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>The async task.</returns>
[HttpPost]
[Route("")]
public Task AddToInventory(InventoryModel item)
{
var data = JsonSerializer.Deserialize<List<InventoryModel>>(System.IO.File.ReadAllText("Data/inventory.json"), _jsonSerializerOptions);
if (data == null)
{
throw new Exception("Unable to get the inventory.");
}
data.Add(item);
System.IO.File.WriteAllText("Data/inventory.json", JsonSerializer.Serialize(data, _jsonSerializerOptions));
return Task.CompletedTask;
}
/// <summary>
/// Deletes from inventory.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>The async task.</returns>
[HttpDelete]
[Route("")]
public Task DeleteFromInventory(InventoryModel item)
{
if (!System.IO.File.Exists("Data/inventory.json"))
{
throw new Exception($"Unable to found the item with name: {item.ItemName}");
}
var data = JsonSerializer.Deserialize<List<InventoryModel>>(System.IO.File.ReadAllText("Data/inventory.json"), _jsonSerializerOptions);
if (data == null)
{
throw new Exception("Unable to get the inventory.");
}
var inventoryItem = data.FirstOrDefault(w => w.ItemName == item.ItemName && w.Position == item.Position);
if (inventoryItem == null)
{
throw new Exception($"Unable to found the item with name: {item.ItemName} at position: {item.Position}");
}
data.Remove(inventoryItem);
System.IO.File.WriteAllText("Data/inventory.json", JsonSerializer.Serialize(data, _jsonSerializerOptions));
return Task.CompletedTask;
}
/// <summary>
/// Gets the inventory.
/// </summary>
/// <returns>The inventory.</returns>
[HttpGet]
[Route("")]
public Task<List<InventoryModel>> GetInventory()
{
if (!System.IO.File.Exists("Data/inventory.json"))
{
return Task.FromResult(new List<InventoryModel>());
}
var data = JsonSerializer.Deserialize<List<InventoryModel>>(System.IO.File.ReadAllText("Data/inventory.json"), _jsonSerializerOptions);
if (data == null)
{
throw new Exception("Unable to get the inventory.");
}
return Task.FromResult(data);
}
/// <summary>
/// Updates the inventory.
/// </summary>
/// <param name="item">The item.</param>
/// <returns>The async task.</returns>
[HttpPut]
[Route("")]
public Task UpdateInventory(InventoryModel item)
{
var data = JsonSerializer.Deserialize<List<InventoryModel>>(System.IO.File.ReadAllText("Data/inventory.json"), _jsonSerializerOptions);
if (data == null)
{
throw new Exception("Unable to get the inventory.");
}
var inventoryItem = data.FirstOrDefault(w => w.ItemName == item.ItemName && w.Position == item.Position);
if (inventoryItem == null)
{
throw new Exception($"Unable to found the item with name: {item.ItemName} at position: {item.Position}");
}
inventoryItem.ItemName = item.ItemName;
inventoryItem.Position = item.Position;
System.IO.File.WriteAllText("Data/inventory.json", JsonSerializer.Serialize(data, _jsonSerializerOptions));
return Task.CompletedTask;
}
}
}

@ -16,7 +16,7 @@ namespace Minecraft.Crafting.Api.Controllers
/// </summary> /// </summary>
[ApiController] [ApiController]
[Route("api/[controller]")] [Route("api/[controller]")]
public class CraftingController : ControllerBase public class UserController : ControllerBase
{ {
/// <summary> /// <summary>
/// The json serializer options. /// The json serializer options.
@ -34,7 +34,7 @@ namespace Minecraft.Crafting.Api.Controllers
/// <param name="item">The item.</param> /// <param name="item">The item.</param>
/// <returns>The async task.</returns> /// <returns>The async task.</returns>
[HttpPost] [HttpPost]
[Route("")] [Route("add")]
public Task Add(User item) public Task Add(User item)
{ {
var data = JsonSerializer.Deserialize<List<User>>(System.IO.File.ReadAllText("Data/items.json"), _jsonSerializerOptions); var data = JsonSerializer.Deserialize<List<User>>(System.IO.File.ReadAllText("Data/items.json"), _jsonSerializerOptions);
@ -178,16 +178,16 @@ namespace Minecraft.Crafting.Api.Controllers
/// Gets the recipes. /// Gets the recipes.
/// </summary> /// </summary>
/// <returns>The recipes.</returns> /// <returns>The recipes.</returns>
[HttpGet] /* [HttpGet]
[Route("recipe")] [Route("recipe")]
public Task<List<Recipe>> GetRecipe() public Task<List<Recipe>> GetRecipe()
{ {
if (!System.IO.File.Exists("Data/convert-recipes.json")) if (!System.IO.File.Exists("Data/items.json"))
{ {
ResetRecipes(); ResetRecipes();
} }
var data = JsonSerializer.Deserialize<List<Recipe>>(System.IO.File.ReadAllText("Data/convert-recipes.json"), _jsonSerializerOptions); var data = JsonSerializer.Deserialize<List<Recipe>>(System.IO.File.ReadAllText("Data/items.json"), _jsonSerializerOptions);
if (data == null) if (data == null)
{ {
@ -196,6 +196,7 @@ namespace Minecraft.Crafting.Api.Controllers
return Task.FromResult(data); return Task.FromResult(data);
} }
*/
/// <summary> /// <summary>
/// Get the items with pagination. /// Get the items with pagination.
@ -237,7 +238,7 @@ namespace Minecraft.Crafting.Api.Controllers
throw new Exception("Unable to get the items."); throw new Exception("Unable to get the items.");
} }
var defaultImage = Convert.ToBase64String(System.IO.File.ReadAllBytes("Images/default.png")); var defaultImage = Convert.ToBase64String(System.IO.File.ReadAllBytes("Images/default.jpeg"));
var imageTranslation = new Dictionary<string, string> var imageTranslation = new Dictionary<string, string>
{ {
@ -299,19 +300,20 @@ namespace Minecraft.Crafting.Api.Controllers
/// Resets the recipes. /// Resets the recipes.
/// </summary> /// </summary>
/// <returns>The async task.</returns> /// <returns>The async task.</returns>
[HttpGet] /* [HttpGet]
[Route("reset-recipes")] [Route("reset-recipes")]
public Task ResetRecipes() public Task ResetRecipes()
{ {
if (!System.IO.File.Exists("Data/convert-recipes.json")) if (!System.IO.File.Exists("Data/items.json"))
{ {
System.IO.File.Delete("Data/convert-recipes.json"); System.IO.File.Delete("Data/items.json");
} }
ConvertRecipes(); ConvertRecipes();
return Task.CompletedTask; return Task.CompletedTask;
} }
*/
/// <summary> /// <summary>
/// Updates the specified identifier. /// Updates the specified identifier.
@ -332,7 +334,7 @@ namespace Minecraft.Crafting.Api.Controllers
throw new Exception($"Unable to found the item with ID: {id}"); throw new Exception($"Unable to found the item with ID: {id}");
} }
itemOriginal.Id = item.Id; //itemOriginal.Id = item.Id;
itemOriginal.Name = item.Name; itemOriginal.Name = item.Name;
itemOriginal.Surname = item.Surname; itemOriginal.Surname = item.Surname;
itemOriginal.Nickname = item.Nickname; itemOriginal.Nickname = item.Nickname;
@ -393,7 +395,7 @@ namespace Minecraft.Crafting.Api.Controllers
/// <summary> /// <summary>
/// Converts the recipes. /// Converts the recipes.
/// </summary> /// </summary>
private void ConvertRecipes() /* private void ConvertRecipes()
{ {
var data = JsonSerializer.Deserialize<List<User>>(System.IO.File.ReadAllText("Data/items.json"), _jsonSerializerOptions); var data = JsonSerializer.Deserialize<List<User>>(System.IO.File.ReadAllText("Data/items.json"), _jsonSerializerOptions);
@ -402,7 +404,7 @@ namespace Minecraft.Crafting.Api.Controllers
return; return;
} }
var recipes = Recipes.FromJson(System.IO.File.ReadAllText("Data/recipes.json")); var recipes = Recipes.FromJson(System.IO.File.ReadAllText("Data/items.json"));
var items = new List<Recipe>(); var items = new List<Recipe>();
@ -447,7 +449,8 @@ namespace Minecraft.Crafting.Api.Controllers
}); });
} }
System.IO.File.WriteAllText("Data/convert-recipes.json", JsonSerializer.Serialize(items, _jsonSerializerOptions)); System.IO.File.WriteAllText("Data/items.json", JsonSerializer.Serialize(items, _jsonSerializerOptions));
} }
*/
} }
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
@ -12,15 +12,6 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<Content Update="Data\items-original.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Data\recipes.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup> <ItemGroup>
<Content Include="Images\*.*"> <Content Include="Images\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>

@ -21,9 +21,10 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages(); builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor(); builder.Services.AddServerSideBlazor();
builder.Services.AddSingleton<WeatherForecastService>(); builder.Services.AddSingleton<WeatherForecastService>();
builder.Services.AddScoped<IDataService, DataLocalService>();
//builder.Services.AddScoped<IDataService, DataApiService>(); builder.Services.AddScoped<IDataService, DataApiService>();
builder.Services.AddScoped<IDataService, DataLocalService>();
builder.Services.AddScoped<IVocListService, VocListLocalService>(); builder.Services.AddScoped<IVocListService, VocListLocalService>();

Loading…
Cancel
Save