diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs index 4494b94..9aefc31 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -34,14 +34,14 @@ namespace API_LoL.Controllers var list = await ChampionsManager.GetItemsByName(name, index,size); if (list.Count() != 0) { - return Ok(list.Select(champion => champion?.toDTO())); + return Ok(list.Select(champion => champion?.ToDTO())); } else { return NoContent(); } }else if(!string.IsNullOrEmpty(skill)) { var list = await ChampionsManager.GetItemsBySkill(skill, index, size); if (list.Count() != 0) { - return Ok(list.Select(champion => champion?.toDTO())); + return Ok(list.Select(champion => champion?.ToDTO())); } else { return NoContent(); } } @@ -49,7 +49,7 @@ namespace API_LoL.Controllers var list = await ChampionsManager.GetItems(index, size); if (list.Count() != 0) { - return Ok(list.Select(champion => champion?.toDTO())); + return Ok(list.Select(champion => champion?.ToDTO())); } else { return NoContent(); } } @@ -57,7 +57,7 @@ namespace API_LoL.Controllers var list = await ChampionsManager.GetItems(index, size); if (list.Count() != 0) { - return Ok(list.Select(champion => champion?.toDTO())); + return Ok(list.Select(champion => champion?.ToDTO())); } else { return NoContent(); } } @@ -73,7 +73,7 @@ namespace API_LoL.Controllers } else { - await ChampionsManager.AddItem(champion.toChampion()); + await ChampionsManager.AddItem(champion.ToChampion()); return CreatedAtAction("Post",champion); } } diff --git a/Sources/API_LoL/Mapper/ChampionMapper.cs b/Sources/API_LoL/Mapper/ChampionMapper.cs index b380b7e..c2e8d0b 100644 --- a/Sources/API_LoL/Mapper/ChampionMapper.cs +++ b/Sources/API_LoL/Mapper/ChampionMapper.cs @@ -9,15 +9,21 @@ namespace DTO.Mapper { public static class ChampionMapper { - public static ChampionDTO toDTO(this Champion champion) + public static ChampionDTO ToDTO(this Champion champion) { return new ChampionDTO(champion.Name, champion.Bio, champion.Icon); + //return new ChampionDTO(champion.Name, champion.Bio, champion.Icon, champion.Skills); } - public static Champion toChampion(this ChampionDTO champion) + public static Champion ToChampion(this ChampionDTO champion) { - return new Champion(champion.Name, ChampionClass.Unknown, champion.Icon, "", champion.Bio); - + Champion champ = new Champion(champion.Name, ChampionClass.Unknown, champion.Icon, "", champion.Bio); + + //foreach (Skill skill in champion.Skills) + //{ + // champ.AddSkill(skill); + //} + return champ; } } } diff --git a/Sources/Api_UT/UnitTest1.cs b/Sources/Api_UT/UnitTest1.cs new file mode 100644 index 0000000..de78d2b --- /dev/null +++ b/Sources/Api_UT/UnitTest1.cs @@ -0,0 +1,41 @@ +using API_LoL.Controllers; +using DTO; +using FluentAssertions; +using Microsoft.AspNetCore.Mvc; +using Model; +using StubLib; + +namespace Api_UT +{ + [TestClass] + public class UnitTest1 + { + [TestMethod] + public async Task TestGet() + { + List list = new List {new ChampionDTO("Akali","",""), new ChampionDTO("Aatrox", "", ""), new ChampionDTO("Ahri", "", ""), new ChampionDTO("Akshan", "", ""), new ChampionDTO("Bard", "", ""), new ChampionDTO("Alistar", "", "") }; + ChampionsController api = new ChampionsController(new StubData()); + IActionResult a = await api.Get(); + + /// utilisation du nuggets fluentAssertion + //Assert.IsNotNull(a); + a.Should().NotBeNull(); + //Assert.AreEqual(list,((OkObjectResult)a).Value); + var aObject = a as OkObjectResult; + aObject.Should().NotBeNull(); + var championresult = aObject.Value as IEnumerable; + list.Should().BeEquivalentTo(championresult); + } + + [TestMethod] + public async Task TestPostValid() + { + ChampionsController api = new ChampionsController(new StubData()); + IActionResult a = await api.Post(new ChampionDTO("nom","bio","icon")); + Assert.IsNotNull(a); + ChampionDTO champ = new ChampionDTO("nom", "bio", "icon"); + //Assert.AreEqual(champ,((CreatedAtActionResult)a).Value); + } + + } +} \ No newline at end of file diff --git a/Sources/DTO/ChampionDTO.cs b/Sources/DTO/ChampionDTO.cs index ddef07e..922d56e 100644 --- a/Sources/DTO/ChampionDTO.cs +++ b/Sources/DTO/ChampionDTO.cs @@ -1,4 +1,5 @@ using Model; +using System.Collections.Immutable; namespace DTO { @@ -11,14 +12,19 @@ namespace DTO Icon = icon; } + //public ChampionDTO(string name, string bio, string icon, ICollection skills) + //{ + // Name = name; + // Bio = bio; + // Icon = icon; + // Skills = skills; + //} + public string Name { get; set; } public string Bio { get; set; } //public ChampionClass Class { get; set; } public string Icon { get; set; } - - - public bool equals(ChampionDTO other) { return other.Name==this.Name && other.Bio==this.Bio && other.Icon==this.Icon; diff --git a/Sources/EntityFramework/ChampionEntity.cs b/Sources/EntityFramework/ChampionEntity.cs index 856100a..5d2c455 100644 --- a/Sources/EntityFramework/ChampionEntity.cs +++ b/Sources/EntityFramework/ChampionEntity.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Collections.Immutable; +using System.Collections.ObjectModel; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; @@ -31,10 +32,13 @@ namespace EntityFramework //public ImmutableHashSet Skills => skills.ToImmutableHashSet(); //private HashSet skills = new HashSet(); + private ICollection Skills { get; set; } + public ChampionEntity(string name,string bio,string icon) { this.Name = name; this.Bio = bio; this.Icon = icon; + Skills= new List(); } public override string ToString() @@ -43,11 +47,11 @@ namespace EntityFramework } -/* - public bool AddSkill(Skill skill) - => skills.Add(skill); - public bool RemoveSkill(Skill skill) - => skills.Remove(skill);*/ + public void AddSkill(SkillEntity skill) + => Skills.Add(skill); + + public void RemoveSkill(SkillEntity skill) + => Skills.Remove(skill); } } diff --git a/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.Designer.cs b/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.Designer.cs new file mode 100644 index 0000000..94bb55c --- /dev/null +++ b/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.Designer.cs @@ -0,0 +1,85 @@ +// +using System; +using EntityFramework; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFramework.Migrations +{ + [DbContext(typeof(LoLDbContext))] + [Migration("20230301152530_SkillMigration")] + partial class SkillMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("EntityFramework.ChampionEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Bio") + .IsRequired() + .HasMaxLength(500) + .HasColumnType("string") + .HasColumnName("Bio"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Champion", (string)null); + }); + + modelBuilder.Entity("EntityFramework.Skill", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("ChampionEntityId") + .HasColumnType("INTEGER"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Type") + .HasColumnType("INTEGER"); + + b.HasKey("Name"); + + b.HasIndex("ChampionEntityId"); + + b.ToTable("Skill"); + }); + + modelBuilder.Entity("EntityFramework.Skill", b => + { + b.HasOne("EntityFramework.ChampionEntity", null) + .WithMany("Skills") + .HasForeignKey("ChampionEntityId"); + }); + + modelBuilder.Entity("EntityFramework.ChampionEntity", b => + { + b.Navigation("Skills"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.cs b/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.cs new file mode 100644 index 0000000..ce0154c --- /dev/null +++ b/Sources/EntityFramework/Migrations/20230301152530_SkillMigration.cs @@ -0,0 +1,63 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EntityFramework.Migrations +{ + /// + public partial class SkillMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Champion", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", maxLength: 50, nullable: false), + Bio = table.Column(type: "string", maxLength: 500, nullable: false), + Icon = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Champion", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Skill", + columns: table => new + { + Name = table.Column(type: "TEXT", nullable: false), + Type = table.Column(type: "INTEGER", nullable: false), + Description = table.Column(type: "TEXT", nullable: false), + ChampionEntityId = table.Column(type: "INTEGER", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Skill", x => x.Name); + table.ForeignKey( + name: "FK_Skill_Champion_ChampionEntityId", + column: x => x.ChampionEntityId, + principalTable: "Champion", + principalColumn: "Id"); + }); + + migrationBuilder.CreateIndex( + name: "IX_Skill_ChampionEntityId", + table: "Skill", + column: "ChampionEntityId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Skill"); + + migrationBuilder.DropTable( + name: "Champion"); + } + } +} diff --git a/Sources/EntityFramework/Migrations/LoLDbContextModelSnapshot.cs b/Sources/EntityFramework/Migrations/LoLDbContextModelSnapshot.cs new file mode 100644 index 0000000..0abeee1 --- /dev/null +++ b/Sources/EntityFramework/Migrations/LoLDbContextModelSnapshot.cs @@ -0,0 +1,82 @@ +// +using System; +using EntityFramework; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFramework.Migrations +{ + [DbContext(typeof(LoLDbContext))] + partial class LoLDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("EntityFramework.ChampionEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Bio") + .IsRequired() + .HasMaxLength(500) + .HasColumnType("string") + .HasColumnName("Bio"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Champion", (string)null); + }); + + modelBuilder.Entity("EntityFramework.Skill", b => + { + b.Property("Name") + .HasColumnType("TEXT"); + + b.Property("ChampionEntityId") + .HasColumnType("INTEGER"); + + b.Property("Description") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Type") + .HasColumnType("INTEGER"); + + b.HasKey("Name"); + + b.HasIndex("ChampionEntityId"); + + b.ToTable("Skill"); + }); + + modelBuilder.Entity("EntityFramework.Skill", b => + { + b.HasOne("EntityFramework.ChampionEntity", null) + .WithMany("Skills") + .HasForeignKey("ChampionEntityId"); + }); + + modelBuilder.Entity("EntityFramework.ChampionEntity", b => + { + b.Navigation("Skills"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFramework/Program.cs b/Sources/EntityFramework/Program.cs index d3d785d..65ef614 100644 --- a/Sources/EntityFramework/Program.cs +++ b/Sources/EntityFramework/Program.cs @@ -18,4 +18,20 @@ using( var context = new LoLDbContext()) { Console.WriteLine("Not Found"); } + + //Test BDD Skills + ChampionEntity champSkill = new ChampionEntity("nomSkill", "bioSkill", "iconSkill"); + + SkillEntity s1 = new SkillEntity("Skill1", "desc", SkillType.Unknown); + SkillEntity s2 = new SkillEntity("Skill2", "desc2", SkillType.Ultimate); + SkillEntity s3 = new SkillEntity("Skill3", "desc3", SkillType.Passive); + + champSkill.AddSkill(s1); + champSkill.AddSkill(s2); + champSkill.AddSkill(s3); + + context.Add(champSkill); + + context.SaveChanges(); + } diff --git a/Sources/EntityFramework/Skill.cs b/Sources/EntityFramework/SkillEntity.cs similarity index 81% rename from Sources/EntityFramework/Skill.cs rename to Sources/EntityFramework/SkillEntity.cs index be43702..d370410 100644 --- a/Sources/EntityFramework/Skill.cs +++ b/Sources/EntityFramework/SkillEntity.cs @@ -7,10 +7,11 @@ using System.Threading.Tasks; namespace EntityFramework { - public class Skill + + public class SkillEntity { public SkillType Type { get; private set; } - + [Key] public string Name { @@ -40,5 +41,11 @@ namespace EntityFramework } } private string description = ""; + + public SkillEntity(string Name, string Description, SkillType Type) { + this.name = Name; + this.Description = Description; + this.Type = Type; + } } } diff --git a/Sources/EntityFramework/champion.db b/Sources/EntityFramework/champion.db new file mode 100644 index 0000000..662e972 Binary files /dev/null and b/Sources/EntityFramework/champion.db differ