Roadblock.

This commit is contained in:
Kira Jiroux 2024-12-06 17:22:58 -05:00
parent 10fcab79c1
commit b9520d3bd8
24 changed files with 420 additions and 15 deletions

View File

@ -0,0 +1,10 @@
namespace PokemonSleepWeb.Client.Models
{
public class PokemonDto
{
public int Id { get; set; }
public string Name { get; set; }
public string SleepType { get; set; }
public string Speciality { get; set; }
}
}

View File

@ -0,0 +1,11 @@
namespace PokemonSleepWeb.Client.Models
{
public class PokemonNatureDto
{
public int Id { get; set; }
public string Nature { get; set; }
public int BerryRating { get; set; }
public int IngredientRating { get; set; }
public int SkillRating { get; set; }
}
}

View File

@ -0,0 +1,11 @@
namespace PokemonSleepWeb.Client.Models
{
public class PokemonSubskillDto
{
public int Id { get; set; }
public string SubSkill { get; set; }
public int BerryRank { get; set; }
public int IngredientRank { get; set; }
public int SkillRank { get; set; }
}
}

View File

@ -0,0 +1,12 @@
using static PokemonSleepWeb.Client.Utility.StaticDetails;
namespace PokemonSleepWeb.Client.Models
{
public class RequestDto
{
public ApiType ApiType { get; set; } = ApiType.GET;
public string Url { get; set; }
public object Data { get; set; }
public string AccessToken { get; set; }
}
}

View File

@ -0,0 +1,9 @@
namespace PokemonSleepWeb.Client.Models
{
public class ResponseDto
{
public object? Result { get; set; }
public bool IsSuccess { get; set; } = true;
public string Message { get; set; } = "";
}
}

View File

@ -10,6 +10,8 @@
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="8.0.10" />
<PackageReference Include="Microsoft.Extensions.Http" Version="9.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View File

@ -1,5 +1,15 @@
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using PokemonSleepWeb.Client.Service;
var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddScoped(sp => new HttpClient
{
BaseAddress = new Uri(builder.Configuration["ApiBaseUrl"]!)
});
builder.Services.AddScoped<BaseService>();
builder.Services.AddScoped<PokemonService>();
await builder.Build().RunAsync();

View File

@ -0,0 +1,84 @@
using Newtonsoft.Json;
using PokemonSleepWeb.Client.Models;
using PokemonSleepWeb.Client.Service.IService;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Text.Json.Serialization;
using static PokemonSleepWeb.Client.Utility.StaticDetails;
namespace PokemonSleepWeb.Client.Service
{
public class BaseService : IBaseService
{
private readonly IHttpClientFactory _httpClientFactory;
public BaseService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<ResponseDto?> SendAsync(RequestDto requestDto)
{
try
{
HttpClient client = _httpClientFactory.CreateClient("PokemonSleepAPI");
HttpRequestMessage message = new();
message.Headers.Add("Accept", "application/json");
message.RequestUri = new Uri(requestDto.Url);
if(requestDto.Data != null)
{
message.Content = new StringContent(JsonConvert.SerializeObject(requestDto.Data), Encoding.UTF8, "application/json");
}
HttpResponseMessage? apiResponse = null;
switch (requestDto.ApiType)
{
case ApiType.POST:
message.Method = HttpMethod.Post;
break;
case ApiType.DELETE:
message.Method = HttpMethod.Delete;
break;
case ApiType.PUT:
message.Method = HttpMethod.Put;
break;
default:
message.Method = HttpMethod.Get;
break;
}
apiResponse = await client.SendAsync(message);
switch(apiResponse.StatusCode)
{
case HttpStatusCode.NotFound:
return new() { IsSuccess = false, Message = "Not Found." };
case HttpStatusCode.Forbidden:
return new() { IsSuccess = false, Message = "Access Denied." };
case HttpStatusCode.Unauthorized:
return new() { IsSuccess = false, Message = "Unauthorized." };
case HttpStatusCode.InternalServerError:
return new() { IsSuccess = false, Message = "Internal Server Error." };
default:
var apiContent = await apiResponse.Content.ReadAsStringAsync();
var apiResponseDto = JsonConvert.DeserializeObject<ResponseDto>(apiContent);
return apiResponseDto;
}
}
catch (Exception ex)
{
var dto = new ResponseDto
{
IsSuccess = false,
Message = ex.Message.ToString()
};
return dto;
}
}
}
}

View File

@ -0,0 +1,9 @@
using PokemonSleepWeb.Client.Models;
namespace PokemonSleepWeb.Client.Service.IService
{
public interface IBaseService
{
Task<ResponseDto?> SendAsync(RequestDto requestDto);
}
}

View File

@ -0,0 +1,11 @@
using PokemonSleepWeb.Client.Models;
namespace PokemonSleepWeb.Client.Service.IService
{
public interface IPokemonService
{
Task<ResponseDto?> GetAllPokemonAsync();
}
}

View File

@ -0,0 +1,24 @@
using PokemonSleepWeb.Client.Service.IService;
using PokemonSleepWeb.Client.Utility;
using PokemonSleepWeb.Client.Models;
namespace PokemonSleepWeb.Client.Service
{
public class PokemonService : IPokemonService
{
private readonly IBaseService _baseService;
public PokemonService(IBaseService baseService)
{
_baseService = baseService;
}
public async Task<ResponseDto?> GetAllPokemonAsync()
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = StaticDetails.ApiType.GET,
Url = StaticDetails.PokemonSleepAPIBase + "/api/pokemon"
});
}
}
}

View File

@ -0,0 +1,11 @@
namespace PokemonSleepWeb.Client.Utility
{
public class StaticDetails
{
public static string PokemonSleepAPIBase { get; set; }
public enum ApiType
{
GET, POST, PUT, DELETE
}
}
}

View File

@ -4,5 +4,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
{
"ApiBaseUrl": "https://localhost:7261"
}
}

View File

@ -1,7 +1,7 @@
@page "/"
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
Welcome to your new app.

View File

@ -0,0 +1,24 @@
@page "/pokemonpage"
@using PokemonSleepWeb.Models
@inject PokemonSleepWeb.Service.PokemonService PokemonService
<h3>Pokemon</h3>
@if (list == null)
{
<p>Loading...</p>
}
else
{
}
@code {
private ResponseDto list;
protected override async Task OnInitializedAsync()
{
list = await PokemonService.GetAllPokemonAsync();
}
}

View File

@ -0,0 +1,34 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using PokemonSleepWeb.Models;
using PokemonSleepWeb.Service.IService;
namespace PokemonSleepWeb.Controllers
{
public class PokemonController : Controller
{
private readonly IPokemonService _pokemonService;
public PokemonController(IPokemonService pokemonService)
{
_pokemonService = pokemonService;
}
public async Task<IActionResult> Index()
{
List<PokemonDto>? list = new();
ResponseDto? response = await _pokemonService.GetAllPokemonAsync();
if (response != null && response.IsSuccess)
{
list = JsonConvert.DeserializeObject<List<PokemonDto>>(Convert.ToString(response.Result));
}
else
{
TempData["error"] = response?.Message;
}
return View(list);
}
}
}

View File

@ -9,11 +9,7 @@
<ItemGroup>
<ProjectReference Include="..\PokemonSleepWeb.Client\PokemonSleepWeb.Client.csproj" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="8.0.10" />
</ItemGroup>
<ItemGroup>
<Folder Include="Controllers\" />
<Folder Include="Service\" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

View File

@ -1,5 +1,8 @@
using PokemonSleepWeb.Client.Pages;
using PokemonSleepWeb.Components;
using PokemonSleepWeb.Service;
using PokemonSleepWeb.Service.IService;
using PokemonSleepWeb.Utility;
var builder = WebApplication.CreateBuilder(args);
@ -8,6 +11,23 @@ builder.Services.AddRazorComponents()
.AddInteractiveServerComponents()
.AddInteractiveWebAssemblyComponents();
builder.Services.AddControllersWithViews();
builder.Services.AddHttpContextAccessor();
builder.Services.AddHttpClient();
builder.Services.AddHttpClient<IPokemonService, PokemonService>();
StaticDetails.PokemonSleepAPIBase = builder.Configuration["ServiceUrls:PokemonAPI"];
builder.Services.AddScoped<IBaseService, BaseService>();
builder.Services.AddScoped<IPokemonService, PokemonService>();
var app = builder.Build();
// Configure the HTTP request pipeline.

View File

@ -0,0 +1,83 @@
using Newtonsoft.Json;
using PokemonSleepWeb.Models;
using PokemonSleepWeb.Service.IService;
using System.Net;
using System.Text;
using System.Text.Json.Serialization;
using static PokemonSleepWeb.Utility.StaticDetails;
namespace PokemonSleepWeb.Service
{
public class BaseService : IBaseService
{
private readonly IHttpClientFactory _httpClientFactory;
public BaseService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public async Task<ResponseDto?> SendAsync(RequestDto requestDto)
{
try
{
HttpClient client = _httpClientFactory.CreateClient("PokemonSleepAPI");
HttpRequestMessage message = new();
message.Headers.Add("Accept", "application/json");
message.RequestUri = new Uri(requestDto.Url);
if(requestDto.Data != null)
{
message.Content = new StringContent(JsonConvert.SerializeObject(requestDto.Data), Encoding.UTF8, "application/json");
}
HttpResponseMessage? apiResponse = null;
switch (requestDto.ApiType)
{
case ApiType.POST:
message.Method = HttpMethod.Post;
break;
case ApiType.DELETE:
message.Method = HttpMethod.Delete;
break;
case ApiType.PUT:
message.Method = HttpMethod.Put;
break;
default:
message.Method = HttpMethod.Get;
break;
}
apiResponse = await client.SendAsync(message);
switch(apiResponse.StatusCode)
{
case HttpStatusCode.NotFound:
return new() { IsSuccess = false, Message = "Not Found." };
case HttpStatusCode.Forbidden:
return new() { IsSuccess = false, Message = "Access Denied." };
case HttpStatusCode.Unauthorized:
return new() { IsSuccess = false, Message = "Unauthorized." };
case HttpStatusCode.InternalServerError:
return new() { IsSuccess = false, Message = "Internal Server Error." };
default:
var apiContent = await apiResponse.Content.ReadAsStringAsync();
var apiResponseDto = JsonConvert.DeserializeObject<ResponseDto>(apiContent);
return apiResponseDto;
}
}
catch (Exception ex)
{
var dto = new ResponseDto
{
IsSuccess = false,
Message = ex.Message.ToString()
};
return dto;
}
}
}
}

View File

@ -0,0 +1,9 @@
using PokemonSleepWeb.Models;
namespace PokemonSleepWeb.Service.IService
{
public interface IBaseService
{
Task<ResponseDto?> SendAsync(RequestDto requestDto);
}
}

View File

@ -0,0 +1,11 @@
using PokemonSleepWeb.Models;
namespace PokemonSleepWeb.Service.IService
{
public interface IPokemonService
{
Task<ResponseDto?> GetAllPokemonAsync();
}
}

View File

@ -0,0 +1,24 @@
using PokemonSleepWeb.Models;
using PokemonSleepWeb.Service.IService;
using PokemonSleepWeb.Utility;
namespace PokemonSleepWeb.Service
{
public class PokemonService : IPokemonService
{
private readonly IBaseService _baseService;
public PokemonService(IBaseService baseService)
{
_baseService = baseService;
}
public async Task<ResponseDto?> GetAllPokemonAsync()
{
return await _baseService.SendAsync(new RequestDto()
{
ApiType = StaticDetails.ApiType.GET,
Url = StaticDetails.PokemonSleepAPIBase + "/api/pokemon"
});
}
}
}

View File

@ -4,13 +4,7 @@ namespace PokemonSleepWeb.Utility
{
public class StaticDetails
{
public static string CouponAPIBase { get; set; }
public static string AuthAPIBase { get; set; }
public static string ProductAPIBase { get; set; }
public static string ShoppingCartAPIBase { get; set; }
public const string RoleAdmin = "ADMIN";
public const string RoleCustomer = "CUSTOMER";
public const string TokenCookie = "JWTToken";
public static string PokemonSleepAPIBase { get; set; }
public enum ApiType
{
GET, POST, PUT, DELETE

View File

@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
"AllowedHosts": "*",
"ServiceUrls": {
"PokemonSleepAPI": "https://localhost:7261"
}
}