Adding DataService and Crafting Components

pull/3/head
arthur.valin 2 years ago
parent 093d1050c9
commit f28ee32cca

@ -1,4 +1,6 @@
<div
@using CraftSharp.Models
<div
class="item"
ondragover="event.preventDefault();"
draggable="true"

@ -2,7 +2,7 @@
using CraftSharp.Models;
using Microsoft.AspNetCore.Components;
namespace Blazor.Components
namespace CraftSharp.Components
{
public partial class CraftingItem
{

@ -0,0 +1,11 @@
@typeparam TItem
<div>
@if ((Items?.Count ?? 0) != 0)
{
@foreach (var item in Items)
{
@ShowTemplate(item);
}
}
</div>

@ -0,0 +1,13 @@
using Microsoft.AspNetCore.Components;
namespace CraftSharp.Components
{
public partial class ShowItems<TItem>
{
[Parameter]
public List<TItem> Items { get; set; }
[Parameter]
public RenderFragment<TItem> ShowTemplate { get; set; }
}
}

@ -1,30 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>41eb41f8-57fb-408a-baab-7328f468e749</UserSecretsId>
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
<DockerfileContext>.</DockerfileContext>
</PropertyGroup>
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>41eb41f8-57fb-408a-baab-7328f468e749</UserSecretsId>
<DockerDefaultTargetOS>Windows</DockerDefaultTargetOS>
<DockerfileContext>.</DockerfileContext>
</PropertyGroup>
<ItemGroup>
<Content Include="Models\Armor.cs" />
<Content Include="Models\Block.cs" />
<Content Include="Models\Boots.cs" />
<Content Include="Models\Chestplate.cs" />
<Content Include="Models\Helmet.cs" />
<Content Include="Models\Item.cs" />
<Content Include="Models\Leggings.cs" />
<Content Include="Models\Rarities.cs" />
<Content Include="Models\Tool.cs" />
<Content Include="Models\ItemModel.cs" />
<Content Include="Models\Rarities.cs" />
<Content Include="Models\Trade.cs" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Localization" Version="7.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
</ItemGroup>
</Project>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Localization" Version="7.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.17.0" />
</ItemGroup>
</Project>

@ -0,0 +1,55 @@
using CraftSharp.Models;
namespace CraftSharp.Factories
{
public static class ItemFactory
{
public static ItemModel ToModel(Item item, byte[] imageContent)
{
return new ItemModel
{
Id = item.Id,
DisplayName = item.DisplayName,
Name = item.Name,
RepairWith = item.RepairWith,
EnchantCategories = item.EnchantCategories,
MaxDurability = item.MaxDurability,
StackSize = item.StackSize,
ImageContent = imageContent,
ImageBase64 = string.IsNullOrWhiteSpace(item.ImageBase64) ? Convert.ToBase64String(imageContent) : item.ImageBase64,
Rarity = item.Rarity
};
}
public static Item Create(ItemModel model)
{
return new Item
{
Id = model.Id,
DisplayName = model.DisplayName,
Name = model.Name,
RepairWith = model.RepairWith,
EnchantCategories = model.EnchantCategories,
MaxDurability = model.MaxDurability,
StackSize = model.StackSize,
CreatedDate = DateTime.Now,
ImageBase64 = Convert.ToBase64String(model.ImageContent),
Rarity = model.Rarity
};
}
public static void Update(Item item, ItemModel model)
{
item.DisplayName = model.DisplayName;
item.Name = model.Name;
item.RepairWith = model.RepairWith;
item.EnchantCategories = model.EnchantCategories;
item.MaxDurability = model.MaxDurability;
item.StackSize = model.StackSize;
item.UpdatedDate = DateTime.Now;
item.ImageBase64 = Convert.ToBase64String(model.ImageContent);
item.Rarity = model.Rarity;
}
}
}

@ -3,9 +3,16 @@
public class Item
{
public int Id { get; set; }
public string DisplayName { get; set; }
public string Name { get; set; }
public int StackSize { get; set; }
public int MaxDurability { get; set; }
public List<string> EnchantCategories { get; set; }
public List<string> RepairWith { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime? UpdatedDate { get; set; }
public string ImageBase64 { get; set; }
public Rarities Rarity { get; set; }
}
}

@ -35,6 +35,8 @@ namespace CraftSharp.Models
public byte[] ImageContent { get; set; }
public string ImageBase64 { get; set; }
[Required]
public Rarities Rarity { get; set; }
}
}

@ -1,4 +1,5 @@
using CraftSharp.Data;
using CraftSharp.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Localization;
@ -18,6 +19,10 @@ builder.Services.AddControllers();
// Add the localization to the app and specify the resources path
builder.Services.AddLocalization(opts => { opts.ResourcesPath = "Resources"; });
builder.Services.AddHttpClient();
builder.Services.AddScoped<IDataService, DataApiService>();
// Configure the localtization
builder.Services.Configure<RequestLocalizationOptions>(options =>
{

@ -0,0 +1,59 @@
using CraftSharp.Components;
using CraftSharp.Factories;
using CraftSharp.Models;
namespace CraftSharp.Services
{
public class DataApiService : IDataService
{
private readonly HttpClient _http;
public DataApiService(
HttpClient http)
{
_http = http;
}
public async Task Add(ItemModel model)
{
// Get the item
var item = ItemFactory.Create(model);
// Save the data
await _http.PostAsJsonAsync("https://localhost:7234/api/Crafting/", item);
}
public async Task<int> Count()
{
return await _http.GetFromJsonAsync<int>("https://localhost:7234/api/Crafting/count");
}
public async Task<List<Item>> List(int currentPage, int pageSize)
{
return await _http.GetFromJsonAsync<List<Item>>($"https://localhost:7234/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}");
}
public async Task<Item> GetById(int id)
{
return await _http.GetFromJsonAsync<Item>($"https://localhost:7234/api/Crafting/{id}");
}
public async Task Update(int id, ItemModel model)
{
// Get the item
var item = ItemFactory.Create(model);
await _http.PutAsJsonAsync($"https://localhost:7234/api/Crafting/{id}", item);
}
public async Task Delete(int id)
{
await _http.DeleteAsync($"https://localhost:7234/api/Crafting/{id}");
}
public async Task<List<CraftingRecipe>> GetRecipes()
{
return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://localhost:7234/api/Crafting/recipe");
}
}
}

@ -1,7 +1,7 @@
using CraftSharp.Components;
using CraftSharp.Models;
namespace Blazor.Services
namespace CraftSharp.Services
{
public interface IDataService
{

Loading…
Cancel
Save