diff --git a/Portfolio.Application/Portfolio.Application.csproj b/Portfolio.Application/Portfolio.Application.csproj index f587933..f1618fa 100644 --- a/Portfolio.Application/Portfolio.Application.csproj +++ b/Portfolio.Application/Portfolio.Application.csproj @@ -8,6 +8,8 @@ + + diff --git a/Portfolio.Application/Services/BaseService.cs b/Portfolio.Application/Services/BaseService.cs new file mode 100644 index 0000000..ccfafa7 --- /dev/null +++ b/Portfolio.Application/Services/BaseService.cs @@ -0,0 +1,90 @@ +using Newtonsoft.Json; +using Portfolio.Domain.Features.Dtos; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Text; +using System.Threading.Tasks; +using static Portfolio.Domain.Utility.StaticDetails; + +namespace Portfolio.Application.Services +{ + 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"); + + //token here + + 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/Portfolio.Application/Services/IBaseService.cs b/Portfolio.Application/Services/IBaseService.cs new file mode 100644 index 0000000..ed81677 --- /dev/null +++ b/Portfolio.Application/Services/IBaseService.cs @@ -0,0 +1,14 @@ +using Portfolio.Domain.Features.Dtos; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Portfolio.Application.Services +{ + public interface IBaseService + { + Task SendAsync(RequestDto requestDto); + } +} diff --git a/Portfolio.Application/Services/PokemonService/IPokemonService.cs b/Portfolio.Application/Services/PokemonService/IPokemonService.cs index 1cb4a16..4a6d293 100644 --- a/Portfolio.Application/Services/PokemonService/IPokemonService.cs +++ b/Portfolio.Application/Services/PokemonService/IPokemonService.cs @@ -1,4 +1,5 @@ -using Portfolio.Domain.Features.Pokemon; +using Portfolio.Domain.Features.Dtos; +using Portfolio.Domain.Features.Pokemon; using System; using System.Collections.Generic; using System.Linq; @@ -9,6 +10,6 @@ namespace Portfolio.Application.Services.PokemonService { public interface IPokemonService { - Task> GetAllPokemonAsync(); + Task> GetAllPokemonAsync(); } } diff --git a/Portfolio.Application/Services/PokemonService/PokemonService.cs b/Portfolio.Application/Services/PokemonService/PokemonService.cs index 7258bec..83a679d 100644 --- a/Portfolio.Application/Services/PokemonService/PokemonService.cs +++ b/Portfolio.Application/Services/PokemonService/PokemonService.cs @@ -1,9 +1,12 @@ -using Portfolio.Domain.Features.Articles; +using Newtonsoft.Json; +using Portfolio.Domain.Features.Articles; +using Portfolio.Domain.Features.Dtos; using Portfolio.Domain.Features.Pokemon; using System; using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using System.Threading.Tasks; namespace Portfolio.Application.Services.PokemonService @@ -16,9 +19,19 @@ namespace Portfolio.Application.Services.PokemonService { _pokemonRepository = pokemonRepository; } - public async Task> GetAllPokemonAsync() + public async Task> GetAllPokemonAsync() { - return await _pokemonRepository.GetAllPokemonsAsync(); + List pokemons = new List(); + ResponseDto? response = await _pokemonRepository.GetAllPokemon(); + + if (response != null && response.IsSuccess) + { + pokemons = JsonConvert.DeserializeObject>(Convert.ToString(response.Result)); + } + + return pokemons; + + //return new List() { // new Pokemon diff --git a/Portfolio.Domain/Features/Dtos/PokemonDto.cs b/Portfolio.Domain/Features/Dtos/PokemonDto.cs new file mode 100644 index 0000000..7477e78 --- /dev/null +++ b/Portfolio.Domain/Features/Dtos/PokemonDto.cs @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Portfolio.Domain.Features.Dtos +{ + public class PokemonDto + { + public int Id { get; set; } + public int PokemonId { get; set; } + public required string PokemonName { get; set; } + public bool IsVariation { get; set; } = false; + public string? VariationName { get; set; } + public required string SleepType { get; set; } + public required string Speciality { get; set; } + } +} diff --git a/Portfolio.Domain/Features/Dtos/RequestDto.cs b/Portfolio.Domain/Features/Dtos/RequestDto.cs new file mode 100644 index 0000000..bc9d802 --- /dev/null +++ b/Portfolio.Domain/Features/Dtos/RequestDto.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.AccessControl; +using System.Text; +using System.Threading.Tasks; +using static Portfolio.Domain.Utility.StaticDetails; + +namespace Portfolio.Domain.Features.Dtos +{ + 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/Portfolio.Domain/Features/Dtos/ResponseDto.cs b/Portfolio.Domain/Features/Dtos/ResponseDto.cs new file mode 100644 index 0000000..7e7f60a --- /dev/null +++ b/Portfolio.Domain/Features/Dtos/ResponseDto.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Portfolio.Domain.Features.Dtos +{ + public class ResponseDto + { + public object? Result { get; set; } + public bool IsSuccess { get; set; } + public string Message { get; set; } + } +} diff --git a/Portfolio.Domain/Features/Pokemon/IPokemonRepository.cs b/Portfolio.Domain/Features/Pokemon/IPokemonRepository.cs index 48c89af..d50fe18 100644 --- a/Portfolio.Domain/Features/Pokemon/IPokemonRepository.cs +++ b/Portfolio.Domain/Features/Pokemon/IPokemonRepository.cs @@ -1,4 +1,5 @@ -using System; +using Portfolio.Domain.Features.Dtos; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -8,6 +9,6 @@ namespace Portfolio.Domain.Features.Pokemon { public interface IPokemonRepository { - Task> GetAllPokemonsAsync(); + Task GetAllPokemon(); } } diff --git a/Portfolio.Domain/Utility/StaticDetails.cs b/Portfolio.Domain/Utility/StaticDetails.cs new file mode 100644 index 0000000..e290917 --- /dev/null +++ b/Portfolio.Domain/Utility/StaticDetails.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Portfolio.Domain.Utility +{ + public class StaticDetails + { + public static string PokemonSleepAPIBase { get; set; } + public enum ApiType + { + GET, POST, PUT, DELETE + } + } +} diff --git a/Portfolio.Infrastructure/ApplicationDbContext.cs b/Portfolio.Infrastructure/ApplicationDbContext.cs index ea6d221..03111a5 100644 --- a/Portfolio.Infrastructure/ApplicationDbContext.cs +++ b/Portfolio.Infrastructure/ApplicationDbContext.cs @@ -13,7 +13,7 @@ namespace Portfolio.Infrastructure { public class ApplicationDbContext : DbContext { - public ApplicationDbContext(DbContextOptions options) :base(options) + public ApplicationDbContext(DbContextOptions options) : base(options) { } @@ -21,5 +21,233 @@ namespace Portfolio.Infrastructure public DbSet PokemonNatures { get; set; } public DbSet PokemonSubskills { get; set; } + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + + + //Pokemon + modelBuilder.Entity().HasData(new Pokemon + { + Id = 1, + PokemonId = 1, + PokemonName = "Bulbasaur", + SleepType = "Dozing", + Speciality = "Ingredients" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 2, + PokemonId = 2, + PokemonName = "Ivysaur", + SleepType = "Dozing", + Speciality = "Ingredients" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 3, + PokemonId = 3, + PokemonName = "Venasaur", + SleepType = "Dozing", + Speciality = "Ingredients" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 4, + PokemonId = 4, + PokemonName = "Charmander", + SleepType = "Snoozing", + Speciality = "Ingredients" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 5, + PokemonId = 5, + PokemonName = "Charmeleon", + SleepType = "Snoozing", + Speciality = "Ingredients" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 6, + PokemonId = 6, + PokemonName = "Charizard", + SleepType = "Snoozing", + Speciality = "Ingredients" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 7, + PokemonId = 7, + PokemonName = "Squirtle", + SleepType = "Slumbering", + Speciality = "Ingredients" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 8, + PokemonId = 8, + PokemonName = "Wartortle", + SleepType = "Slumbering", + Speciality = "Ingredients" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 9, + PokemonId = 9, + PokemonName = "Blastoise", + SleepType = "Slumbering", + Speciality = "Ingredients" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 10, + PokemonId = 10, + PokemonName = "Caterpie", + SleepType = "Dozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 11, + PokemonId = 11, + PokemonName = "Metapod", + SleepType = "Dozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 12, + PokemonId = 12, + PokemonName = "Butterfree", + SleepType = "Dozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 13, + PokemonId = 19, + PokemonName = "Rattata", + SleepType = "Snoozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 14, + PokemonId = 20, + PokemonName = "Raticate", + SleepType = "Snoozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 15, + PokemonId = 23, + PokemonName = "Ekans", + SleepType = "Dozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 16, + PokemonId = 24, + PokemonName = "Arbok", + SleepType = "Dozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 17, + PokemonId = 25, + PokemonName = "Pikachu", + SleepType = "Snoozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 18, + PokemonId = 26, + PokemonName = "Raticate", + SleepType = "Snoozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 19, + PokemonId = 35, + PokemonName = "Clefairy", + SleepType = "Snoozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 20, + PokemonId = 36, + PokemonName = "Clefable", + SleepType = "Snoozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 21, + PokemonId = 37, + PokemonName = "Vulpix", + SleepType = "Snoozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 22, + PokemonId = 38, + PokemonName = "Ninetails", + SleepType = "Snoozing", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 23, + PokemonId = 37, + PokemonName = "Vulpix", + IsVariation = true, + VariationName = "Alolan", + SleepType = "Slumbering", + Speciality = "Berries" + + }); + modelBuilder.Entity().HasData(new Pokemon + { + Id = 24, + PokemonId = 38, + PokemonName = "Ninetails", + IsVariation = true, + VariationName = "Alolan", + SleepType = "Slumbering", + Speciality = "Berries" + + }); + } + } } diff --git a/Portfolio.Infrastructure/DependencyInjection.cs b/Portfolio.Infrastructure/DependencyInjection.cs index c109a9e..9d6cfe0 100644 --- a/Portfolio.Infrastructure/DependencyInjection.cs +++ b/Portfolio.Infrastructure/DependencyInjection.cs @@ -1,4 +1,5 @@ -using Microsoft.EntityFrameworkCore; +using AutoMapper; +using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Portfolio.Application.Services.Articles; @@ -17,11 +18,21 @@ namespace Portfolio.Infrastructure { public static IServiceCollection AddInfrastructure( this IServiceCollection services, IConfiguration configuration) { + IMapper mapper = MappingConfig.RegisterMaps().CreateMapper(); + + + services.AddDbContext(options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection") )); + services.AddScoped(); + + services.AddSingleton(mapper); + services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies()); + + return services; } } diff --git a/Portfolio.Infrastructure/MappingConfig.cs b/Portfolio.Infrastructure/MappingConfig.cs new file mode 100644 index 0000000..65323f7 --- /dev/null +++ b/Portfolio.Infrastructure/MappingConfig.cs @@ -0,0 +1,25 @@ +using AutoMapper; +using Portfolio.Domain.Features.Dtos; +using Portfolio.Domain.Features.Pokemon; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.NetworkInformation; +using System.Text; +using System.Threading.Tasks; + +namespace Portfolio.Infrastructure +{ + public class MappingConfig + { + public static MapperConfiguration RegisterMaps() + { + var mappingConfig = new MapperConfiguration(config => + { + config.CreateMap(); + config.CreateMap(); + }); + return mappingConfig; + } + } +} diff --git a/Portfolio.Infrastructure/Migrations/20250217221142_AddPokemon.Designer.cs b/Portfolio.Infrastructure/Migrations/20250217221142_AddPokemon.Designer.cs new file mode 100644 index 0000000..782b7d2 --- /dev/null +++ b/Portfolio.Infrastructure/Migrations/20250217221142_AddPokemon.Designer.cs @@ -0,0 +1,335 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Metadata; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Portfolio.Infrastructure; + +#nullable disable + +namespace Portfolio.Infrastructure.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20250217221142_AddPokemon")] + partial class AddPokemon + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "9.0.2") + .HasAnnotation("Relational:MaxIdentifierLength", 128); + + SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); + + modelBuilder.Entity("Portfolio.Domain.Features.Pokemon.Pokemon", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("IsVariation") + .HasColumnType("bit"); + + b.Property("PokemonId") + .HasColumnType("int"); + + b.Property("PokemonName") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SleepType") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("Speciality") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("VariationName") + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("Pokemons"); + + b.HasData( + new + { + Id = 1, + IsVariation = false, + PokemonId = 1, + PokemonName = "Bulbasaur", + SleepType = "Dozing", + Speciality = "Ingredients" + }, + new + { + Id = 2, + IsVariation = false, + PokemonId = 2, + PokemonName = "Ivysaur", + SleepType = "Dozing", + Speciality = "Ingredients" + }, + new + { + Id = 3, + IsVariation = false, + PokemonId = 3, + PokemonName = "Venasaur", + SleepType = "Dozing", + Speciality = "Ingredients" + }, + new + { + Id = 4, + IsVariation = false, + PokemonId = 4, + PokemonName = "Charmander", + SleepType = "Snoozing", + Speciality = "Ingredients" + }, + new + { + Id = 5, + IsVariation = false, + PokemonId = 5, + PokemonName = "Charmeleon", + SleepType = "Snoozing", + Speciality = "Ingredients" + }, + new + { + Id = 6, + IsVariation = false, + PokemonId = 6, + PokemonName = "Charizard", + SleepType = "Snoozing", + Speciality = "Ingredients" + }, + new + { + Id = 7, + IsVariation = false, + PokemonId = 7, + PokemonName = "Squirtle", + SleepType = "Slumbering", + Speciality = "Ingredients" + }, + new + { + Id = 8, + IsVariation = false, + PokemonId = 8, + PokemonName = "Wartortle", + SleepType = "Slumbering", + Speciality = "Ingredients" + }, + new + { + Id = 9, + IsVariation = false, + PokemonId = 9, + PokemonName = "Blastoise", + SleepType = "Slumbering", + Speciality = "Ingredients" + }, + new + { + Id = 10, + IsVariation = false, + PokemonId = 10, + PokemonName = "Caterpie", + SleepType = "Dozing", + Speciality = "Berries" + }, + new + { + Id = 11, + IsVariation = false, + PokemonId = 11, + PokemonName = "Metapod", + SleepType = "Dozing", + Speciality = "Berries" + }, + new + { + Id = 12, + IsVariation = false, + PokemonId = 12, + PokemonName = "Butterfree", + SleepType = "Dozing", + Speciality = "Berries" + }, + new + { + Id = 13, + IsVariation = false, + PokemonId = 19, + PokemonName = "Rattata", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 14, + IsVariation = false, + PokemonId = 20, + PokemonName = "Raticate", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 15, + IsVariation = false, + PokemonId = 23, + PokemonName = "Ekans", + SleepType = "Dozing", + Speciality = "Berries" + }, + new + { + Id = 16, + IsVariation = false, + PokemonId = 24, + PokemonName = "Arbok", + SleepType = "Dozing", + Speciality = "Berries" + }, + new + { + Id = 17, + IsVariation = false, + PokemonId = 25, + PokemonName = "Pikachu", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 18, + IsVariation = false, + PokemonId = 26, + PokemonName = "Raticate", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 19, + IsVariation = false, + PokemonId = 35, + PokemonName = "Clefairy", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 20, + IsVariation = false, + PokemonId = 36, + PokemonName = "Clefable", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 21, + IsVariation = false, + PokemonId = 37, + PokemonName = "Vulpix", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 22, + IsVariation = false, + PokemonId = 38, + PokemonName = "Ninetails", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 23, + IsVariation = true, + PokemonId = 37, + PokemonName = "Vulpix", + SleepType = "Slumbering", + Speciality = "Berries", + VariationName = "Alolan" + }, + new + { + Id = 24, + IsVariation = true, + PokemonId = 38, + PokemonName = "Ninetails", + SleepType = "Slumbering", + Speciality = "Berries", + VariationName = "Alolan" + }); + }); + + modelBuilder.Entity("Portfolio.Domain.Features.Pokemon_Natures.PokemonNatures", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BerryRating") + .HasColumnType("int"); + + b.Property("IngredientRating") + .HasColumnType("int"); + + b.Property("Nature") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.Property("SkillRating") + .HasColumnType("int"); + + b.HasKey("Id"); + + b.ToTable("PokemonNatures"); + }); + + modelBuilder.Entity("Portfolio.Domain.Features.Pokemon_Subskills.PokemonSubskills", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("int"); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property("Id")); + + b.Property("BerryRank") + .HasColumnType("int"); + + b.Property("IngredientRank") + .HasColumnType("int"); + + b.Property("SkillRank") + .HasColumnType("int"); + + b.Property("SubSkill") + .IsRequired() + .HasColumnType("nvarchar(max)"); + + b.HasKey("Id"); + + b.ToTable("PokemonSubskills"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Portfolio.Infrastructure/Migrations/20250217221142_AddPokemon.cs b/Portfolio.Infrastructure/Migrations/20250217221142_AddPokemon.cs new file mode 100644 index 0000000..0ed1d38 --- /dev/null +++ b/Portfolio.Infrastructure/Migrations/20250217221142_AddPokemon.cs @@ -0,0 +1,171 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional + +namespace Portfolio.Infrastructure.Migrations +{ + /// + public partial class AddPokemon : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.InsertData( + table: "Pokemons", + columns: new[] { "Id", "IsVariation", "PokemonId", "PokemonName", "SleepType", "Speciality", "VariationName" }, + values: new object[,] + { + { 1, false, 1, "Bulbasaur", "Dozing", "Ingredients", null }, + { 2, false, 2, "Ivysaur", "Dozing", "Ingredients", null }, + { 3, false, 3, "Venasaur", "Dozing", "Ingredients", null }, + { 4, false, 4, "Charmander", "Snoozing", "Ingredients", null }, + { 5, false, 5, "Charmeleon", "Snoozing", "Ingredients", null }, + { 6, false, 6, "Charizard", "Snoozing", "Ingredients", null }, + { 7, false, 7, "Squirtle", "Slumbering", "Ingredients", null }, + { 8, false, 8, "Wartortle", "Slumbering", "Ingredients", null }, + { 9, false, 9, "Blastoise", "Slumbering", "Ingredients", null }, + { 10, false, 10, "Caterpie", "Dozing", "Berries", null }, + { 11, false, 11, "Metapod", "Dozing", "Berries", null }, + { 12, false, 12, "Butterfree", "Dozing", "Berries", null }, + { 13, false, 19, "Rattata", "Snoozing", "Berries", null }, + { 14, false, 20, "Raticate", "Snoozing", "Berries", null }, + { 15, false, 23, "Ekans", "Dozing", "Berries", null }, + { 16, false, 24, "Arbok", "Dozing", "Berries", null }, + { 17, false, 25, "Pikachu", "Snoozing", "Berries", null }, + { 18, false, 26, "Raticate", "Snoozing", "Berries", null }, + { 19, false, 35, "Clefairy", "Snoozing", "Berries", null }, + { 20, false, 36, "Clefable", "Snoozing", "Berries", null }, + { 21, false, 37, "Vulpix", "Snoozing", "Berries", null }, + { 22, false, 38, "Ninetails", "Snoozing", "Berries", null }, + { 23, true, 37, "Vulpix", "Slumbering", "Berries", "Alolan" }, + { 24, true, 38, "Ninetails", "Slumbering", "Berries", "Alolan" } + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 1); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 2); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 3); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 4); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 5); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 6); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 7); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 8); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 9); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 10); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 11); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 12); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 13); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 14); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 15); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 16); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 17); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 18); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 19); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 20); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 21); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 22); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 23); + + migrationBuilder.DeleteData( + table: "Pokemons", + keyColumn: "Id", + keyValue: 24); + } + } +} diff --git a/Portfolio.Infrastructure/Migrations/ApplicationDbContextModelSnapshot.cs b/Portfolio.Infrastructure/Migrations/ApplicationDbContextModelSnapshot.cs index 93ef60a..c1dc41b 100644 --- a/Portfolio.Infrastructure/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/Portfolio.Infrastructure/Migrations/ApplicationDbContextModelSnapshot.cs @@ -53,6 +53,226 @@ namespace Portfolio.Infrastructure.Migrations b.HasKey("Id"); b.ToTable("Pokemons"); + + b.HasData( + new + { + Id = 1, + IsVariation = false, + PokemonId = 1, + PokemonName = "Bulbasaur", + SleepType = "Dozing", + Speciality = "Ingredients" + }, + new + { + Id = 2, + IsVariation = false, + PokemonId = 2, + PokemonName = "Ivysaur", + SleepType = "Dozing", + Speciality = "Ingredients" + }, + new + { + Id = 3, + IsVariation = false, + PokemonId = 3, + PokemonName = "Venasaur", + SleepType = "Dozing", + Speciality = "Ingredients" + }, + new + { + Id = 4, + IsVariation = false, + PokemonId = 4, + PokemonName = "Charmander", + SleepType = "Snoozing", + Speciality = "Ingredients" + }, + new + { + Id = 5, + IsVariation = false, + PokemonId = 5, + PokemonName = "Charmeleon", + SleepType = "Snoozing", + Speciality = "Ingredients" + }, + new + { + Id = 6, + IsVariation = false, + PokemonId = 6, + PokemonName = "Charizard", + SleepType = "Snoozing", + Speciality = "Ingredients" + }, + new + { + Id = 7, + IsVariation = false, + PokemonId = 7, + PokemonName = "Squirtle", + SleepType = "Slumbering", + Speciality = "Ingredients" + }, + new + { + Id = 8, + IsVariation = false, + PokemonId = 8, + PokemonName = "Wartortle", + SleepType = "Slumbering", + Speciality = "Ingredients" + }, + new + { + Id = 9, + IsVariation = false, + PokemonId = 9, + PokemonName = "Blastoise", + SleepType = "Slumbering", + Speciality = "Ingredients" + }, + new + { + Id = 10, + IsVariation = false, + PokemonId = 10, + PokemonName = "Caterpie", + SleepType = "Dozing", + Speciality = "Berries" + }, + new + { + Id = 11, + IsVariation = false, + PokemonId = 11, + PokemonName = "Metapod", + SleepType = "Dozing", + Speciality = "Berries" + }, + new + { + Id = 12, + IsVariation = false, + PokemonId = 12, + PokemonName = "Butterfree", + SleepType = "Dozing", + Speciality = "Berries" + }, + new + { + Id = 13, + IsVariation = false, + PokemonId = 19, + PokemonName = "Rattata", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 14, + IsVariation = false, + PokemonId = 20, + PokemonName = "Raticate", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 15, + IsVariation = false, + PokemonId = 23, + PokemonName = "Ekans", + SleepType = "Dozing", + Speciality = "Berries" + }, + new + { + Id = 16, + IsVariation = false, + PokemonId = 24, + PokemonName = "Arbok", + SleepType = "Dozing", + Speciality = "Berries" + }, + new + { + Id = 17, + IsVariation = false, + PokemonId = 25, + PokemonName = "Pikachu", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 18, + IsVariation = false, + PokemonId = 26, + PokemonName = "Raticate", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 19, + IsVariation = false, + PokemonId = 35, + PokemonName = "Clefairy", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 20, + IsVariation = false, + PokemonId = 36, + PokemonName = "Clefable", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 21, + IsVariation = false, + PokemonId = 37, + PokemonName = "Vulpix", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 22, + IsVariation = false, + PokemonId = 38, + PokemonName = "Ninetails", + SleepType = "Snoozing", + Speciality = "Berries" + }, + new + { + Id = 23, + IsVariation = true, + PokemonId = 37, + PokemonName = "Vulpix", + SleepType = "Slumbering", + Speciality = "Berries", + VariationName = "Alolan" + }, + new + { + Id = 24, + IsVariation = true, + PokemonId = 38, + PokemonName = "Ninetails", + SleepType = "Slumbering", + Speciality = "Berries", + VariationName = "Alolan" + }); }); modelBuilder.Entity("Portfolio.Domain.Features.Pokemon_Natures.PokemonNatures", b => diff --git a/Portfolio.Infrastructure/Portfolio.Infrastructure.csproj b/Portfolio.Infrastructure/Portfolio.Infrastructure.csproj index 20ed18b..0574950 100644 --- a/Portfolio.Infrastructure/Portfolio.Infrastructure.csproj +++ b/Portfolio.Infrastructure/Portfolio.Infrastructure.csproj @@ -7,6 +7,7 @@ + diff --git a/Portfolio.Infrastructure/Repositories/PokemonRepository.cs b/Portfolio.Infrastructure/Repositories/PokemonRepository.cs index ac893cf..4ff4129 100644 --- a/Portfolio.Infrastructure/Repositories/PokemonRepository.cs +++ b/Portfolio.Infrastructure/Repositories/PokemonRepository.cs @@ -1,5 +1,10 @@ -using Microsoft.EntityFrameworkCore; +using AutoMapper; +using Azure; +using Microsoft.EntityFrameworkCore; +using Portfolio.Application.Services; +using Portfolio.Domain.Features.Dtos; using Portfolio.Domain.Features.Pokemon; +using Portfolio.Domain.Utility; using System; using System.Collections.Generic; using System.Linq; @@ -11,15 +16,40 @@ namespace Portfolio.Infrastructure.Repositories public class PokemonRepository : IPokemonRepository { private readonly ApplicationDbContext _context; + private readonly IBaseService _baseService; + private ResponseDto _response; + private IMapper _mapper; - public PokemonRepository(ApplicationDbContext context) + public PokemonRepository(ApplicationDbContext context, IMapper mapper, IBaseService baseService) { _context = context; + _baseService = baseService; + _mapper = mapper; + _response = new ResponseDto(); + } - public async Task> GetAllPokemonsAsync() + + public async Task GetAllPokemon() { - return await _context.Pokemons.ToListAsync(); + return await _baseService.SendAsync(new RequestDto() + { + ApiType = StaticDetails.ApiType.GET, + Url = StaticDetails.PokemonSleepAPIBase + "/api/pokemon" + }); + //try + //{ + // IEnumerable objList = _context.Pokemons.ToList(); + // _response.IsSuccess = true; + // _response.Message = "All Pokemon found."; + // _response.Result = _mapper.Map>(objList); + //} + //catch (Exception ex) + //{ + // _response.IsSuccess = false; + // _response.Message = ex.Message; + //} + //return _response; } } } diff --git a/Portfolio.WebUI.Server/Components/Pages/PokemonSleep.razor b/Portfolio.WebUI.Server/Components/Pages/PokemonSleep.razor index 80d5e73..2ac61b5 100644 --- a/Portfolio.WebUI.Server/Components/Pages/PokemonSleep.razor +++ b/Portfolio.WebUI.Server/Components/Pages/PokemonSleep.razor @@ -1,4 +1,5 @@ @page "/pokemonsleep" + @inject IPokemonService PokemonService @attribute [StreamRendering] @@ -11,6 +12,10 @@ { Loading... } +else if(somethingWrong) +{ + Something went wrong... +} else { @@ -45,25 +50,29 @@ else @foreach (var pokemon in pokemons) { - string URL = "https://www.serebii.net/pokemonsleep/pokemon/" + pokemon.Id + ".png"; - string ShinyURL = "https://www.serebii.net/pokemonsleep/pokemon/shiny/" + pokemon.Id + ".png"; + @if(pokemon.Id == 3) + { + string URL = "https://www.serebii.net/pokemonsleep/pokemon/" + pokemon.Id + ".png"; + string ShinyURL = "https://www.serebii.net/pokemonsleep/pokemon/shiny/" + pokemon.Id + ".png"; - - - - - - @pokemon.Id - @pokemon.PokemonName - @pokemon.SleepType - @pokemon.Speciality - - - - - + + + + + + @pokemon.Id + @pokemon.PokemonName + @pokemon.SleepType + @pokemon.Speciality + + + + + - + + + } } @@ -74,17 +83,24 @@ else } @code { - private List pokemons = new List(); + private List pokemons = new List(); + private bool somethingWrong = false; protected override async Task OnInitializedAsync() { // Simulate asynchronous loading to demonstrate streaming rendering await Task.Delay(500); + var result = await PokemonService.GetAllPokemonAsync(); if (result is not null) { + somethingWrong = false; pokemons = result; } + else + { + somethingWrong = true; + } } } diff --git a/Portfolio.WebUI.Server/Components/_Imports.razor b/Portfolio.WebUI.Server/Components/_Imports.razor index 5dda988..abf1f5e 100644 --- a/Portfolio.WebUI.Server/Components/_Imports.razor +++ b/Portfolio.WebUI.Server/Components/_Imports.razor @@ -10,6 +10,8 @@ @using Portfolio.WebUI.Server.Components @using Portfolio.Domain.Features.Articles @using Portfolio.Domain.Features.Pokemon +@using Portfolio.Domain.Features.Dtos @using Portfolio.Application.Services.Articles @using Portfolio.Application.Services.PokemonService +
Loading...
Something went wrong...