diff --git a/StarWarsKb.Back/Controllers/CharactersController.cs b/StarWarsKb.Back/Controllers/CharactersController.cs index 423d52e..5a6dd8f 100644 --- a/StarWarsKb.Back/Controllers/CharactersController.cs +++ b/StarWarsKb.Back/Controllers/CharactersController.cs @@ -9,9 +9,9 @@ namespace StarWarsKb.Back.Controllers [Route("[controller]")] public class CharactersController : ControllerBase { - private readonly ICharactersRepository _charactersRepository; + private readonly IBaseRepository _charactersRepository; - public CharactersController(ICharactersRepository charactersRepository) + public CharactersController(IBaseRepository charactersRepository) { _charactersRepository = charactersRepository; } @@ -20,6 +20,10 @@ public CharactersController(ICharactersRepository charactersRepository) public IList Get() => _charactersRepository.GetAll(); [HttpGet("{id}")] - public Character Get(int id) => _charactersRepository.GetById(id); + public Character Get(int id) + { + var byId = _charactersRepository.GetById(id); + return byId; + } } } \ No newline at end of file diff --git a/StarWarsKb.Back/Migrations/20220402083803_parsemigration.Designer.cs b/StarWarsKb.Back/Migrations/20220402083803_parsemigration.Designer.cs new file mode 100644 index 0000000..3fe72f6 --- /dev/null +++ b/StarWarsKb.Back/Migrations/20220402083803_parsemigration.Designer.cs @@ -0,0 +1,213 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using StarWarsKb.Back.Model; + +namespace StarWarsKb.Back.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20220402083803_parsemigration")] + partial class parsemigration + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) + .HasAnnotation("ProductVersion", "3.1.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.Character", b => + { + b.Property("StarWarId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("BirthYear") + .HasColumnType("text"); + + b.Property("EyeColor") + .HasColumnType("text"); + + b.Property("Gender") + .HasColumnType("text"); + + b.Property("HairColor") + .HasColumnType("text"); + + b.Property("Height") + .HasColumnType("integer"); + + b.Property("HomeWorldId") + .HasColumnType("integer"); + + b.Property("Mass") + .HasColumnType("double precision"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("SkinColor") + .HasColumnType("text"); + + b.HasKey("StarWarId"); + + b.HasIndex("HomeWorldId"); + + b.ToTable("Characters"); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.Film", b => + { + b.Property("StarWarId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("EpisodeId") + .HasColumnType("integer"); + + b.Property("Title") + .HasColumnType("text"); + + b.HasKey("StarWarId"); + + b.ToTable("Films"); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.Planet", b => + { + b.Property("StarWarId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("Climate") + .HasColumnType("text"); + + b.Property("Diameter") + .HasColumnType("integer"); + + b.Property("Gravity") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("OrbitalPeriod") + .HasColumnType("integer"); + + b.Property("Population") + .HasColumnType("integer"); + + b.Property("RotationPeriod") + .HasColumnType("integer"); + + b.Property("SurfaceWater") + .HasColumnType("text"); + + b.Property("Terrain") + .HasColumnType("text"); + + b.HasKey("StarWarId"); + + b.ToTable("Planets"); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.Starship", b => + { + b.Property("StarWarId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("CargoCapacity") + .HasColumnType("bigint"); + + b.Property("Consumables") + .HasColumnType("text"); + + b.Property("CostInCredits") + .HasColumnType("bigint"); + + b.Property("CrewCount") + .HasColumnType("integer"); + + b.Property("HyperdriveRating") + .HasColumnType("text"); + + b.Property("Lenght") + .HasColumnType("integer"); + + b.Property("MGLT") + .HasColumnType("text"); + + b.Property("Manufacturer") + .HasColumnType("text"); + + b.Property("MaxAtmospheringSpeed") + .HasColumnType("text"); + + b.Property("Model") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("PassengersCount") + .HasColumnType("integer"); + + b.Property("StarshipClass") + .HasColumnType("text"); + + b.HasKey("StarWarId"); + + b.ToTable("Starships"); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.StarshipsCharacters", b => + { + b.Property("CharacterId") + .HasColumnType("integer"); + + b.Property("StarshipId") + .HasColumnType("integer"); + + b.HasKey("CharacterId", "StarshipId"); + + b.HasIndex("StarshipId"); + + b.ToTable("StarshipsCharacters"); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.Character", b => + { + b.HasOne("StarWarsKb.Infrastructure.Model.Planet", "HomeWorld") + .WithMany("Residents") + .HasForeignKey("HomeWorldId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.StarshipsCharacters", b => + { + b.HasOne("StarWarsKb.Infrastructure.Model.Character", "Character") + .WithMany("StarshipCharacters") + .HasForeignKey("CharacterId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("StarWarsKb.Infrastructure.Model.Starship", "Starship") + .WithMany("StarshipsCharacters") + .HasForeignKey("StarshipId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/StarWarsKb.Back/Migrations/20220402083803_parsemigration.cs b/StarWarsKb.Back/Migrations/20220402083803_parsemigration.cs new file mode 100644 index 0000000..8da7a45 --- /dev/null +++ b/StarWarsKb.Back/Migrations/20220402083803_parsemigration.cs @@ -0,0 +1,89 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace StarWarsKb.Back.Migrations +{ + public partial class parsemigration : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AddColumn( + name: "Climate", + table: "Planets", + nullable: true); + + migrationBuilder.AddColumn( + name: "Diameter", + table: "Planets", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "Gravity", + table: "Planets", + nullable: true); + + migrationBuilder.AddColumn( + name: "OrbitalPeriod", + table: "Planets", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "Population", + table: "Planets", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "RotationPeriod", + table: "Planets", + nullable: false, + defaultValue: 0); + + migrationBuilder.AddColumn( + name: "SurfaceWater", + table: "Planets", + nullable: true); + + migrationBuilder.AddColumn( + name: "Terrain", + table: "Planets", + nullable: true); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropColumn( + name: "Climate", + table: "Planets"); + + migrationBuilder.DropColumn( + name: "Diameter", + table: "Planets"); + + migrationBuilder.DropColumn( + name: "Gravity", + table: "Planets"); + + migrationBuilder.DropColumn( + name: "OrbitalPeriod", + table: "Planets"); + + migrationBuilder.DropColumn( + name: "Population", + table: "Planets"); + + migrationBuilder.DropColumn( + name: "RotationPeriod", + table: "Planets"); + + migrationBuilder.DropColumn( + name: "SurfaceWater", + table: "Planets"); + + migrationBuilder.DropColumn( + name: "Terrain", + table: "Planets"); + } + } +} diff --git a/StarWarsKb.Back/Migrations/20220402130523_parsemigration1.Designer.cs b/StarWarsKb.Back/Migrations/20220402130523_parsemigration1.Designer.cs new file mode 100644 index 0000000..6471166 --- /dev/null +++ b/StarWarsKb.Back/Migrations/20220402130523_parsemigration1.Designer.cs @@ -0,0 +1,213 @@ +// +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using StarWarsKb.Back.Model; + +namespace StarWarsKb.Back.Migrations +{ + [DbContext(typeof(ApplicationDbContext))] + [Migration("20220402130523_parsemigration1")] + partial class parsemigration1 + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn) + .HasAnnotation("ProductVersion", "3.1.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.Character", b => + { + b.Property("StarWarId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("BirthYear") + .HasColumnType("text"); + + b.Property("EyeColor") + .HasColumnType("text"); + + b.Property("Gender") + .HasColumnType("text"); + + b.Property("HairColor") + .HasColumnType("text"); + + b.Property("Height") + .HasColumnType("integer"); + + b.Property("HomeWorldId") + .HasColumnType("integer"); + + b.Property("Mass") + .HasColumnType("double precision"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("SkinColor") + .HasColumnType("text"); + + b.HasKey("StarWarId"); + + b.HasIndex("HomeWorldId"); + + b.ToTable("Characters"); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.Film", b => + { + b.Property("StarWarId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("EpisodeId") + .HasColumnType("integer"); + + b.Property("Title") + .HasColumnType("text"); + + b.HasKey("StarWarId"); + + b.ToTable("Films"); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.Planet", b => + { + b.Property("StarWarId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("Climate") + .HasColumnType("text"); + + b.Property("Diameter") + .HasColumnType("integer"); + + b.Property("Gravity") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("OrbitalPeriod") + .HasColumnType("integer"); + + b.Property("Population") + .HasColumnType("integer"); + + b.Property("RotationPeriod") + .HasColumnType("integer"); + + b.Property("SurfaceWater") + .HasColumnType("text"); + + b.Property("Terrain") + .HasColumnType("text"); + + b.HasKey("StarWarId"); + + b.ToTable("Planets"); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.Starship", b => + { + b.Property("StarWarId") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + + b.Property("CargoCapacity") + .HasColumnType("bigint"); + + b.Property("Consumables") + .HasColumnType("text"); + + b.Property("CostInCredits") + .HasColumnType("bigint"); + + b.Property("CrewCount") + .HasColumnType("integer"); + + b.Property("HyperdriveRating") + .HasColumnType("text"); + + b.Property("Lenght") + .HasColumnType("integer"); + + b.Property("MGLT") + .HasColumnType("text"); + + b.Property("Manufacturer") + .HasColumnType("text"); + + b.Property("MaxAtmospheringSpeed") + .HasColumnType("text"); + + b.Property("Model") + .HasColumnType("text"); + + b.Property("Name") + .HasColumnType("text"); + + b.Property("PassengersCount") + .HasColumnType("integer"); + + b.Property("StarshipClass") + .HasColumnType("text"); + + b.HasKey("StarWarId"); + + b.ToTable("Starships"); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.StarshipsCharacters", b => + { + b.Property("CharacterId") + .HasColumnType("integer"); + + b.Property("StarshipId") + .HasColumnType("integer"); + + b.HasKey("CharacterId", "StarshipId"); + + b.HasIndex("StarshipId"); + + b.ToTable("StarshipsCharacters"); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.Character", b => + { + b.HasOne("StarWarsKb.Infrastructure.Model.Planet", "HomeWorld") + .WithMany("Residents") + .HasForeignKey("HomeWorldId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("StarWarsKb.Infrastructure.Model.StarshipsCharacters", b => + { + b.HasOne("StarWarsKb.Infrastructure.Model.Character", "Character") + .WithMany("StarshipCharacters") + .HasForeignKey("CharacterId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("StarWarsKb.Infrastructure.Model.Starship", "Starship") + .WithMany("StarshipsCharacters") + .HasForeignKey("StarshipId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/StarWarsKb.Back/Migrations/20220402130523_parsemigration1.cs b/StarWarsKb.Back/Migrations/20220402130523_parsemigration1.cs new file mode 100644 index 0000000..7de4d8f --- /dev/null +++ b/StarWarsKb.Back/Migrations/20220402130523_parsemigration1.cs @@ -0,0 +1,17 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +namespace StarWarsKb.Back.Migrations +{ + public partial class parsemigration1 : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/StarWarsKb.Back/Migrations/ApplicationDbContextModelSnapshot.cs b/StarWarsKb.Back/Migrations/ApplicationDbContextModelSnapshot.cs index 728d33f..5b8afa3 100644 --- a/StarWarsKb.Back/Migrations/ApplicationDbContextModelSnapshot.cs +++ b/StarWarsKb.Back/Migrations/ApplicationDbContextModelSnapshot.cs @@ -84,9 +84,33 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("integer") .HasAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn); + b.Property("Climate") + .HasColumnType("text"); + + b.Property("Diameter") + .HasColumnType("integer"); + + b.Property("Gravity") + .HasColumnType("text"); + b.Property("Name") .HasColumnType("text"); + b.Property("OrbitalPeriod") + .HasColumnType("integer"); + + b.Property("Population") + .HasColumnType("integer"); + + b.Property("RotationPeriod") + .HasColumnType("integer"); + + b.Property("SurfaceWater") + .HasColumnType("text"); + + b.Property("Terrain") + .HasColumnType("text"); + b.HasKey("StarWarId"); b.ToTable("Planets"); diff --git a/StarWarsKb.Back/Model/ApplicationDbContext.cs b/StarWarsKb.Back/Model/ApplicationDbContext.cs index 5c4275c..eeab606 100644 --- a/StarWarsKb.Back/Model/ApplicationDbContext.cs +++ b/StarWarsKb.Back/Model/ApplicationDbContext.cs @@ -47,7 +47,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder) .WithOne(ch => ch.HomeWorld) .HasForeignKey(ch1 => ch1.HomeWorldId); - modelBuilder.Entity().HasKey(x => x.StarWarId); } } diff --git a/StarWarsKb.Back/Model/CharacterService.cs b/StarWarsKb.Back/Model/CharacterService.cs new file mode 100644 index 0000000..b47e835 --- /dev/null +++ b/StarWarsKb.Back/Model/CharacterService.cs @@ -0,0 +1,43 @@ +using System; +using StarWarsKb.Back.Model.POCO; +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public class CharacterService : ICharacterService + { + public Character CreateFromPOCO(CharacterPOCO characterPoco) + { + //TODO: написать тесты + var ch = new Character + { + BirthYear = characterPoco.birth_year, + EyeColor = characterPoco.eye_color, + HairColor = characterPoco.hair_color, + Gender = characterPoco.gender, + SkinColor = characterPoco.skin_color, + Name = characterPoco.name, + StarWarId = characterPoco.id + }; + + var homeWorldId = characterPoco.homeworld + .Replace(@"https://swapi.dev/api/planets/", "") + .Replace(@"/",""); + + ch.HomeWorldId = int.Parse(homeWorldId); + + ch.Height = Int32.TryParse(characterPoco.height, out var height) ? height : -1; + + ch.Mass = double.TryParse(characterPoco.mass, out var mass) ? mass : -1; + + foreach (var url in characterPoco.starships) + { + var starshipId = url.Replace(@"https://swapi.dev/api/starships/", "") + .Replace(@"/",""); + ch.StarshipCharacters.Add(new StarshipsCharacters{CharacterId = characterPoco.id, StarshipId = int.Parse(starshipId)}); + } + + return ch; + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/CharactersRepository.cs b/StarWarsKb.Back/Model/CharactersRepository.cs new file mode 100644 index 0000000..027f75d --- /dev/null +++ b/StarWarsKb.Back/Model/CharactersRepository.cs @@ -0,0 +1,39 @@ +using System.Collections.Generic; +using System.Linq; +using Microsoft.EntityFrameworkCore; +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public class CharactersRepository : IBaseRepository + { + private readonly ApplicationDbContext _context; + + public CharactersRepository(ApplicationDbContext context) + { + _context = context; + } + public IList GetAll() + { + return _context.Characters + .Include(c => c.StarshipCharacters) + .Include(cc => cc.HomeWorld) + .ToList(); + } + public Character GetById(int id) + { + return _context.Characters + .Include(c => c.StarshipCharacters) + .Include(cc => cc.HomeWorld) + .First(x => x.StarWarId == id); + } + public void Save(Character c) + { + _context.Characters.Add(c); + } + public void Save(IList characters) + { + _context.Characters.AddRange(characters); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/IBaseRepository.cs b/StarWarsKb.Back/Model/IBaseRepository.cs new file mode 100644 index 0000000..79421f3 --- /dev/null +++ b/StarWarsKb.Back/Model/IBaseRepository.cs @@ -0,0 +1,13 @@ +using System.Collections.Generic; +using StarWars.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public interface IBaseRepository where T : IStarWarsEntity + { + IList GetAll(); + T GetById(int id); + void Save(T entity); + void Save(IList entities); + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/ICharacterService.cs b/StarWarsKb.Back/Model/ICharacterService.cs new file mode 100644 index 0000000..71b1921 --- /dev/null +++ b/StarWarsKb.Back/Model/ICharacterService.cs @@ -0,0 +1,10 @@ +using StarWarsKb.Back.Model.POCO; +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public interface ICharacterService + { + Character CreateFromPOCO(CharacterPOCO characterPoco); + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/ICharactersRepository.cs b/StarWarsKb.Back/Model/ICharactersRepository.cs deleted file mode 100644 index a7ff441..0000000 --- a/StarWarsKb.Back/Model/ICharactersRepository.cs +++ /dev/null @@ -1,11 +0,0 @@ -using System.Collections.Generic; -using StarWarsKb.Infrastructure.Model; - -namespace StarWarsKb.Back.Model -{ - public interface ICharactersRepository - { - IList GetAll(); - Character GetById(int id); - } -} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/IPlanetService.cs b/StarWarsKb.Back/Model/IPlanetService.cs new file mode 100644 index 0000000..532b795 --- /dev/null +++ b/StarWarsKb.Back/Model/IPlanetService.cs @@ -0,0 +1,10 @@ +using StarWarsKb.Back.Model.POCO; +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public interface IPlanetService + { + Planet CreateFromPOCO(PlanetPOCO planetPoco); + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/IStarshipService.cs b/StarWarsKb.Back/Model/IStarshipService.cs new file mode 100644 index 0000000..8f30451 --- /dev/null +++ b/StarWarsKb.Back/Model/IStarshipService.cs @@ -0,0 +1,10 @@ +using StarWarsKb.Back.Model.POCO; +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public interface IStarshipService + { + Starship CreateFromPOCO(StarshipPOCO poco); + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/IUnitOfWork.cs b/StarWarsKb.Back/Model/IUnitOfWork.cs new file mode 100644 index 0000000..bb99366 --- /dev/null +++ b/StarWarsKb.Back/Model/IUnitOfWork.cs @@ -0,0 +1,7 @@ +namespace StarWarsKb.Back.Model +{ + public interface IUnitOfWork + { + public void SaveChanges(); + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/IUpdateService.cs b/StarWarsKb.Back/Model/IUpdateService.cs new file mode 100644 index 0000000..beaf551 --- /dev/null +++ b/StarWarsKb.Back/Model/IUpdateService.cs @@ -0,0 +1,8 @@ +namespace StarWarsKb.Back.Model +{ + public interface IUpdateService + { + void RunUpdate(); + void CreateData(); + } +} \ No newline at end of file diff --git a/StarWarsKb.Infrastructure/Services/IWebReader.cs b/StarWarsKb.Back/Model/IWebReader.cs similarity index 55% rename from StarWarsKb.Infrastructure/Services/IWebReader.cs rename to StarWarsKb.Back/Model/IWebReader.cs index 5d514f1..d43a7ae 100644 --- a/StarWarsKb.Infrastructure/Services/IWebReader.cs +++ b/StarWarsKb.Back/Model/IWebReader.cs @@ -1,6 +1,6 @@ -using StarWarsKb.Infrastructure.Services.POCO; +using StarWarsKb.Back.Model.POCO; -namespace StarWarsKb.Infrastructure.Services +namespace StarWarsKb.Back.Model { public interface IWebReader where T : IStarWarPOCOEntity { diff --git a/StarWarsKb.Infrastructure/Services/POCO/CharacterPOCO.cs b/StarWarsKb.Back/Model/POCO/CharacterPOCO.cs similarity index 77% rename from StarWarsKb.Infrastructure/Services/POCO/CharacterPOCO.cs rename to StarWarsKb.Back/Model/POCO/CharacterPOCO.cs index 8754aee..e5b3e67 100644 --- a/StarWarsKb.Infrastructure/Services/POCO/CharacterPOCO.cs +++ b/StarWarsKb.Back/Model/POCO/CharacterPOCO.cs @@ -1,10 +1,11 @@ using System; using System.Collections.Generic; -namespace StarWarsKb.Infrastructure.Services.POCO +namespace StarWarsKb.Back.Model.POCO { public class CharacterPOCO : IStarWarPOCOEntity { + public int id { get; set; } public string name { get; set; } public string height { get; set; } public string mass { get; set; } @@ -21,5 +22,12 @@ public class CharacterPOCO : IStarWarPOCOEntity public DateTime created { get; set; } public DateTime edited { get; set; } public string url { get; set; } + + public override string ToString() + { + return name; + } + + public static CharacterPOCO Empty => new CharacterPOCO {name = "EMPTY"}; } } \ No newline at end of file diff --git a/StarWarsKb.Back/Model/POCO/IStarWarPOCOEntity.cs b/StarWarsKb.Back/Model/POCO/IStarWarPOCOEntity.cs new file mode 100644 index 0000000..6461f83 --- /dev/null +++ b/StarWarsKb.Back/Model/POCO/IStarWarPOCOEntity.cs @@ -0,0 +1,9 @@ +namespace StarWarsKb.Back.Model.POCO +{ + public interface IStarWarPOCOEntity + { + public string url { get; set; } + public string name { get; set; } + public int id { get; set; } + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/POCO/PlanetPOCO.cs b/StarWarsKb.Back/Model/POCO/PlanetPOCO.cs new file mode 100644 index 0000000..1205f54 --- /dev/null +++ b/StarWarsKb.Back/Model/POCO/PlanetPOCO.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; + +namespace StarWarsKb.Back.Model.POCO +{ + public class PlanetPOCO : IStarWarPOCOEntity + { + public PlanetPOCO() + { + residents = new List(); + films = new List(); + } + + public int id { get; set; } + public string url { get; set; } + public string name { get; set; } + public string rotation_period { get; set; } + public string orbital_period { get; set; } + public string diameter { get; set; } + public string climate { get; set; } + public string gravity { get; set; } + public string terrain { get; set; } + public string surface_water { get; set; } + public string population { get; set; } + public IList residents { get; set; } + public IList films { get; set; } + public DateTime created { get; set; } + public DateTime edited { get; set; } + + public override string ToString() + { + return name; + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Infrastructure/Services/POCO/StarshipPOCO.cs b/StarWarsKb.Back/Model/POCO/StarshipPOCO.cs similarity index 85% rename from StarWarsKb.Infrastructure/Services/POCO/StarshipPOCO.cs rename to StarWarsKb.Back/Model/POCO/StarshipPOCO.cs index b92a50f..3deb92e 100644 --- a/StarWarsKb.Infrastructure/Services/POCO/StarshipPOCO.cs +++ b/StarWarsKb.Back/Model/POCO/StarshipPOCO.cs @@ -1,10 +1,11 @@ using System; using System.Collections.Generic; -namespace StarWarsKb.Infrastructure.Services.POCO +namespace StarWarsKb.Back.Model.POCO { public class StarshipPOCO : IStarWarPOCOEntity { + public int id { get; set; } public string name { get; set; } public string model { get; set; } public string manufacturer { get; set; } @@ -23,5 +24,10 @@ public class StarshipPOCO : IStarWarPOCOEntity public DateTime created { get; set; } public DateTime edited { get; set; } public string url { get; set; } + + public override string ToString() + { + return name; + } } } \ No newline at end of file diff --git a/StarWarsKb.Back/Model/PlanetService.cs b/StarWarsKb.Back/Model/PlanetService.cs new file mode 100644 index 0000000..f0e4006 --- /dev/null +++ b/StarWarsKb.Back/Model/PlanetService.cs @@ -0,0 +1,27 @@ +using StarWarsKb.Back.Model.POCO; +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public class PlanetService : IPlanetService + { + public Planet CreateFromPOCO(PlanetPOCO planetPoco) + { + var planet = new Planet + { + Climate = planetPoco.climate, + Gravity = planetPoco.gravity, + Name = planetPoco.name, + Terrain = planetPoco.terrain, + SurfaceWater = planetPoco.surface_water, + Diameter = int.TryParse(planetPoco.diameter, out var diameter) ? diameter : -1, + Population = int.TryParse(planetPoco.population, out var population) ? population : -1, + OrbitalPeriod = int.TryParse(planetPoco.orbital_period, out var orbital) ? orbital : -1, + RotationPeriod = int.TryParse(planetPoco.rotation_period, out var rotation) ? rotation : -1, + StarWarId = planetPoco.id + }; + + return planet; + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/PlanetsRepository.cs b/StarWarsKb.Back/Model/PlanetsRepository.cs new file mode 100644 index 0000000..8176bfc --- /dev/null +++ b/StarWarsKb.Back/Model/PlanetsRepository.cs @@ -0,0 +1,34 @@ +using System.Collections.Generic; +using System.Linq; +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public class PlanetsRepository : IBaseRepository + { + private readonly ApplicationDbContext _dbContext; + + public PlanetsRepository(ApplicationDbContext dbContext) + { + _dbContext = dbContext; + } + public IList GetAll() + { + return _dbContext.Planets.ToList(); + } + + public Planet GetById(int id) + { + return _dbContext.Planets.First(x => x.StarWarId == id); + } + + public void Save(Planet entity) + { + _dbContext.Planets.Add(entity); + } + public void Save(IList entities) + { + _dbContext.Planets.AddRange(entities); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/StarshipService.cs b/StarWarsKb.Back/Model/StarshipService.cs new file mode 100644 index 0000000..15aca00 --- /dev/null +++ b/StarWarsKb.Back/Model/StarshipService.cs @@ -0,0 +1,32 @@ +using StarWarsKb.Back.Model.POCO; +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public class StarshipService : IStarshipService + { + public Starship CreateFromPOCO(StarshipPOCO poco) + { + var starship = new Starship + { + Consumables = poco.consumables, + HyperdriveRating = poco.hyperdrive_rating, + Manufacturer = poco.manufacturer, + MaxAtmospheringSpeed = poco.max_atmosphering_speed, + MGLT = poco.MGLT, + Model = poco.model, + StarWarId = poco.id, + Name = poco.name, + StarshipClass = poco.starship_class + }; + + starship.Lenght = int.TryParse(poco.lenght, out var lenght) ? lenght : -1; + starship.CargoCapacity = long.TryParse(poco.cargo_capacity, out var cap) ? cap : -1; + starship.CrewCount = int.TryParse(poco.crew, out var crew) ? crew : -1; + starship.PassengersCount = int.TryParse(poco.passengers, out var pass) ? pass : -1; + starship.CostInCredits = long.TryParse(poco.cost_in_credits, out var cost) ? cost : -1; + + return starship; + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/StarshipsRepository.cs b/StarWarsKb.Back/Model/StarshipsRepository.cs new file mode 100644 index 0000000..ae36b10 --- /dev/null +++ b/StarWarsKb.Back/Model/StarshipsRepository.cs @@ -0,0 +1,36 @@ +using System.Collections.Generic; +using System.Linq; +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public class StarshipsRepository : IBaseRepository + { + private readonly ApplicationDbContext _dbContext; + + public StarshipsRepository(ApplicationDbContext dbContext) + { + _dbContext = dbContext; + } + + public IList GetAll() + { + return _dbContext.Starships.ToList(); + } + + public Starship GetById(int id) + { + return _dbContext.Starships.First(x => x.StarWarId == id); + } + + public void Save(Starship s) + { + _dbContext.Starships.Add(s); + } + + public void Save(IList starships) + { + _dbContext.Starships.AddRange(starships); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/StubCharactersRepository.cs b/StarWarsKb.Back/Model/StubCharactersRepository.cs index f82600d..ae7849e 100644 --- a/StarWarsKb.Back/Model/StubCharactersRepository.cs +++ b/StarWarsKb.Back/Model/StubCharactersRepository.cs @@ -4,7 +4,7 @@ namespace StarWarsKb.Back.Model { - public class StubCharactersRepository : ICharactersRepository + public class StubCharactersRepository : IBaseRepository { private IList _list; @@ -68,5 +68,15 @@ public Character GetById(int id) { return _list.First(x => x.StarWarId == id); } + + public void Save(Character c) + { + throw new System.NotImplementedException(); + } + + public void Save(IList characters) + { + throw new System.NotImplementedException(); + } } } \ No newline at end of file diff --git a/StarWarsKb.Back/Model/UnitOfWork.cs b/StarWarsKb.Back/Model/UnitOfWork.cs new file mode 100644 index 0000000..9fe7ac7 --- /dev/null +++ b/StarWarsKb.Back/Model/UnitOfWork.cs @@ -0,0 +1,19 @@ +using Microsoft.EntityFrameworkCore; + +namespace StarWarsKb.Back.Model +{ + public class UnitOfWork : IUnitOfWork + { + private readonly DbContext _dbContext; + + public UnitOfWork(DbContext dbContext) + { + _dbContext = dbContext; + } + + public void SaveChanges() + { + _dbContext.SaveChanges(); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Back/Model/UpdateService.cs b/StarWarsKb.Back/Model/UpdateService.cs new file mode 100644 index 0000000..029cf3d --- /dev/null +++ b/StarWarsKb.Back/Model/UpdateService.cs @@ -0,0 +1,124 @@ +using System.Collections.Generic; +using System.Linq; +using StarWarsKb.Back.Model.POCO; +using StarWarsKb.Infrastructure.Model; + +namespace StarWarsKb.Back.Model +{ + public class UpdateService : IUpdateService + { + private readonly IWebReader _characterWebReader; + private readonly IWebReader _starshipWebReader; + private readonly IWebReader _planetWebReader; + private readonly ICharacterService _characterService; + private readonly IStarshipService _starshipService; + private readonly IUnitOfWork _unitOfWork; + private readonly IBaseRepository _charactersRepository; + private readonly IBaseRepository _starshipsRepository; + private readonly IBaseRepository _planetsRepository; + private readonly IPlanetService _planetService; + public UpdateService(IWebReader characterWebReader, + IWebReader starshipWebReader, IWebReader planetWebReader, + ICharacterService characterService, IStarshipService starshipService, + IUnitOfWork unitOfWork, IBaseRepository charactersRepository, + IBaseRepository starshipsRepository, IBaseRepository planetsRepository, + IPlanetService planetService) + { + _characterWebReader = characterWebReader; + _starshipWebReader = starshipWebReader; + _planetWebReader = planetWebReader; + _characterService = characterService; + _starshipService = starshipService; + _unitOfWork = unitOfWork; + _charactersRepository = charactersRepository; + _starshipsRepository = starshipsRepository; + _planetsRepository = planetsRepository; + _planetService = planetService; + } + + void RunDataOperations(bool saveIfTrueUpdateIfFalse) + { + IList characterPocos = new List(); + var empty = "EMPTY"; + + for (var i = 1; i < 90; i++) + { + var item = _characterWebReader.ReadEntityWithId(i); + if (item.name != empty) + { + characterPocos.Add(item); + } + } + + IList planetPocos = new List(); + for (var i = 1; i < 65; i++) + { + var item = _planetWebReader.ReadEntityWithId(i); + if (item.name != empty) + { + planetPocos.Add(item); + } + } + + IList starshipPocos = new List(); + for (int i = 1; i < 75; i++) + { + var item = _starshipWebReader.ReadEntityWithId(i); + if (item.name != empty) + { + starshipPocos.Add(item); + } + } + + IList characters = new List(); + foreach (var characterPoco in characterPocos) + { + if (characterPoco.name != empty) + { + characters.Add(_characterService.CreateFromPOCO(characterPoco)); + } + } + + IList starships = new List(); + foreach (var starshipPoco in starshipPocos) + { + if (starshipPoco.name != empty) + { + starships.Add(_starshipService.CreateFromPOCO(starshipPoco)); + } + } + + IList planets = new List(); + foreach (var planetPoco in planetPocos) + { + planets.Add(_planetService.CreateFromPOCO(planetPoco)); + } + + for (int i = 0; i < characters.Count; i++) + { + if (characters[i].HomeWorldId != 0) + { + characters[i].HomeWorld = planets.First(x => x.StarWarId == characters[i].HomeWorldId); + characters[i].HomeWorldId = 0; + } + } + + if (saveIfTrueUpdateIfFalse) + { + _planetsRepository.Save(planets); + _charactersRepository.Save(characters); + _starshipsRepository.Save(starships); + } + + _unitOfWork.SaveChanges(); + } + public void CreateData() + { + RunDataOperations(true); + } + public void RunUpdate() + { + RunDataOperations(false); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Infrastructure/Services/WebCharacterPOCOReader.cs b/StarWarsKb.Back/Model/WebCharacterPOCOReader.cs similarity index 58% rename from StarWarsKb.Infrastructure/Services/WebCharacterPOCOReader.cs rename to StarWarsKb.Back/Model/WebCharacterPOCOReader.cs index d94b426..f5f3404 100644 --- a/StarWarsKb.Infrastructure/Services/WebCharacterPOCOReader.cs +++ b/StarWarsKb.Back/Model/WebCharacterPOCOReader.cs @@ -1,7 +1,6 @@ -using StarWarsKb.Infrastructure.Services; -using StarWarsKb.Infrastructure.Services.POCO; +using StarWarsKb.Back.Model.POCO; -namespace StarWars.Infrastructure.Services +namespace StarWarsKb.Back.Model { public class WebCharacterPOCOReader : WebPOCOReader, IWebReader { @@ -9,7 +8,5 @@ public WebCharacterPOCOReader() : base("people") { } - - } } \ No newline at end of file diff --git a/StarWarsKb.Infrastructure/Services/WebPOCOReader.cs b/StarWarsKb.Back/Model/WebPOCOReader.cs similarity index 55% rename from StarWarsKb.Infrastructure/Services/WebPOCOReader.cs rename to StarWarsKb.Back/Model/WebPOCOReader.cs index 1be81ba..e0c46b4 100644 --- a/StarWarsKb.Infrastructure/Services/WebPOCOReader.cs +++ b/StarWarsKb.Back/Model/WebPOCOReader.cs @@ -1,10 +1,11 @@ +using System; using System.Net.Http; using System.Text.Json; -using StarWarsKb.Infrastructure.Services.POCO; +using StarWarsKb.Back.Model.POCO; -namespace StarWarsKb.Infrastructure.Services +namespace StarWarsKb.Back.Model { - public abstract class WebPOCOReader where T : IStarWarPOCOEntity + public abstract class WebPOCOReader where T : IStarWarPOCOEntity, new() { private HttpClient _client; private const string BaseUrl = @"https://swapi.dev/api/"; @@ -20,9 +21,19 @@ public T ReadEntityWithId(int id) { var task = _client.GetStringAsync($"{BaseUrl}{subURL}/{id}/"); - var poco = JsonSerializer.Deserialize(task.Result); - - return poco; + T entity; + + try + { + entity = JsonSerializer.Deserialize(task.Result); + entity.id = id; + } + catch (Exception exc ) + { + entity = new T {name = "EMPTY"}; + } + + return entity; } } } \ No newline at end of file diff --git a/StarWarsKb.Back/Model/WebPlanetPOCOReader.cs b/StarWarsKb.Back/Model/WebPlanetPOCOReader.cs new file mode 100644 index 0000000..bdc1e1e --- /dev/null +++ b/StarWarsKb.Back/Model/WebPlanetPOCOReader.cs @@ -0,0 +1,12 @@ +using StarWarsKb.Back.Model.POCO; + +namespace StarWarsKb.Back.Model +{ + public class WebPlanetPOCOReader : WebPOCOReader, IWebReader + { + public WebPlanetPOCOReader() : base("planets") + { + + } + } +} \ No newline at end of file diff --git a/StarWarsKb.Infrastructure/Services/WebStarshipPOCOReader.cs b/StarWarsKb.Back/Model/WebStarshipPOCOReader.cs similarity index 67% rename from StarWarsKb.Infrastructure/Services/WebStarshipPOCOReader.cs rename to StarWarsKb.Back/Model/WebStarshipPOCOReader.cs index a0f004e..ed7d6b3 100644 --- a/StarWarsKb.Infrastructure/Services/WebStarshipPOCOReader.cs +++ b/StarWarsKb.Back/Model/WebStarshipPOCOReader.cs @@ -1,6 +1,6 @@ -using StarWarsKb.Infrastructure.Services.POCO; +using StarWarsKb.Back.Model.POCO; -namespace StarWarsKb.Infrastructure.Services +namespace StarWarsKb.Back.Model { public class WebStarshipPOCOReader : WebPOCOReader, IWebReader { diff --git a/StarWarsKb.Back/Properties/launchSettings.json b/StarWarsKb.Back/Properties/launchSettings.json index 717e2e7..5a8e7a6 100644 --- a/StarWarsKb.Back/Properties/launchSettings.json +++ b/StarWarsKb.Back/Properties/launchSettings.json @@ -12,7 +12,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "weatherforecast", + "launchUrl": "characters", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } @@ -20,7 +20,7 @@ "StarWarsKb.Back": { "commandName": "Project", "launchBrowser": true, - "launchUrl": "weatherforecast", + "launchUrl": "characters", "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" diff --git a/StarWarsKb.Back/Startup.cs b/StarWarsKb.Back/Startup.cs index 3d89e11..ad19ef8 100644 --- a/StarWarsKb.Back/Startup.cs +++ b/StarWarsKb.Back/Startup.cs @@ -10,6 +10,8 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using StarWarsKb.Back.Model; +using StarWarsKb.Infrastructure.Model; namespace StarWarsKb.Back { @@ -26,6 +28,10 @@ public Startup(IConfiguration configuration) public void ConfigureServices(IServiceCollection services) { services.AddControllers(); + services.AddTransient, CharactersRepository>(); + services.AddDbContext(); + services.AddTransient, StarshipsRepository>(); + services.AddTransient, PlanetsRepository>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. diff --git a/StarWarsKb.Front/Models/CharactersReader.cs b/StarWarsKb.Front/Models/CharactersReader.cs index 930a9a0..cb4f91f 100644 --- a/StarWarsKb.Front/Models/CharactersReader.cs +++ b/StarWarsKb.Front/Models/CharactersReader.cs @@ -27,7 +27,7 @@ public Task> Characters() public async Task GetById(int id) { - var str = await _httpReader.GetJsonStringByUrl($@"https://localhost:7131/Characters/{id}"); + var str = await _httpReader.GetJsonStringByUrl($@"https://localhost:5001/Characters/{id}"); if (str == _httpReader.ErrorMessage) return await new Task(() => new Character()); @@ -36,7 +36,7 @@ public async Task GetById(int id) private async Task> GetAllAsync() { - var str = await _httpReader.GetJsonStringByUrl(@"https://localhost:7131/Characters"); + var str = await _httpReader.GetJsonStringByUrl(@"https://localhost:5001/Characters"); if (str == _httpReader.ErrorMessage) return await new Task>(() => new List()); diff --git a/StarWarsKb.Front/Properties/launchSettings.json b/StarWarsKb.Front/Properties/launchSettings.json index ba3e844..03411c8 100644 --- a/StarWarsKb.Front/Properties/launchSettings.json +++ b/StarWarsKb.Front/Properties/launchSettings.json @@ -18,7 +18,7 @@ "StarWarsKb.Front": { "commandName": "Project", "launchBrowser": true, - "applicationUrl": "https://localhost:5001;http://localhost:5000", + "applicationUrl": "https://localhost:8888;http://localhost:8887", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/StarWarsKb.Front/Startup.cs b/StarWarsKb.Front/Startup.cs index 1279734..6e3bbd1 100644 --- a/StarWarsKb.Front/Startup.cs +++ b/StarWarsKb.Front/Startup.cs @@ -25,7 +25,7 @@ public Startup(IConfiguration configuration) public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); - services.AddTransient(); + services.AddTransient(); services.AddTransient(); services.AddHttpClient(); services.AddTransient(); diff --git a/StarWarsKb.Front/Views/SWKB/Characters.cshtml b/StarWarsKb.Front/Views/SWKB/Characters.cshtml index 139060c..e7bde2f 100644 --- a/StarWarsKb.Front/Views/SWKB/Characters.cshtml +++ b/StarWarsKb.Front/Views/SWKB/Characters.cshtml @@ -10,14 +10,31 @@ @foreach (var c in Model) { - sw -
+ sw +
@c.Name
-

Homeworld: @c.HomeWorld.Name, height: @c.Height, mass: @c.Mass, hair color: @c.HairColor - eye color: @c.EyeColor, birth year: @c.BirthYear, gender: @c.Gender -
- films: @c.Films.Count, species: @c.Species.Count, vehicles: @c.Species.Count, starships: @c.Starships.Count +

Homeworld: @c.HomeWorld.Name, height: + @if (c.Height < 0) + { + unknown + } + else + { + @c.Height + } + mass: + @if (c.Mass < 0) + { + unknown + } + else + { + @c.Mass + } + , + hair color: @c.HairColor, + eye color: @c.EyeColor, birth year: @c.BirthYear, gender: @c.Gender, starships: @c.StarshipCharacters.Count

diff --git a/StarWarsKb.Infrastructure/Class1.cs b/StarWarsKb.Infrastructure/Class1.cs deleted file mode 100644 index bf58956..0000000 --- a/StarWarsKb.Infrastructure/Class1.cs +++ /dev/null @@ -1,8 +0,0 @@ -using System; - -namespace StarWarsKb.Infrastructure -{ - public class Class1 - { - } -} \ No newline at end of file diff --git a/StarWarsKb.Infrastructure/Model/Character.cs b/StarWarsKb.Infrastructure/Model/Character.cs index e2c5293..c37a687 100644 --- a/StarWarsKb.Infrastructure/Model/Character.cs +++ b/StarWarsKb.Infrastructure/Model/Character.cs @@ -12,6 +12,7 @@ public Character() Species = new List(); Vehicles = new List(); Starships = new List(); + StarshipCharacters = new List(); } [NotMapped] diff --git a/StarWarsKb.Infrastructure/Model/Planet.cs b/StarWarsKb.Infrastructure/Model/Planet.cs index eb82dac..c6592e1 100644 --- a/StarWarsKb.Infrastructure/Model/Planet.cs +++ b/StarWarsKb.Infrastructure/Model/Planet.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Text.Json.Serialization; using StarWars.Infrastructure.Model; namespace StarWarsKb.Infrastructure.Model @@ -6,7 +7,17 @@ namespace StarWarsKb.Infrastructure.Model public class Planet : IStarWarsEntity { public string Name { get; set; } + [JsonIgnore] public IList Residents { get; set; } public int StarWarId { get; set; } + + public int RotationPeriod { get; set; } + public int OrbitalPeriod { get; set; } + public int Diameter { get; set; } + public string Climate { get; set; } + public string Gravity { get; set; } + public string Terrain { get; set; } + public string SurfaceWater { get; set; } + public int Population { get; set; } } } \ No newline at end of file diff --git a/StarWarsKb.Infrastructure/Model/StarshipsCharacters.cs b/StarWarsKb.Infrastructure/Model/StarshipsCharacters.cs index 22f1c82..241dc1c 100644 --- a/StarWarsKb.Infrastructure/Model/StarshipsCharacters.cs +++ b/StarWarsKb.Infrastructure/Model/StarshipsCharacters.cs @@ -1,10 +1,14 @@ -namespace StarWarsKb.Infrastructure.Model +using System.Text.Json.Serialization; + +namespace StarWarsKb.Infrastructure.Model { public class StarshipsCharacters { public int StarshipId { get; set; } + [JsonIgnore] public Starship Starship { get; set; } public int CharacterId { get; set; } + [JsonIgnore] public Character Character { get; set; } } } \ No newline at end of file diff --git a/StarWarsKb.Infrastructure/Services/POCO/IStarWarPOCOEntity.cs b/StarWarsKb.Infrastructure/Services/POCO/IStarWarPOCOEntity.cs deleted file mode 100644 index f4c6e2c..0000000 --- a/StarWarsKb.Infrastructure/Services/POCO/IStarWarPOCOEntity.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace StarWarsKb.Infrastructure.Services.POCO -{ - public interface IStarWarPOCOEntity - { - public string url { get; set; } - } -} \ No newline at end of file diff --git a/StarWarsKb.UnitTests/Back/Model/UpdateServiceDebugTests.cs b/StarWarsKb.UnitTests/Back/Model/UpdateServiceDebugTests.cs new file mode 100644 index 0000000..e84e7fa --- /dev/null +++ b/StarWarsKb.UnitTests/Back/Model/UpdateServiceDebugTests.cs @@ -0,0 +1,31 @@ +using StarWarsKb.Back.Model; +using Xunit; + +namespace StarWarsKb.UnitTests.Back.Model +{ + public class UpdateServiceDebugTests + { + [Fact] + public void DebugTest() + { + var characterWebReader = new WebCharacterPOCOReader(); + var planetWebReader = new WebPlanetPOCOReader(); + var starshipsWebReader = new WebStarshipPOCOReader(); + var characterService = new CharacterService(); + var starshipService = new StarshipService(); + var dbContext = new ApplicationDbContext(); + var unitOfWork = new UnitOfWork(dbContext); + var characterRepository = new CharactersRepository(dbContext); + var starshipsRepository = new StarshipsRepository(dbContext); + var planetRepository = new PlanetsRepository(dbContext); + var planetService = new PlanetService(); + + var updateService = new UpdateService(characterWebReader, + starshipsWebReader, planetWebReader, characterService, + starshipService, unitOfWork, characterRepository, + starshipsRepository, planetRepository, planetService); + + updateService.CreateData(); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.UnitTests/Back/Model/UpdateServiceTests.cs b/StarWarsKb.UnitTests/Back/Model/UpdateServiceTests.cs new file mode 100644 index 0000000..f45b46d --- /dev/null +++ b/StarWarsKb.UnitTests/Back/Model/UpdateServiceTests.cs @@ -0,0 +1,73 @@ +using Moq; +using StarWarsKb.Back.Model; +using StarWarsKb.Back.Model.POCO; +using StarWarsKb.Infrastructure.Model; +using Xunit; + +namespace StarWarsKb.UnitTests.Back.Model +{ + public class UpdateServiceTests + { + [Fact] + public void TestOfTest() + { + Assert.Equal(4, 2+2); + } + + private readonly Mock> _mockCharacterWebReader; + private readonly Mock> _mockStarshipWebReader; + private readonly Mock> _mockPlanetWebReader; + private readonly Mock _mockCharacterService; + private readonly Mock _mockStarshipService; + private readonly Mock _mockUnitOfWork; + private readonly Mock> _mockCharacterRepository; + private readonly Mock> _mockStarshipsRepository; + private readonly Mock> _mockPlanetsRepository; + private readonly Mock _mockPlanetService; + + public UpdateServiceTests() + { + _mockCharacterWebReader = new Mock>(); + _mockStarshipWebReader = new Mock>(); + _mockPlanetWebReader = new Mock>(); + _mockCharacterService = new Mock(); + _mockStarshipService = new Mock(); + _mockUnitOfWork = new Mock(); + _mockCharacterRepository = new Mock>(); + _mockStarshipsRepository = new Mock>(); + _mockPlanetsRepository = new Mock>(); + _mockPlanetService = new Mock(); + + IUpdateService updateService = new UpdateService(_mockCharacterWebReader.Object, + _mockStarshipWebReader.Object, + _mockPlanetWebReader.Object, + _mockCharacterService.Object, + _mockStarshipService.Object, + _mockUnitOfWork.Object, + _mockCharacterRepository.Object, + _mockStarshipsRepository.Object, + _mockPlanetsRepository.Object, + _mockPlanetService.Object); + + updateService.CreateData(); + } + + [Fact(Skip = "")] + public void PlanetReaderCalledAtLeastOnce() + { + _mockPlanetWebReader.Verify(x => x.ReadEntityWithId(It.IsAny()), Times.AtLeast(1)); + } + + [Fact(Skip = "")] + public void StarshipWebReaderCalledAtLeastOnce() + { + _mockStarshipWebReader.Verify(x => x.ReadEntityWithId(It.IsAny()), Times.AtLeast(1)); + } + + [Fact(Skip = "")] + public void CharacterWebReaderCalledAtLeastOnce() + { + _mockCharacterWebReader.Verify(x => x.ReadEntityWithId(It.IsAny()), Times.AtLeast(1)); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.UnitTests/Back/Model/WebCharacterPOCOReaderTests.cs b/StarWarsKb.UnitTests/Back/Model/WebCharacterPOCOReaderTests.cs new file mode 100644 index 0000000..cd3e533 --- /dev/null +++ b/StarWarsKb.UnitTests/Back/Model/WebCharacterPOCOReaderTests.cs @@ -0,0 +1,29 @@ +using StarWarsKb.Back.Model; +using StarWarsKb.Back.Model.POCO; +using Xunit; + +namespace StarWarsKb.UnitTests.Back.Model +{ + public class WebCharacterPOCOReaderTests + { + [Fact] + public void CanGetPeopleObjectFromAPI() + { + var reader = new WebCharacterPOCOReader(); + + CharacterPOCO characterPoco = reader.ReadEntityWithId(2); + + Assert.Equal("C-3PO", characterPoco.name); + } + + [Fact] + public void ReturnNullIf404() + { + var reader = new WebCharacterPOCOReader(); + + CharacterPOCO characterPoco = reader.ReadEntityWithId(777); + + Assert.Equal("EMPTY", characterPoco.name); + } + } +} \ No newline at end of file diff --git a/StarWarsKb.UnitTests/StarWarsKb.UnitTests.csproj b/StarWarsKb.UnitTests/StarWarsKb.UnitTests.csproj index 09baf2f..56057e9 100644 --- a/StarWarsKb.UnitTests/StarWarsKb.UnitTests.csproj +++ b/StarWarsKb.UnitTests/StarWarsKb.UnitTests.csproj @@ -7,10 +7,15 @@ - - - - + + + + + + + + + diff --git a/StarWarsKb.UnitTests/UnitTest1.cs b/StarWarsKb.UnitTests/UnitTest1.cs deleted file mode 100644 index 485869d..0000000 --- a/StarWarsKb.UnitTests/UnitTest1.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using Xunit; - -namespace StarWarsKb.UnitTests -{ - public class UnitTest1 - { - [Fact] - public void Test1() - { - } - } -} \ No newline at end of file diff --git a/StarWarsKb.sln.DotSettings.user b/StarWarsKb.sln.DotSettings.user new file mode 100644 index 0000000..7f83c9c --- /dev/null +++ b/StarWarsKb.sln.DotSettings.user @@ -0,0 +1,7 @@ + + <SessionState ContinuousTestingMode="0" IsActive="True" Name="ParserTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <TestAncestor> + <TestId>xUnit::E5779146-3178-47BC-BF24-CF5743CD66B9::.NETCoreApp,Version=v3.1::StarWarsKb.UnitTests.Back.Model.WebCharacterPOCOReaderTests</TestId> + <TestId>xUnit::E5779146-3178-47BC-BF24-CF5743CD66B9::.NETCoreApp,Version=v3.1::StarWarsKb.UnitTests.Back.Model.UpdateServiceDebugTests.DebugTest</TestId> + </TestAncestor> +</SessionState> \ No newline at end of file