// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) UCA Clermont-Ferrand All rights reserved.
//
// --------------------------------------------------------------------------------------------------------------------
namespace Minecraft.Crafting.Api.Controllers
{
using Microsoft.AspNetCore.Mvc;
using Minecraft.Crafting.Api.Models;
using System.Text.Json;
using System.Text.Json.Serialization;
///
/// The inventory controller.
///
[ApiController]
[Route("api/[controller]")]
public class InventoryController : ControllerBase
{
///
/// The json serializer options.
///
private readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};
///
/// Adds to inventory.
///
/// The item.
/// The async task.
[HttpPost]
[Route("")]
public Task AddToInventory(InventoryModel item)
{
var data = JsonSerializer.Deserialize>(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;
}
///
/// Deletes from inventory.
///
/// The item.
/// The async task.
[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>(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;
}
///
/// Gets the inventory.
///
/// The inventory.
[HttpGet]
[Route("")]
public Task> GetInventory()
{
if (!System.IO.File.Exists("Data/inventory.json"))
{
return Task.FromResult(new List());
}
var data = JsonSerializer.Deserialize>(System.IO.File.ReadAllText("Data/inventory.json"), _jsonSerializerOptions);
if (data == null)
{
throw new Exception("Unable to get the inventory.");
}
return Task.FromResult(data);
}
///
/// Updates the inventory.
///
/// The item.
/// The async task.
[HttpPut]
[Route("")]
public Task UpdateInventory(string[] infos)
{
var data = JsonSerializer.Deserialize>(System.IO.File.ReadAllText("Data/inventory.json"), _jsonSerializerOptions);
if (data == null)
{
throw new Exception("Unable to get the inventory.");
}
data.ToArray()[Int32.Parse(infos[1])-1].ItemName = infos[0];
data.ToArray()[Int32.Parse(infos[1])-1].Position = Int32.Parse(infos[1]);
data.ToArray()[Int32.Parse(infos[1])-1].Number = Int32.Parse(infos[2]);
System.IO.File.WriteAllText("Data/inventory.json", JsonSerializer.Serialize(data, _jsonSerializerOptions));
return Task.CompletedTask;
}
}
}