diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Models/PokemonDto.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Models/PokemonDto.cs new file mode 100644 index 0000000..93a4c1b --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Models/PokemonDto.cs @@ -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; } + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Models/PokemonNatureDto.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Models/PokemonNatureDto.cs new file mode 100644 index 0000000..8d17ed1 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Models/PokemonNatureDto.cs @@ -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; } + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Models/PokemonSubskillDto.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Models/PokemonSubskillDto.cs new file mode 100644 index 0000000..c44de52 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Models/PokemonSubskillDto.cs @@ -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; } + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Models/RequestDto.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Models/RequestDto.cs new file mode 100644 index 0000000..d185224 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Models/RequestDto.cs @@ -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; } + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Models/ResponseDto.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Models/ResponseDto.cs new file mode 100644 index 0000000..e64da81 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Models/ResponseDto.cs @@ -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; } = ""; + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/PokemonSleepWeb.Client.csproj b/PokemonSleepWeb/PokemonSleepWeb.Client/PokemonSleepWeb.Client.csproj index fe74c0b..743e46d 100644 --- a/PokemonSleepWeb/PokemonSleepWeb.Client/PokemonSleepWeb.Client.csproj +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/PokemonSleepWeb.Client.csproj @@ -10,6 +10,8 @@ + + diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Program.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Program.cs index 519269f..c5af3bb 100644 --- a/PokemonSleepWeb/PokemonSleepWeb.Client/Program.cs +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Program.cs @@ -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(); +builder.Services.AddScoped(); + await builder.Build().RunAsync(); diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Service/BaseService.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Service/BaseService.cs new file mode 100644 index 0000000..1f3fe6a --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Service/BaseService.cs @@ -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 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(apiContent); + return apiResponseDto; + + } + + } + catch (Exception ex) + { + var dto = new ResponseDto + { + IsSuccess = false, + Message = ex.Message.ToString() + }; + return dto; + } + } + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Service/IService/IBaseService.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Service/IService/IBaseService.cs new file mode 100644 index 0000000..e240813 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Service/IService/IBaseService.cs @@ -0,0 +1,9 @@ +using PokemonSleepWeb.Client.Models; + +namespace PokemonSleepWeb.Client.Service.IService +{ + public interface IBaseService + { + Task SendAsync(RequestDto requestDto); + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Service/IService/IPokemonService.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Service/IService/IPokemonService.cs new file mode 100644 index 0000000..06ed76a --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Service/IService/IPokemonService.cs @@ -0,0 +1,11 @@ +using PokemonSleepWeb.Client.Models; + +namespace PokemonSleepWeb.Client.Service.IService +{ + public interface IPokemonService + { + + Task GetAllPokemonAsync(); + + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Service/PokemonService.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Service/PokemonService.cs new file mode 100644 index 0000000..b0dcee5 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Service/PokemonService.cs @@ -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 GetAllPokemonAsync() + { + return await _baseService.SendAsync(new RequestDto() + { + ApiType = StaticDetails.ApiType.GET, + Url = StaticDetails.PokemonSleepAPIBase + "/api/pokemon" + }); + } + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/Utility/StaticDetails.cs b/PokemonSleepWeb/PokemonSleepWeb.Client/Utility/StaticDetails.cs new file mode 100644 index 0000000..c4ebb0e --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/Utility/StaticDetails.cs @@ -0,0 +1,11 @@ +namespace PokemonSleepWeb.Client.Utility +{ + public class StaticDetails + { + public static string PokemonSleepAPIBase { get; set; } + public enum ApiType + { + GET, POST, PUT, DELETE + } + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb.Client/wwwroot/appsettings.json b/PokemonSleepWeb/PokemonSleepWeb.Client/wwwroot/appsettings.json index 0c208ae..afb86ac 100644 --- a/PokemonSleepWeb/PokemonSleepWeb.Client/wwwroot/appsettings.json +++ b/PokemonSleepWeb/PokemonSleepWeb.Client/wwwroot/appsettings.json @@ -4,5 +4,8 @@ "Default": "Information", "Microsoft.AspNetCore": "Warning" } + }, + { + "ApiBaseUrl": "https://localhost:7261" } } diff --git a/PokemonSleepWeb/PokemonSleepWeb/Components/Pages/Home.razor b/PokemonSleepWeb/PokemonSleepWeb/Components/Pages/Home.razor index 9001e0b..f5cba7f 100644 --- a/PokemonSleepWeb/PokemonSleepWeb/Components/Pages/Home.razor +++ b/PokemonSleepWeb/PokemonSleepWeb/Components/Pages/Home.razor @@ -1,7 +1,7 @@ @page "/" + Home -

Hello, world!

-Welcome to your new app. + diff --git a/PokemonSleepWeb/PokemonSleepWeb/Components/Pages/PokemonPage.razor b/PokemonSleepWeb/PokemonSleepWeb/Components/Pages/PokemonPage.razor new file mode 100644 index 0000000..abfe5a0 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb/Components/Pages/PokemonPage.razor @@ -0,0 +1,24 @@ +@page "/pokemonpage" +@using PokemonSleepWeb.Models +@inject PokemonSleepWeb.Service.PokemonService PokemonService + + +

Pokemon

+ +@if (list == null) +{ +

Loading...

+} +else +{ + +} + +@code { + private ResponseDto list; + + protected override async Task OnInitializedAsync() + { + list = await PokemonService.GetAllPokemonAsync(); + } +} \ No newline at end of file diff --git a/PokemonSleepWeb/PokemonSleepWeb/Controllers/PokemonController.cs b/PokemonSleepWeb/PokemonSleepWeb/Controllers/PokemonController.cs new file mode 100644 index 0000000..a0e9aa2 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb/Controllers/PokemonController.cs @@ -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 Index() + { + List? list = new(); + ResponseDto? response = await _pokemonService.GetAllPokemonAsync(); + + if (response != null && response.IsSuccess) + { + list = JsonConvert.DeserializeObject>(Convert.ToString(response.Result)); + } + else + { + TempData["error"] = response?.Message; + } + + return View(list); + } + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb/PokemonSleepWeb.csproj b/PokemonSleepWeb/PokemonSleepWeb/PokemonSleepWeb.csproj index fdf2925..b0c6578 100644 --- a/PokemonSleepWeb/PokemonSleepWeb/PokemonSleepWeb.csproj +++ b/PokemonSleepWeb/PokemonSleepWeb/PokemonSleepWeb.csproj @@ -9,11 +9,7 @@ - - - - - + diff --git a/PokemonSleepWeb/PokemonSleepWeb/Program.cs b/PokemonSleepWeb/PokemonSleepWeb/Program.cs index 32754e8..6c48818 100644 --- a/PokemonSleepWeb/PokemonSleepWeb/Program.cs +++ b/PokemonSleepWeb/PokemonSleepWeb/Program.cs @@ -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(); + +StaticDetails.PokemonSleepAPIBase = builder.Configuration["ServiceUrls:PokemonAPI"]; + + +builder.Services.AddScoped(); +builder.Services.AddScoped(); + + + + + + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/PokemonSleepWeb/PokemonSleepWeb/Service/BaseService.cs b/PokemonSleepWeb/PokemonSleepWeb/Service/BaseService.cs new file mode 100644 index 0000000..0ee4126 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb/Service/BaseService.cs @@ -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 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(apiContent); + return apiResponseDto; + + } + + } + catch (Exception ex) + { + var dto = new ResponseDto + { + IsSuccess = false, + Message = ex.Message.ToString() + }; + return dto; + } + } + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb/Service/IService/IBaseService.cs b/PokemonSleepWeb/PokemonSleepWeb/Service/IService/IBaseService.cs new file mode 100644 index 0000000..f96731a --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb/Service/IService/IBaseService.cs @@ -0,0 +1,9 @@ +using PokemonSleepWeb.Models; + +namespace PokemonSleepWeb.Service.IService +{ + public interface IBaseService + { + Task SendAsync(RequestDto requestDto); + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb/Service/IService/IPokemonService.cs b/PokemonSleepWeb/PokemonSleepWeb/Service/IService/IPokemonService.cs new file mode 100644 index 0000000..2769609 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb/Service/IService/IPokemonService.cs @@ -0,0 +1,11 @@ +using PokemonSleepWeb.Models; + +namespace PokemonSleepWeb.Service.IService +{ + public interface IPokemonService + { + + Task GetAllPokemonAsync(); + + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb/Service/PokemonService.cs b/PokemonSleepWeb/PokemonSleepWeb/Service/PokemonService.cs new file mode 100644 index 0000000..3eacb97 --- /dev/null +++ b/PokemonSleepWeb/PokemonSleepWeb/Service/PokemonService.cs @@ -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 GetAllPokemonAsync() + { + return await _baseService.SendAsync(new RequestDto() + { + ApiType = StaticDetails.ApiType.GET, + Url = StaticDetails.PokemonSleepAPIBase + "/api/pokemon" + }); + } + } +} diff --git a/PokemonSleepWeb/PokemonSleepWeb/Utility/StaticDetails.cs b/PokemonSleepWeb/PokemonSleepWeb/Utility/StaticDetails.cs index 837aebc..1ad3665 100644 --- a/PokemonSleepWeb/PokemonSleepWeb/Utility/StaticDetails.cs +++ b/PokemonSleepWeb/PokemonSleepWeb/Utility/StaticDetails.cs @@ -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 diff --git a/PokemonSleepWeb/PokemonSleepWeb/appsettings.json b/PokemonSleepWeb/PokemonSleepWeb/appsettings.json index 10f68b8..2ec84ea 100644 --- a/PokemonSleepWeb/PokemonSleepWeb/appsettings.json +++ b/PokemonSleepWeb/PokemonSleepWeb/appsettings.json @@ -5,5 +5,8 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "ServiceUrls": { + "PokemonSleepAPI": "https://localhost:7261" + } }