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>
[ApiController]
[Route("api/[controller]")]
public class CraftingController : ControllerBase
public class UserController : ControllerBase
{
/// <summary>
/// The json serializer options.
@ -34,7 +34,7 @@ namespace Minecraft.Crafting.Api.Controllers
/// <param name="item">The item.</param>
/// <returns>The async task.</returns>
[HttpPost]
[Route("")]
[Route("add")]
public Task Add(User item)
{
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.
/// </summary>
/// <returns>The recipes.</returns>
[HttpGet]
/* [HttpGet]
[Route("recipe")]
public Task<List<Recipe>> GetRecipe()
{
if (!System.IO.File.Exists("Data/convert-recipes.json"))
if (!System.IO.File.Exists("Data/items.json"))
{
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)
{
@ -196,6 +196,7 @@ namespace Minecraft.Crafting.Api.Controllers
return Task.FromResult(data);
}
*/
/// <summary>
/// Get the items with pagination.
@ -237,7 +238,7 @@ namespace Minecraft.Crafting.Api.Controllers
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>
{
@ -299,19 +300,20 @@ namespace Minecraft.Crafting.Api.Controllers
/// Resets the recipes.
/// </summary>
/// <returns>The async task.</returns>
[HttpGet]
/* [HttpGet]
[Route("reset-recipes")]
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();
return Task.CompletedTask;
}
*/
/// <summary>
/// Updates the specified identifier.
@ -332,7 +334,7 @@ namespace Minecraft.Crafting.Api.Controllers
throw new Exception($"Unable to found the item with ID: {id}");
}
itemOriginal.Id = item.Id;
//itemOriginal.Id = item.Id;
itemOriginal.Name = item.Name;
itemOriginal.Surname = item.Surname;
itemOriginal.Nickname = item.Nickname;
@ -393,7 +395,7 @@ namespace Minecraft.Crafting.Api.Controllers
/// <summary>
/// Converts the recipes.
/// </summary>
private void ConvertRecipes()
/* private void ConvertRecipes()
{
var data = JsonSerializer.Deserialize<List<User>>(System.IO.File.ReadAllText("Data/items.json"), _jsonSerializerOptions);
@ -402,7 +404,7 @@ namespace Minecraft.Crafting.Api.Controllers
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>();
@ -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>
<TargetFramework>net6.0</TargetFramework>
@ -12,15 +12,6 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<Content Update="Data\items-original.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Update="Data\recipes.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<ItemGroup>
<Content Include="Images\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>

@ -21,9 +21,10 @@ var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRazorPages();
builder.Services.AddServerSideBlazor();
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>();

Loading…
Cancel
Save