From 6e0244255bf81ed35fcc015414d1bee1ed46f2ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20P=C3=A9rez=20Ngounou?= Date: Fri, 20 Jan 2023 10:55:17 +0100 Subject: [PATCH 1/2] utilisation de linq --- Sources/BowlingService/JoueurService.cs | 76 ++++++++++--------------- 1 file changed, 30 insertions(+), 46 deletions(-) diff --git a/Sources/BowlingService/JoueurService.cs b/Sources/BowlingService/JoueurService.cs index 2b39ff6..e2df2a8 100644 --- a/Sources/BowlingService/JoueurService.cs +++ b/Sources/BowlingService/JoueurService.cs @@ -35,36 +35,25 @@ namespace BowlingService { JoueurDTO result = null; try + { + //Mapping entre la classe joueur et la classe joueurEntity + JoueurEntity entity = _mapper.Map(_joueur); + + + entity.PartieEntities=_joueur.PartieDTO.Select(p => { - //Mapping entre la classe joueur et la classe joueurEntity - JoueurEntity entity = new JoueurEntity - { - Id = _joueur.Id, - Pseudo = _joueur.Pseudo, - }; - - //Parcourt de la liste des parties d'un joueur - for (int i = 0; i < _joueur.PartieDTO.Count; i++) - { - //Mapping entre les parties d'un joueur et les partieEntity d'une partieEntity - PartieEntity partieEntity = _mapper.Map(_joueur.PartieDTO.ElementAt(1)); - - //Parcourt de la liste des frames d'une partie - for (int j = 0; j < _joueur.PartieDTO.ElementAt(i).FramesDTO.Count; j++) - { - //Mapping entre les frames d'une partie et les frameEntity d'une partieEntity - FrameEntity frameEntity = _mapper.Map(_joueur.PartieDTO.ElementAt(i).FramesDTO.ElementAt(j)); - partieEntity.Frames.Add(frameEntity); - } - entity.PartieEntities.Add(partieEntity); - } - result = _mapper.Map(await _joueurRepository.Add(entity)); - } - catch (Exception ex) - { - Debug.WriteLine(ex); - throw; - } + var partieEntity = _mapper.Map(p); + partieEntity.Frames=p.FramesDTO.Select(f => _mapper.Map(f)).ToList(); + return partieEntity; + }).ToList(); + + result = _mapper.Map(await _joueurRepository.Add(entity)); + } + catch (Exception ex) + { + Debug.WriteLine(ex); + throw; + } return result; } @@ -88,8 +77,9 @@ namespace BowlingService { List joueurs = new List(); var data= await _joueurRepository.GetAllJoueur(); - foreach (var item in data) - joueurs.Add(_mapper.Map(item)); + + joueurs= data.Select(j => _mapper.Map(j)).ToList(); + return joueurs; } } @@ -101,28 +91,22 @@ namespace BowlingService /// public async Task GetDataWithName(string name) { - using (var context = new BowlingContext()) - { - JoueurDTO _joueur = null; + JoueurDTO _joueur = null; - var query = _joueurRepository.GetJoueurByNom(name); - _joueur = _mapper.Map(query.Result); - return _joueur; - } + var query = _joueurRepository.GetJoueurByNom(name); + _joueur = _mapper.Map(query.Result); + return _joueur; } public async Task Update(JoueurDTO _joueur) { bool result = false; - using (var context = new BowlingContext()) + JoueurEntity entity = _joueurRepository.GetJoueur(_joueur.Id).Result; + if (entity!=null) { - JoueurEntity entity = _joueurRepository.GetJoueur(_joueur.Id).Result; - if (entity!=null) - { - entity.Pseudo = _joueur.Pseudo; - result = _joueurRepository.Update(entity).Result; - } - } + entity.Pseudo = _joueur.Pseudo; + result = _joueurRepository.Update(entity).Result; + } return result; } #endregion From 4545a7c6b61ae150111c0e87552205970713ae8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Victor=20P=C3=A9rez=20Ngounou?= Date: Sat, 21 Jan 2023 10:09:54 +0100 Subject: [PATCH 2/2] Push --- Sources/BowlingApi/BowlingApi.csproj | 4 +- .../Controllers/JoueurController.cs | 1 + Sources/BowlingApi/Program.cs | 7 ++ .../BowlingApi/Properties/launchSettings.json | 6 +- Sources/BowlingEF/Entities/PartieEntity.cs | 2 +- .../Interfaces/IJoueurService.cs | 1 + Sources/BowlingService/JoueurService.cs | 80 ++++++++++++++----- Sources/DTOs/EquipeDTO.cs | 2 +- Sources/DTOs/FrameDTO.cs | 8 +- Sources/DTOs/JoueurDTO.cs | 4 +- Sources/DTOs/PartieDTO.cs | 3 +- Sources/Mapper/EquipeProfile.cs | 1 + Sources/Mapper/FrameProfile.cs | 1 + Sources/Mapper/JoueurProfile.cs | 1 + Sources/Mapper/PartieProfile.cs | 1 + Sources/Solution.sln | 7 ++ .../BowlingAPITest/BowlingAPITest.csproj | 30 +++++++ .../BowlingAPITest/TestJoueurController.cs | 64 +++++++++++++++ Sources/Tests/BowlingAPITest/Usings.cs | 6 ++ 19 files changed, 196 insertions(+), 33 deletions(-) create mode 100644 Sources/Tests/BowlingAPITest/BowlingAPITest.csproj create mode 100644 Sources/Tests/BowlingAPITest/TestJoueurController.cs create mode 100644 Sources/Tests/BowlingAPITest/Usings.cs diff --git a/Sources/BowlingApi/BowlingApi.csproj b/Sources/BowlingApi/BowlingApi.csproj index 64f00fb..953d788 100644 --- a/Sources/BowlingApi/BowlingApi.csproj +++ b/Sources/BowlingApi/BowlingApi.csproj @@ -7,8 +7,10 @@ 11 + + - + diff --git a/Sources/BowlingApi/Controllers/JoueurController.cs b/Sources/BowlingApi/Controllers/JoueurController.cs index a854999..34a5eac 100644 --- a/Sources/BowlingApi/Controllers/JoueurController.cs +++ b/Sources/BowlingApi/Controllers/JoueurController.cs @@ -1,5 +1,6 @@ using BowlingEF.Entities; using BowlingService.Interfaces; +using DTOs; using Microsoft.AspNetCore.Mvc; namespace BowlingApi.Controllers; diff --git a/Sources/BowlingApi/Program.cs b/Sources/BowlingApi/Program.cs index 2a009e5..2d86089 100644 --- a/Sources/BowlingApi/Program.cs +++ b/Sources/BowlingApi/Program.cs @@ -19,6 +19,13 @@ builder.Services.AddAutoMapper(typeof(JoueurProfile)); builder.Services.AddScoped(); builder.Services.AddScoped(); +//configure Logger +builder.Services.AddLogging(configure => +{ + configure.AddConsole(); + configure.AddDebug(); +}); + var app = builder.Build(); // Configure the HTTP request pipeline. diff --git a/Sources/BowlingApi/Properties/launchSettings.json b/Sources/BowlingApi/Properties/launchSettings.json index ea7e601..018693f 100644 --- a/Sources/BowlingApi/Properties/launchSettings.json +++ b/Sources/BowlingApi/Properties/launchSettings.json @@ -1,4 +1,4 @@ -{ +{ "$schema": "https://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, @@ -11,7 +11,6 @@ "profiles": { "http": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "http://localhost:5229", @@ -21,7 +20,6 @@ }, "https": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "https://localhost:7097;http://localhost:5229", @@ -38,4 +36,4 @@ } } } -} +} \ No newline at end of file diff --git a/Sources/BowlingEF/Entities/PartieEntity.cs b/Sources/BowlingEF/Entities/PartieEntity.cs index 4929b51..4682cae 100644 --- a/Sources/BowlingEF/Entities/PartieEntity.cs +++ b/Sources/BowlingEF/Entities/PartieEntity.cs @@ -21,7 +21,7 @@ namespace BowlingEF.Entities [ForeignKey("JoueurForeignKey")] public JoueurEntity Joueur { get; set; } [Required] - public DateTime Date { get; set; } + public DateTime Date { get; set; } = DateTime.Now; public ICollection Frames { get; set; } [Required] public int? Score { get; set; } diff --git a/Sources/BowlingService/Interfaces/IJoueurService.cs b/Sources/BowlingService/Interfaces/IJoueurService.cs index a12c192..06dd825 100644 --- a/Sources/BowlingService/Interfaces/IJoueurService.cs +++ b/Sources/BowlingService/Interfaces/IJoueurService.cs @@ -1,4 +1,5 @@ using BowlingEF.Entities; +using DTOs; namespace BowlingService.Interfaces; diff --git a/Sources/BowlingService/JoueurService.cs b/Sources/BowlingService/JoueurService.cs index e2df2a8..7fa147b 100644 --- a/Sources/BowlingService/JoueurService.cs +++ b/Sources/BowlingService/JoueurService.cs @@ -5,6 +5,7 @@ using System.Diagnostics; using AutoMapper; using BowlingRepository.Interface; using BowlingService.Interfaces; +using DTOs; namespace BowlingService { @@ -20,10 +21,11 @@ namespace BowlingService #region Méthodes - public JoueurService(IJoueurRepository joueurRepository,IMapper mapper) + public JoueurService(IJoueurRepository joueurRepository,IMapper mapper , ILogger logger) { _joueurRepository = joueurRepository; _mapper = mapper; + _logger = logger; } /// @@ -38,8 +40,6 @@ namespace BowlingService { //Mapping entre la classe joueur et la classe joueurEntity JoueurEntity entity = _mapper.Map(_joueur); - - entity.PartieEntities=_joueur.PartieDTO.Select(p => { var partieEntity = _mapper.Map(p); @@ -48,10 +48,11 @@ namespace BowlingService }).ToList(); result = _mapper.Map(await _joueurRepository.Add(entity)); + _logger.LogInformation("A new player was added : {player}", _joueur.Pseudo); } catch (Exception ex) { - Debug.WriteLine(ex); + _logger.LogError(ex, "Error while adding new player : {player}", _joueur.Pseudo); throw; } return result; @@ -64,7 +65,18 @@ namespace BowlingService /// public async Task Delete(JoueurDTO _joueur) { - return await _joueurRepository.Delete(_joueur.Id); + var result = false; + try + { + result = await _joueurRepository.Delete(_joueur.Id); + _logger.LogInformation("A player was deleted : {player}", _joueur.Pseudo); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while deleting player : {player}", _joueur.Pseudo); + throw; + } + return result; } /// @@ -76,10 +88,17 @@ namespace BowlingService using (var context = new BowlingContext()) { List joueurs = new List(); - var data= await _joueurRepository.GetAllJoueur(); - - joueurs= data.Select(j => _mapper.Map(j)).ToList(); - + try + { + var joueursEntity = await _joueurRepository.GetAllJoueur(); + joueurs = joueursEntity.Select(j => _mapper.Map(j)).ToList(); + _logger.LogInformation("All players were retrieved"); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while retrieving all players"); + throw; + } return joueurs; } } @@ -92,22 +111,47 @@ namespace BowlingService public async Task GetDataWithName(string name) { JoueurDTO _joueur = null; - - var query = _joueurRepository.GetJoueurByNom(name); - _joueur = _mapper.Map(query.Result); + + try + { + var joueurEntity = await _joueurRepository.GetJoueurByNom(name); + _joueur = _mapper.Map(joueurEntity); + _logger.LogInformation("Player was retrieved : {player}", name); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while retrieving player : {player}", name); + throw; + } return _joueur; } public async Task Update(JoueurDTO _joueur) { bool result = false; - JoueurEntity entity = _joueurRepository.GetJoueur(_joueur.Id).Result; - if (entity!=null) + try { - entity.Pseudo = _joueur.Pseudo; - result = _joueurRepository.Update(entity).Result; - } - return result; + JoueurEntity entity = _joueurRepository.GetJoueur(_joueur.Id).Result; + if (entity!= null) + { + entity.Pseudo = _joueur.Pseudo; + entity.PartieEntities = _joueur.PartieDTO.Select(p => + { + var partieEntity = _mapper.Map(p); + partieEntity.Frames = p.FramesDTO.Select(f => _mapper.Map(f)).ToList(); + return partieEntity; + }).ToList(); + result = await _joueurRepository.Update(entity); + _logger.LogInformation("Player was updated : {player}", _joueur.Pseudo); + } + + return result; + } + catch (Exception ex) + { + _logger.LogError(ex, "Error while updating player : {player}", _joueur.Pseudo); + throw; + } } #endregion diff --git a/Sources/DTOs/EquipeDTO.cs b/Sources/DTOs/EquipeDTO.cs index 18d129e..e30f2ac 100644 --- a/Sources/DTOs/EquipeDTO.cs +++ b/Sources/DTOs/EquipeDTO.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace BowlingEF.Entities +namespace DTOs { /// /// Classe de gestion des equipes diff --git a/Sources/DTOs/FrameDTO.cs b/Sources/DTOs/FrameDTO.cs index 377953a..53da26c 100644 --- a/Sources/DTOs/FrameDTO.cs +++ b/Sources/DTOs/FrameDTO.cs @@ -1,7 +1,7 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -namespace BowlingEF.Entities +namespace DTOs { /// /// Classe de gestion des frames @@ -9,18 +9,14 @@ namespace BowlingEF.Entities public class FrameDTO { #region Properties - [Key] - [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Id { get; set; } - [Required] public int Numero { get; set; } - [Required] public int Lancer1 { get; set; } - [Required] public int Lancer2 { get; set; } public int Lancer3 { get; set; } public PartieDTO Partie { get; set; } #endregion } + } \ No newline at end of file diff --git a/Sources/DTOs/JoueurDTO.cs b/Sources/DTOs/JoueurDTO.cs index 8cf54ff..012f3f0 100644 --- a/Sources/DTOs/JoueurDTO.cs +++ b/Sources/DTOs/JoueurDTO.cs @@ -1,7 +1,7 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; -namespace BowlingEF.Entities +namespace DTOs { /// /// Classe de gestion des Joueurs @@ -18,4 +18,6 @@ namespace BowlingEF.Entities public ICollection PartieDTO { get; set; } = new List(); #endregion } + + } \ No newline at end of file diff --git a/Sources/DTOs/PartieDTO.cs b/Sources/DTOs/PartieDTO.cs index 0386481..a6761f1 100644 --- a/Sources/DTOs/PartieDTO.cs +++ b/Sources/DTOs/PartieDTO.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace BowlingEF.Entities +namespace DTOs { /// /// Classe de gestion des parties @@ -34,4 +34,5 @@ namespace BowlingEF.Entities } #endregion } + } diff --git a/Sources/Mapper/EquipeProfile.cs b/Sources/Mapper/EquipeProfile.cs index 6f88c5a..9ca8017 100644 --- a/Sources/Mapper/EquipeProfile.cs +++ b/Sources/Mapper/EquipeProfile.cs @@ -1,5 +1,6 @@ using AutoMapper; using BowlingEF.Entities; +using DTOs; namespace Mapper; diff --git a/Sources/Mapper/FrameProfile.cs b/Sources/Mapper/FrameProfile.cs index c94e61f..ceecdd1 100644 --- a/Sources/Mapper/FrameProfile.cs +++ b/Sources/Mapper/FrameProfile.cs @@ -1,5 +1,6 @@ using AutoMapper; using BowlingEF.Entities; +using DTOs; namespace Mapper; diff --git a/Sources/Mapper/JoueurProfile.cs b/Sources/Mapper/JoueurProfile.cs index c109b3b..3f24031 100644 --- a/Sources/Mapper/JoueurProfile.cs +++ b/Sources/Mapper/JoueurProfile.cs @@ -1,5 +1,6 @@ using AutoMapper; using BowlingEF.Entities; +using DTOs; namespace Mapper; diff --git a/Sources/Mapper/PartieProfile.cs b/Sources/Mapper/PartieProfile.cs index 8b4ffab..2826969 100644 --- a/Sources/Mapper/PartieProfile.cs +++ b/Sources/Mapper/PartieProfile.cs @@ -1,5 +1,6 @@ using AutoMapper; using BowlingEF.Entities; +using DTOs; namespace Mapper; diff --git a/Sources/Solution.sln b/Sources/Solution.sln index ed81a6b..af70618 100644 --- a/Sources/Solution.sln +++ b/Sources/Solution.sln @@ -33,6 +33,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mapper", "Mapper\Mapper.csp EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BowlingRepository", "BowlingRepository\BowlingRepository.csproj", "{0C93426A-0AA1-45AE-BF57-FC06B1FC9A2C}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BowlingAPITest", "Tests\BowlingAPITest\BowlingAPITest.csproj", "{E5F97007-B13F-4D89-BCC7-4C1D836413AF}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -75,6 +77,10 @@ Global {0C93426A-0AA1-45AE-BF57-FC06B1FC9A2C}.Debug|Any CPU.Build.0 = Debug|Any CPU {0C93426A-0AA1-45AE-BF57-FC06B1FC9A2C}.Release|Any CPU.ActiveCfg = Release|Any CPU {0C93426A-0AA1-45AE-BF57-FC06B1FC9A2C}.Release|Any CPU.Build.0 = Release|Any CPU + {E5F97007-B13F-4D89-BCC7-4C1D836413AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E5F97007-B13F-4D89-BCC7-4C1D836413AF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E5F97007-B13F-4D89-BCC7-4C1D836413AF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E5F97007-B13F-4D89-BCC7-4C1D836413AF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -86,6 +92,7 @@ Global {E9350F3C-4E54-46C3-9C2D-0724C6DDF154} = {A67CAE01-FD47-4EFC-A226-0E23403693F4} {4C872D24-DF74-4F5C-B6C3-A00BB0E72BE5} = {BEA68AD8-C88B-4C1C-BE08-00832C695E76} {0C93426A-0AA1-45AE-BF57-FC06B1FC9A2C} = {8CF29C06-8F11-49EA-80FC-579B98519159} + {E5F97007-B13F-4D89-BCC7-4C1D836413AF} = {C75DF644-C41F-4A08-8B69-C8554204AC72} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {4D47853B-D1A3-49A5-84BA-CD2DC65FD105} diff --git a/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj b/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj new file mode 100644 index 0000000..ad62438 --- /dev/null +++ b/Sources/Tests/BowlingAPITest/BowlingAPITest.csproj @@ -0,0 +1,30 @@ + + + + net7.0 + enable + enable + + false + + + + + + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + runtime; build; native; contentfiles; analyzers; buildtransitive + all + + + + + + + + + diff --git a/Sources/Tests/BowlingAPITest/TestJoueurController.cs b/Sources/Tests/BowlingAPITest/TestJoueurController.cs new file mode 100644 index 0000000..44bd48d --- /dev/null +++ b/Sources/Tests/BowlingAPITest/TestJoueurController.cs @@ -0,0 +1,64 @@ +using DTOs; + +namespace BowlingAPITest; + +public class TestController +{ + [Fact] + public async void Get_ShouldReturnOkResult() + { + // Arrange + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ReturnsAsync(new List()); + var controller = new JoueurController(mockService.Object); + + // Act + var result = await controller.Get(); + + // Assert + Assert.IsType(result); + + } + + [Fact] + public async void Get_ShouldReturnAllItems() + { + // Arrange + var testItems = GetTestItems(); + var mockService = new Mock(); + mockService.Setup(service => service.GetAll()).ReturnsAsync(testItems); + var controller = new JoueurController(mockService.Object); + + // Act + var result = await controller.Get(); + + // Assert + var okResult = result as OkObjectResult; + var items = Assert.IsType>(okResult.Value); + Assert.Equal(2, items.Count); + } + + private IEnumerable GetTestItems() + { + + var testItems = new List(); + testItems.Add(new JoueurDTO {Pseudo = "Item1" }); + testItems.Add(new JoueurDTO { Pseudo = "Item2" }); + return testItems; + } + + // [Fact] + // public async void GetById_ShouldReturnNotFound() + // { + // // Arrange + // var mockService = new Mock(); + // mockService.Setup(service => service.Get(1)).ReturnsAsync((JoueurDTO)null); + // var controller = new JoueurController(mockService.Object); + // + // // Act + // var result = await controller.Get(1); + // + // // Assert + // Assert.IsType(result); + // } +} \ No newline at end of file diff --git a/Sources/Tests/BowlingAPITest/Usings.cs b/Sources/Tests/BowlingAPITest/Usings.cs new file mode 100644 index 0000000..49abb01 --- /dev/null +++ b/Sources/Tests/BowlingAPITest/Usings.cs @@ -0,0 +1,6 @@ +global using Xunit; +global using BowlingApi.Controllers; +global using BowlingEF.Entities; +global using BowlingService.Interfaces; +global using Microsoft.AspNetCore.Mvc; +global using Moq;