From 7afe310f4edbb761e6cab65de505512206796c8c Mon Sep 17 00:00:00 2001 From: Pierre Ferreira Date: Fri, 24 Feb 2023 17:19:38 +0100 Subject: [PATCH] =?UTF-8?q?:package:=20ajout=20de=20l'attribut=20Skill=20p?= =?UTF-8?q?our=20un=20champion,=20la=20liaison=20=C3=A0=20la=20bdd=20n'est?= =?UTF-8?q?=20pas=20faite=20car=20si=20on=20utilise=20un=20immutablehashse?= =?UTF-8?q?t,=20il=20faut=20pouvoir=20demander=20a=20entity=20framework=20?= =?UTF-8?q?d'ignorer=20une=20des=20comparaisons=20entre=20hashset=20et=20i?= =?UTF-8?q?mmutablehashset?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../API_LoL/Controllers/ChampionsController.cs | 4 ++-- Sources/API_LoL/Mapper/ChampionMapper.cs | 14 ++++++++++---- Sources/Api_UT/UnitTest1.cs | 6 +----- Sources/DTO/ChampionDTO.cs | 17 +++++++++++++++-- Sources/EntityFramework/ChampionEntity.cs | 12 +++++++----- Sources/EntityFramework/Skill.cs | 5 ++++- 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs index 1d4efad..8ad3719 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -25,7 +25,7 @@ namespace API_LoL.Controllers { var list = await ChampionsManager.GetItems(0,await ChampionsManager.GetNbItems()); 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 } 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 index 4c934b2..de78d2b 100644 --- a/Sources/Api_UT/UnitTest1.cs +++ b/Sources/Api_UT/UnitTest1.cs @@ -2,12 +2,8 @@ using API_LoL.Controllers; using DTO; using FluentAssertions; using Microsoft.AspNetCore.Mvc; -using Microsoft.EntityFrameworkCore.Query; using Model; using StubLib; -using System.Collections; -using System.Collections.Generic; -using System.Diagnostics; namespace Api_UT { @@ -38,7 +34,7 @@ namespace Api_UT IActionResult a = await api.Post(new ChampionDTO("nom","bio","icon")); Assert.IsNotNull(a); ChampionDTO champ = new ChampionDTO("nom", "bio", "icon"); - Assert.IsTrue(champ.equals((ChampionDTO)((CreatedAtActionResult)a).Value)); + //Assert.AreEqual(champ,((CreatedAtActionResult)a).Value); } } diff --git a/Sources/DTO/ChampionDTO.cs b/Sources/DTO/ChampionDTO.cs index ddef07e..1e359a8 100644 --- a/Sources/DTO/ChampionDTO.cs +++ b/Sources/DTO/ChampionDTO.cs @@ -1,4 +1,5 @@ using Model; +using System.Collections.Immutable; namespace DTO { @@ -11,13 +12,25 @@ 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; } - - + /// + /// pour plus tard ? + /// + //public ImmutableHashSet Skills { get; set; } + + //public ICollection Skills { get; set; } public bool equals(ChampionDTO other) { diff --git a/Sources/EntityFramework/ChampionEntity.cs b/Sources/EntityFramework/ChampionEntity.cs index 2f859e1..f55a7f0 100644 --- a/Sources/EntityFramework/ChampionEntity.cs +++ b/Sources/EntityFramework/ChampionEntity.cs @@ -28,8 +28,10 @@ namespace EntityFramework [Required] public string Icon { get; set; } - public ImmutableHashSet Skills => skills.ToImmutableHashSet(); - private HashSet skills = new HashSet(); + //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; @@ -44,10 +46,10 @@ namespace EntityFramework - public bool AddSkill(Skill skill) - => skills.Add(skill); + public void AddSkill(Skill skill) + => this.Skills.Add(skill); public bool RemoveSkill(Skill skill) - => skills.Remove(skill); + => Skills.Remove(skill); } } diff --git a/Sources/EntityFramework/Skill.cs b/Sources/EntityFramework/Skill.cs index e9b1f32..bf9934e 100644 --- a/Sources/EntityFramework/Skill.cs +++ b/Sources/EntityFramework/Skill.cs @@ -1,15 +1,18 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace EntityFramework { + public class Skill { public SkillType Type { get; private set; } - + + [Key] public string Name { get => name;