diff --git a/Project/EntityFramework/API/Controllers/LangueController.cs b/Project/EntityFramework/API/Controllers/LangueController.cs index e05a578..6696284 100644 --- a/Project/EntityFramework/API/Controllers/LangueController.cs +++ b/Project/EntityFramework/API/Controllers/LangueController.cs @@ -21,12 +21,12 @@ namespace API.Controllers } [HttpGet] - public async Task>> GetLangues(int index, int count) + public async Task>> GetLangues(int index, int count) { try { _logger.LogInformation("Getting langues "); var groups = await _service.Gets(index,count); - return Ok(groups); + return groups; } catch (Exception ex) { @@ -44,7 +44,7 @@ namespace API.Controllers try { _logger.LogInformation("Getting a langue with name {name}",name); var group = await _service.GetById(name); - return Ok(group); + return group; } catch (Exception ex) { @@ -55,7 +55,8 @@ namespace API.Controllers return StatusCode(400, ex.Message); } } - + //On ne peut pas changer la langue car son nom est son Id + /* [HttpPut] public async Task> UpdateLangue([FromQuery]LangueDTO langue) { @@ -63,7 +64,7 @@ namespace API.Controllers { _logger.LogInformation("Updating a langue with name : {name}", langue.name); var updatedGroup = await _service.Update(langue); - return Ok(updatedGroup); + return updatedGroup; } catch (Exception ex) { @@ -73,7 +74,7 @@ namespace API.Controllers // Retourner une réponse d'erreur return StatusCode(400, ex.Message); } - } + }*/ [HttpPost] public async Task> AddLangue([FromQuery]LangueDTO langue) @@ -81,16 +82,8 @@ namespace API.Controllers try { _logger.LogInformation("Adding a langue with name : {name}", langue.name); - if (langue.name == null) - { - return BadRequest("Name is required"); - } - if (_service.GetById(langue.name) != null) - { - return BadRequest("Name already exists"); - } var newGroup = await _service.Add(langue); - return Ok(newGroup); + return newGroup; } catch (Exception ex) { @@ -109,7 +102,7 @@ namespace API.Controllers { _logger.LogInformation("Deleting a langue with name : {name}", name); var group = await _service.Delete(name); - return Ok(group); + return group; } catch (Exception ex) { diff --git a/Project/EntityFramework/API/Controllers/RoleController.cs b/Project/EntityFramework/API/Controllers/RoleController.cs index e98c3bf..0d3e239 100644 --- a/Project/EntityFramework/API/Controllers/RoleController.cs +++ b/Project/EntityFramework/API/Controllers/RoleController.cs @@ -20,13 +20,13 @@ namespace API.Controllers } [HttpGet] - public async Task>> GetRoles(int index, int count) + public async Task>> GetRoles(int index, int count) { try { _logger.LogInformation("Getting Roles "); var groups = await _service.Gets(index, count); - return Ok(groups); + return groups; } catch (Exception ex) { @@ -45,7 +45,7 @@ namespace API.Controllers { _logger.LogInformation("Getting a role with id {id}", id); var group = await _service.GetById(id); - return Ok(group); + return group; } catch (Exception ex) { @@ -63,7 +63,7 @@ namespace API.Controllers try { _logger.LogInformation("Updating a role with id : {id}", role.Id); var updatedGroup = await _service.Update(role); - return Ok(updatedGroup); + return updatedGroup; } catch (Exception ex) { @@ -83,7 +83,7 @@ namespace API.Controllers _logger.LogInformation("Adding a role with id : {id}", role.Id); role.Id = _service.Gets(0, 0).Result.TotalCount + 1; var newGroup = await _service.Add(role); - return Ok(newGroup); + return newGroup; } catch (Exception ex) { @@ -101,7 +101,7 @@ namespace API.Controllers try { _logger.LogInformation("Deleting a role with id : {id}", id); var group = await _service.Delete(id); - return Ok(group); + return group; } catch (Exception ex) { diff --git a/Project/EntityFramework/API/Db.in_english_please.db-shm b/Project/EntityFramework/API/Db.in_english_please.db-shm index 1315390..f94f32b 100644 Binary files a/Project/EntityFramework/API/Db.in_english_please.db-shm and b/Project/EntityFramework/API/Db.in_english_please.db-shm differ diff --git a/Project/EntityFramework/API/Db.in_english_please.db-wal b/Project/EntityFramework/API/Db.in_english_please.db-wal index 42a1ad2..a5f910f 100644 Binary files a/Project/EntityFramework/API/Db.in_english_please.db-wal and b/Project/EntityFramework/API/Db.in_english_please.db-wal differ diff --git a/Project/EntityFramework/API/Program.cs b/Project/EntityFramework/API/Program.cs index 2621fda..99ba58c 100644 --- a/Project/EntityFramework/API/Program.cs +++ b/Project/EntityFramework/API/Program.cs @@ -10,6 +10,7 @@ using DTOToEntity; using DTO; using System; using Microsoft.AspNetCore.Mvc; +using Microsoft.IdentityModel.Tokens; var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -24,8 +25,7 @@ builder.Services.AddApiVersioning(o => o.AssumeDefaultVersionWhenUnspecified = true; o.ReportApiVersions = true; }); - -builder.Services.AddScoped(); +builder.Services.AddScoped(); builder.Services.AddScoped,LangueService>(); builder.Services.AddScoped,RoleService>(); builder.Services.AddScoped,TranslateService>(); @@ -33,7 +33,6 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); var app = builder.Build(); - // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { diff --git a/Project/EntityFramework/DTO/LangueDTO.cs b/Project/EntityFramework/DTO/LangueDTO.cs index 254521c..2ecb394 100644 --- a/Project/EntityFramework/DTO/LangueDTO.cs +++ b/Project/EntityFramework/DTO/LangueDTO.cs @@ -8,7 +8,8 @@ namespace DTO { public class LangueDTO { - public string name { get; set; } + public string + name { get; set; } //public ICollection vocabularys { get; set; } = new List(); } diff --git a/Project/EntityFramework/DTOToEntity/GroupService.cs b/Project/EntityFramework/DTOToEntity/GroupService.cs index 5e7daae..938144b 100644 --- a/Project/EntityFramework/DTOToEntity/GroupService.cs +++ b/Project/EntityFramework/DTOToEntity/GroupService.cs @@ -88,10 +88,22 @@ namespace DTOToEntity public async Task Update(GroupDTO group) { - var groupEntity = group.ToEntity(); - var res = context.Groups.Update(groupEntity); + if(group == null) + { + throw new ArgumentNullException(); + } + var existingGroup = await context.Groups.FindAsync(group.Id); + if (existingGroup == null) + { + throw new Exception("Group not found"); + } + existingGroup.year = group.Year; + existingGroup.sector = group.sector; + existingGroup.Num = group.Num; await context.SaveChangesAsync(); - return res.Entity.ToDTO(); + return existingGroup.ToDTO(); } } + + } diff --git a/Project/EntityFramework/DTOToEntity/LangueService.cs b/Project/EntityFramework/DTOToEntity/LangueService.cs index 1a0d9fb..58bca98 100644 --- a/Project/EntityFramework/DTOToEntity/LangueService.cs +++ b/Project/EntityFramework/DTOToEntity/LangueService.cs @@ -14,6 +14,11 @@ namespace DTOToEntity { private readonly StubbedContext _context = new StubbedContext(); public LangueService() { } + + public LangueService(StubbedContext context) + { + this._context = context; + } public async Task Add(LangueDTO langue) { var langueEntity = langue.ToEntity(); diff --git a/Project/EntityFramework/DTOToEntity/RoleService.cs b/Project/EntityFramework/DTOToEntity/RoleService.cs index 45ed31d..3908d60 100644 --- a/Project/EntityFramework/DTOToEntity/RoleService.cs +++ b/Project/EntityFramework/DTOToEntity/RoleService.cs @@ -13,6 +13,12 @@ namespace DTOToEntity public class RoleService : IService { private readonly StubbedContext _context = new StubbedContext(); + + public RoleService() { } + public RoleService(StubbedContext context) + { + _context = context; + } public async Task Add(RoleDTO role) { var roleEntity = role.ToEntity(); @@ -32,9 +38,9 @@ namespace DTOToEntity { throw new Exception("Role not found"); } - _context.Roles.Remove(role); + var res = _context.Roles.Remove(role); await _context.SaveChangesAsync(); - return role.ToDTO(); + return res.Entity.ToDTO(); } public async Task GetById(object id) @@ -55,13 +61,16 @@ namespace DTOToEntity public async Task Update(RoleDTO role) { - RoleEntity? roleEntity = await _context.Roles.FirstOrDefaultAsync(r => r.Id == role.Id); if (role == null) { - throw new Exception("Role not found"); + throw new ArgumentNullException(); + } + var roleEntity = await _context.Roles.FindAsync(role.Id); + if (roleEntity != null) + { + throw new Exception("role not found"); } - roleEntity= role.ToEntity(); - _context.Roles.Update(roleEntity); + roleEntity.Name = role.Name; await _context.SaveChangesAsync(); return roleEntity.ToDTO(); } diff --git a/Project/EntityFramework/TU/GroupTU.cs b/Project/EntityFramework/TU/GroupTU.cs index da8446c..dd37f60 100644 --- a/Project/EntityFramework/TU/GroupTU.cs +++ b/Project/EntityFramework/TU/GroupTU.cs @@ -2,13 +2,11 @@ using DbContextLib; using Entities; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging; using StubbedContextLib; using API.Controllers; using DTOToEntity; using DTO; -using Microsoft.AspNetCore.Mvc; using Moq; namespace TU @@ -16,9 +14,6 @@ namespace TU [TestClass] public class GroupTU { - private static ILogger _logger = new NullLogger(); - private static IGroupService _booksService = new GroupService(); - private GroupController _controller = new GroupController(_booksService, _logger); @@ -156,12 +151,12 @@ namespace TU { context.Database.EnsureCreated(); GroupEntity g1 = new GroupEntity { Id = 4, Num = 4, sector = "sect3", year = 2020 }; + GroupDTO updatedGroupDTO = new GroupDTO { Id = 4, Num = 2, sector = "sect4", Year = 2021 }; await context.Groups.AddAsync(g1); await context.SaveChangesAsync(); var mockLogger = new Mock>(); var controller = new GroupController(new GroupService(context), mockLogger.Object); - var updatedGroupDTO = new GroupDTO { Id = 4, Num = 2, sector = "sect4", Year = 2021 }; var result = await controller.UpdateGroup(updatedGroupDTO); diff --git a/Project/EntityFramework/TU/LangueTU.cs b/Project/EntityFramework/TU/LangueTU.cs index 346125a..be30490 100644 --- a/Project/EntityFramework/TU/LangueTU.cs +++ b/Project/EntityFramework/TU/LangueTU.cs @@ -8,15 +8,13 @@ using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging; using StubbedContextLib; using DTO; +using Moq; namespace TU { [TestClass] public class LangueTU { - private static ILogger _logger = new NullLogger(); - private static IService _langueService = new LangueService(); - private LangueController _controller = new LangueController(_langueService,_logger); [TestMethod] public async Task TestAddLangue() @@ -26,19 +24,24 @@ namespace TU var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; + using (var context = new StubbedContext(options)) { context.Database.EnsureCreated(); - var vocab = new VocabularyEntity { word = "test", Langue = null }; - var newLangue = new LangueDTO { name = "français" }; - var res = await _controller.AddLangue(newLangue); - Assert.IsNotNull(res); - Assert.AreEqual("français", res.Value.name); + // Créer un mock pour le logger + var mockLogger = new Mock>(); + + var controller = new LangueController(new LangueService(context), mockLogger.Object); + var newLangue = new LangueDTO { name = "test" }; + var result = await controller.AddLangue(newLangue); + Assert.IsNotNull(result.Value); + Assert.AreEqual(newLangue.name, result.Value.name); } + } [TestMethod] public async Task TestDeleteLangue() @@ -48,26 +51,29 @@ namespace TU var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; + using (var context = new StubbedContext(options)) { context.Database.EnsureCreated(); - var vocab = new VocabularyEntity { word = "test", Langue = null }; - var newLangue = new LangueEntity { name = "français", vocabularys=[vocab] }; - vocab.Langue = newLangue; - await context.Langues.AddAsync(newLangue); - await context.SaveChangesAsync(); - - var langue = await context.Langues.FirstOrDefaultAsync(b => b.name == "français"); - Assert.IsNotNull(langue); - Assert.AreEqual("français", langue.name); - Assert.AreEqual(vocab, langue.vocabularys.First()); - - context.Langues.Remove(newLangue); - await context.SaveChangesAsync(); - var langue2 = await context.Langues.FirstOrDefaultAsync(b => b.name == "français"); - Assert.IsNull(langue2); + var newLangue = new LangueDTO { name = "test" }; + await context.Langues.AddAsync(newLangue.ToEntity()); + // Créer un mock pour le logger + var mockLogger = new Mock>(); + + var controller = new LangueController(new LangueService(context), mockLogger.Object); + + + var result = await controller.DeleteLangue("test"); + Assert.IsNotNull(result.Value); + Assert.AreEqual(newLangue.name, result.Value.name); + var res = await context.Langues.FirstOrDefaultAsync(l => l.name == "test"); + Assert.IsNull(res); + + } } + + /* [TestMethod] public async Task TestUpdateLangue() { @@ -76,29 +82,76 @@ namespace TU var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; + using (var context = new StubbedContext(options)) { context.Database.EnsureCreated(); + var newLangue = new LangueDTO { name = "test" }; + await context.Langues.AddAsync(newLangue.ToEntity()); + // Créer un mock pour le logger + var mockLogger = new Mock>(); + + var controller = new LangueController(new LangueService(context), mockLogger.Object); + newLangue. + + var result = await controller.UpdateLangue(new); + Assert.IsNotNull(result.Value); + Assert.AreEqual(newLangue.name, result.Value.name); + var res = await context.Langues.FirstOrDefaultAsync(l => l.name == "test"); + Assert.IsNull(res); + + + } + }*/ + [TestMethod] + public async Task TestGetById() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + var newLangue = new LangueDTO { name = "test" }; + await context.Langues.AddAsync(newLangue.ToEntity()); + // Créer un mock pour le logger + var mockLogger = new Mock>(); + + var controller = new LangueController(new LangueService(context), mockLogger.Object); + + + var result = await controller.GetLangue("test"); + Assert.IsNotNull(result.Value); + Assert.AreEqual(newLangue.name, result.Value.name); + } + } + + [TestMethod] + public async Task TestGetGroups() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new StubbedContext(options)) + { + context.Database.EnsureCreated(); + var mockLogger = new Mock>(); + + var controller = new LangueController(new LangueService(context), mockLogger.Object); + + + var result = await controller.GetLangues(0, 5); + Assert.IsNotNull(result.Value); + Assert.AreEqual(2, result.Value.TotalCount); + Assert.AreEqual("English", result.Value.Items.ToList()[0].name); + Assert.AreEqual("French", result.Value.Items.ToList()[1].name); - var newBook = new GroupEntity { Id = 2, year = 2, sector = "medecin" }; - await context.Groups.AddAsync(newBook); - await context.SaveChangesAsync(); - - var group1 = await context.Groups.FirstOrDefaultAsync(b => b.sector == "medecin"); - Assert.IsNotNull(group1); - Assert.AreEqual("medecin", group1.sector); - Assert.AreEqual(2, group1.year); - Assert.AreEqual(2, group1.Id); - - group1.sector = "informatique"; - group1.year = 3; - context.Groups.Update(group1); - await context.SaveChangesAsync(); - var group2 = await context.Groups.FirstOrDefaultAsync(b => b.sector == "informatique"); - Assert.IsNotNull(group2); - Assert.AreEqual("informatique", group2.sector); - Assert.AreEqual(3, group2.year); - Assert.AreEqual(2, group2.Id); } } } diff --git a/Project/EntityFramework/TU/RoleTU.cs b/Project/EntityFramework/TU/RoleTU.cs index 3afc9c3..c112efe 100644 --- a/Project/EntityFramework/TU/RoleTU.cs +++ b/Project/EntityFramework/TU/RoleTU.cs @@ -1,7 +1,12 @@ +using API.Controllers; using DbContextLib; +using DTO; +using DTOToEntity; using Entities; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; +using Moq; using StubbedContextLib; namespace TU @@ -17,18 +22,23 @@ namespace TU var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; + using (var context = new StubbedContext(options)) { context.Database.EnsureCreated(); - var user = new UserEntity { Id = 1, Name = "name", UserName = "username", NickName = "nickname", ExtraTime = true, GroupId = 1, Password = "1234", Email = "" }; - var newRole = new RoleEntity { Id=4, Name = "user" , Users = [user] }; - await context.Roles.AddAsync(newRole); - await context.SaveChangesAsync(); - var role = await context.Roles.FirstOrDefaultAsync(b => b.Name == "user"); - Assert.IsNotNull(role); - Assert.AreEqual("user", role.Name); - Assert.AreEqual(user, role.Users.First()); + var mockLogger = new Mock>(); + + var controller = new RoleController(new RoleService(context), mockLogger.Object); + + var newRole = new RoleDTO { Id = 100, Name = "test" }; + + var result = await controller.AddRole(newRole); + + Assert.IsNotNull(result.Value); + //ici on met 4 pour verifier que le Id n'est pas celui que l'on donne mais bien : CountList + 1 + Assert.AreEqual(4, result.Value.Id); + Assert.AreEqual("test", result.Value.Name); } } [TestMethod] @@ -39,26 +49,23 @@ namespace TU var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; + using (var context = new StubbedContext(options)) { context.Database.EnsureCreated(); - var user = new UserEntity { Id = 4, Name = "name", UserName = "username", NickName = "nickname", ExtraTime = true, GroupId = 1, Password = "1234", Email = "", RoleId=5 }; - var user1 = new UserEntity { Id = 5, Name = "name2", UserName = "username2", NickName = "nickname2", ExtraTime = true, GroupId = 2, Password = "1234", Email = "", RoleId=5 }; - var newRole = new RoleEntity { Id=5,Name = "user" }; - newRole.Users.Add(user); - await context.Roles.AddAsync(newRole); - await context.SaveChangesAsync(); - var role = await context.Roles.FirstOrDefaultAsync(b => b.Name == "user"); - Assert.AreEqual(newRole, role); - role.Name = "admin"; - context.Roles.Update(role); + var mockLogger = new Mock>(); - await context.SaveChangesAsync(); - var role1 = await context.Roles.FirstOrDefaultAsync(b => b. Name == "admin"); - Assert.IsNotNull(role1); - Assert.AreEqual("admin", role1.Name); - Assert.AreEqual(user, role1.Users.First()); + var controller = new RoleController(new RoleService(context), mockLogger.Object); + + var newRole = new RoleDTO { Id = 4, Name = "test" }; + await controller.AddRole(newRole); + var newRole2 = new RoleDTO { Id = 4, Name = "modifié" }; + await controller.UpdateRole(newRole2); + var res = await context.Roles.FirstOrDefaultAsync(r =>r.Id == 4); + Assert.IsNotNull(res); + Assert.AreEqual(4, res.Id); + Assert.AreEqual("modifié", res.Name); } } @@ -70,21 +77,24 @@ namespace TU var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; + using (var context = new StubbedContext(options)) { context.Database.EnsureCreated(); - var newRole = new RoleEntity { Name = "user" }; - await context.Roles.AddAsync(newRole); - await context.SaveChangesAsync(); + var mockLogger = new Mock>(); - var role = await context.Roles.FirstOrDefaultAsync(b => b.Name == "user"); - context.Roles.Remove(role); + var controller = new RoleController(new RoleService(context), mockLogger.Object); + + var newRole = new RoleDTO { Id = 4, Name = "test" }; + await context.Roles.AddAsync(newRole.ToEntity()); await context.SaveChangesAsync(); - var role1 = await context.Roles.FirstOrDefaultAsync(b => b.Name == "user"); - Assert.IsNull(role1); + var result = await controller.DeleteRole(4); + Assert.IsNotNull(result.Value); + Assert.AreEqual(4, result.Value.Id); + Assert.AreEqual("test", result.Value.Name); } } - } + } } \ No newline at end of file