diff --git a/.drone.yml b/.drone.yml index 5014581..5250971 100644 --- a/.drone.yml +++ b/.drone.yml @@ -104,7 +104,8 @@ steps: from_secret: SECRET_REGISTRY_USERNAME password: from_secret: SECRET_REGISTRY_PASSWORD - + + - name: deploy-container-mysql image: hub.codefirst.iut.uca.fr/thomas.bellembois/codefirst-dockerproxy-clientdrone:latest environment: @@ -122,6 +123,7 @@ steps: CODEFIRST_CLIENTDRONE_ENV_MARIADB_PASSWORD: from_secret: db_password ADMINS: antoinejourdain,patrickbrugiere,luciegoigoux,marcchevaldonne + - name: deploy-container diff --git a/Project/EntityFramework/API/API.csproj b/Project/EntityFramework/API/API.csproj index eebe763..0a29e7c 100644 --- a/Project/EntityFramework/API/API.csproj +++ b/Project/EntityFramework/API/API.csproj @@ -5,7 +5,7 @@ enable enable $(MSBuildProjectDirectory) - 30f8a49f-7e7d-4394-80f0-b36f39b45831 + 1540d456-1696-425e-b511-611542ce7c5e Linux diff --git a/Project/EntityFramework/API/API.csproj.user b/Project/EntityFramework/API/API.csproj.user index 031db34..448f454 100644 --- a/Project/EntityFramework/API/API.csproj.user +++ b/Project/EntityFramework/API/API.csproj.user @@ -5,4 +5,7 @@ MvcControllerEmptyScaffolder root/Common/MVC/Controller + + ProjectDebugger + \ No newline at end of file diff --git a/Project/EntityFramework/API/Controllers/RoleController.cs b/Project/EntityFramework/API/Controllers/RoleController.cs index c448c0d..eac740f 100644 --- a/Project/EntityFramework/API/Controllers/RoleController.cs +++ b/Project/EntityFramework/API/Controllers/RoleController.cs @@ -41,7 +41,7 @@ namespace API.Controllers } [HttpGet("{id}")] - public async Task> GetRole(int id) + public async Task> GetRole(long id) { try { @@ -80,7 +80,7 @@ namespace API.Controllers } [HttpDelete("{id}")] - public async Task> DeleteRole(int id) + public async Task> DeleteRole(long id) { try { _logger.LogInformation("Deleting a role with id : {id}", id); diff --git a/Project/EntityFramework/API/Controllers/TranslateController.cs b/Project/EntityFramework/API/Controllers/TranslateController.cs index da08816..73ae8e9 100644 --- a/Project/EntityFramework/API/Controllers/TranslateController.cs +++ b/Project/EntityFramework/API/Controllers/TranslateController.cs @@ -40,7 +40,7 @@ namespace API.Controllers } } [HttpGet("{id}")] - public async Task> GetTranslate(int id) + public async Task> GetTranslate(long id) { try { _logger.LogInformation("Getting a Translate with id {id}", id); @@ -76,7 +76,7 @@ namespace API.Controllers } [HttpDelete("{id}")] - public async Task> DeleteTranslate(int id) + public async Task> DeleteTranslate(long id) { try { _logger.LogInformation("Deleting a Translate with id : {id}", id); diff --git a/Project/EntityFramework/API/Db.in_english_please.db b/Project/EntityFramework/API/Db.in_english_please.db index d850996..5f360cf 100644 Binary files a/Project/EntityFramework/API/Db.in_english_please.db and b/Project/EntityFramework/API/Db.in_english_please.db differ diff --git a/Project/EntityFramework/API/Properties/launchSettings.json b/Project/EntityFramework/API/Properties/launchSettings.json index 87920c5..4aebf86 100644 --- a/Project/EntityFramework/API/Properties/launchSettings.json +++ b/Project/EntityFramework/API/Properties/launchSettings.json @@ -1,33 +1,24 @@ -{ - "$schema": "http://json.schemastore.org/launchsettings.json", - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:32547", - "sslPort": 44345 - } - }, +{ "profiles": { "http": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", - "applicationUrl": "http://localhost:5124", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "dotnetRunMessages": true, + "applicationUrl": "http://localhost:5124" }, "https": { "commandName": "Project", - "dotnetRunMessages": true, "launchBrowser": true, "launchUrl": "swagger", - "applicationUrl": "https://localhost:7013;http://localhost:5124", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" - } + }, + "dotnetRunMessages": true, + "applicationUrl": "https://localhost:7013;http://localhost:5124" }, "IIS Express": { "commandName": "IISExpress", @@ -36,6 +27,26 @@ "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } + }, + "Docker": { + "commandName": "Docker", + "launchBrowser": true, + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", + "environmentVariables": { + "ASPNETCORE_HTTPS_PORTS": "8081", + "ASPNETCORE_HTTP_PORTS": "8080" + }, + "publishAllPorts": true, + "useSSL": true + } + }, + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:32547", + "sslPort": 44345 } } -} +} \ No newline at end of file diff --git a/Project/EntityFramework/DTOToEntity/GroupService.cs b/Project/EntityFramework/DTOToEntity/GroupService.cs index 45c1cd1..a27893d 100644 --- a/Project/EntityFramework/DTOToEntity/GroupService.cs +++ b/Project/EntityFramework/DTOToEntity/GroupService.cs @@ -14,16 +14,16 @@ namespace DTOToEntity public class GroupService : IGroupService { - private readonly StubbedContext context = new StubbedContext(); + private readonly UnitOfWork context = new UnitOfWork(); public GroupService() { } - public GroupService(StubbedContext context) + public GroupService(StubbedContext _context) { - this.context = context; + context = new UnitOfWork(_context); } public async Task Add(GroupDTO group) { @@ -31,21 +31,21 @@ namespace DTOToEntity { throw new ArgumentNullException(); } - var groupEntity = group.ToEntity(); - var res = context.Groups.Add(groupEntity); - await context.SaveChangesAsync(); + GroupEntity groupEntity = group.ToEntity(); + context.GroupRepository.Insert(groupEntity); + await context.SaveChangesAsync(); - return res.Entity.ToDTO(); ; + return groupEntity.ToDTO(); ; } public async Task AddUserToGroup(long idUser, long idGroup) { - var group = context.Groups.Find(idGroup); + var group = context.GroupRepository.GetById(idGroup); if (group == null) { throw new Exception("Group not found"); } - var user = context.Users.Find(idUser); + var user = context.UserRepository.GetById(idUser); if (user == null) { throw new Exception("User not found"); @@ -58,17 +58,18 @@ namespace DTOToEntity public async Task AddVocabularyListToGroup(long vocabId, long groupId) { - var group = context.Groups.Find(groupId); + var group = context.GroupRepository.GetById(groupId); if (group == null) { throw new Exception("Group not found"); } - var vocab = context.VocabularyLists.Find(vocabId); + var vocab = context.VocabularyListRepository.GetById(vocabId); if (vocab == null) { throw new Exception("VocabularyList not found"); } group.GroupVocabularyList.Add(vocab); + await context.SaveChangesAsync(); return vocab.ToDTO(); @@ -76,10 +77,10 @@ namespace DTOToEntity public async Task Delete(object id) { - var group = await context.Groups.FindAsync((long)id); + var group = context.GroupRepository.GetById((long)id); if (group != null) - { - context.Groups.Remove(group); + { + context.GroupRepository.Delete(group); await context.SaveChangesAsync(); }else { @@ -90,7 +91,7 @@ namespace DTOToEntity public async Task GetById(object id) { - var group = await context.Groups.FindAsync((long)id); + var group = context.GroupRepository.GetById((long) id); if (group == null) { throw new Exception("Group not found"); @@ -100,26 +101,26 @@ namespace DTOToEntity public async Task> GetByNum(int index, int count, int num) { - var groups = context.Groups.Where(g => g.Num == num).Skip(index * count).Take(count); - return new PageResponse(groups.ToList().Select(g => g.ToDTO()), context.Groups.Count()); + var groups = context.GroupRepository.GetItems(filter: g => g.Num == num, index, count); + return new PageResponse(groups.ToList().Select(g => g.ToDTO()), context.GroupRepository.GetItems(0, 1000000000).Count()); } public async Task> GetBySector(int index, int count, string sector) { - var groups = context.Groups.Where(g => g.sector == sector).Skip(index * count).Take(count); - return new PageResponse(groups.ToList().Select(g => g.ToDTO()), context.Groups.Count()); + var groups = context.GroupRepository.GetItems(filter: g => g.sector == sector, index, count); + return new PageResponse(groups.ToList().Select(g => g.ToDTO()), context.GroupRepository.GetItems(0, 100000000).Count()); } public async Task> GetByYear(int index, int count, int year) { - var groups = context.Groups.Where(g => g.year == year).Skip(index * count).Take(count); - return new PageResponse(groups.ToList().Select(g => g.ToDTO()), context.Groups.Count()); + var groups = context.GroupRepository.GetItems(filter: g => g.year == year, index, count); + return new PageResponse(groups.ToList().Select(g => g.ToDTO()), context.GroupRepository.GetItems(0,100000000).Count()); } public async Task> Gets(int index, int count) { - IEnumerable groups = await context.Groups.Skip(index * count).Take(count).ToListAsync(); - return new PageResponse(groups.ToList().Select(g => g.ToDTO()), context.Groups.Count()); + IEnumerable groups = context.GroupRepository.GetItems(index,count); + return new PageResponse(groups.ToList().Select(g => g.ToDTO()), context.GroupRepository.GetItems(0, 1000000000).Count()); } public async Task Update(GroupDTO group) @@ -128,7 +129,7 @@ namespace DTOToEntity { throw new ArgumentNullException(); } - var existingGroup = await context.Groups.FindAsync(group.Id); + var existingGroup = context.GroupRepository.GetById(group.Id); if (existingGroup == null) { throw new Exception("Group not found"); diff --git a/Project/EntityFramework/DTOToEntity/LangueService.cs b/Project/EntityFramework/DTOToEntity/LangueService.cs index 75e41cb..280f015 100644 --- a/Project/EntityFramework/DTOToEntity/LangueService.cs +++ b/Project/EntityFramework/DTOToEntity/LangueService.cs @@ -1,4 +1,5 @@ -using DTO; +using DbContextLib; +using DTO; using Entities; using Microsoft.EntityFrameworkCore; using StubbedContextLib; @@ -12,29 +13,29 @@ namespace DTOToEntity { public class LangueService : IService { - private readonly StubbedContext _context = new StubbedContext(); + private readonly UnitOfWork _context = new UnitOfWork(); public LangueService() { } public LangueService(StubbedContext context) { - this._context = context; + this._context = new UnitOfWork(context); } public async Task Add(LangueDTO langue) { var langueEntity = langue.ToEntity(); - var res = _context.Langues.Add(langueEntity); + _context.LangueRepository.Insert(langueEntity); await _context.SaveChangesAsync(); - return res.Entity.ToDTO(); + return langue; } public async Task Delete(object id) { - var langue = await _context.Langues.FindAsync(id); + var langue = _context.LangueRepository.GetById(id); if (langue != null) { - _context.Langues.Remove(langue); - await _context.SaveChangesAsync(); + _context.LangueRepository.Delete(langue); + await _context.SaveChangesAsync(); } else { throw new Exception("Langue not found"); @@ -44,7 +45,7 @@ namespace DTOToEntity public async Task GetById(object id) { - var langue = await _context.Langues.FindAsync(id); + var langue = _context.LangueRepository.GetById(id); if (langue == null) { throw new Exception("Langue not found"); @@ -55,13 +56,13 @@ namespace DTOToEntity public async Task> Gets(int index,int count) { - IEnumerable langues = await _context.Langues.Skip(index * count).Take(count).ToListAsync(); - return new PageResponse(langues.ToList().Select(l => l.ToDTO()), _context.Langues.Count()); + IEnumerable langues = _context.LangueRepository.GetItems(index, count); + return new PageResponse(langues.ToList().Select(l => l.ToDTO()), _context.LangueRepository.GetItems(0, 1000000000).Count()); } public async Task Update(LangueDTO langue) { - LangueEntity? langueToUpdate = await _context.Langues.FindAsync(langue.name); + LangueEntity? langueToUpdate = _context.LangueRepository.GetById(langue.name); if (langueToUpdate == null) { throw new Exception("Langue not found"); diff --git a/Project/EntityFramework/DTOToEntity/RoleService.cs b/Project/EntityFramework/DTOToEntity/RoleService.cs index b84f50a..a319dc7 100644 --- a/Project/EntityFramework/DTOToEntity/RoleService.cs +++ b/Project/EntityFramework/DTOToEntity/RoleService.cs @@ -1,4 +1,5 @@ -using DTO; +using DbContextLib; +using DTO; using Entities; using Microsoft.EntityFrameworkCore; using StubbedContextLib; @@ -12,40 +13,41 @@ namespace DTOToEntity { public class RoleService : IService { - private readonly StubbedContext _context = new StubbedContext(); + private readonly UnitOfWork _context = new UnitOfWork(); public RoleService() { } public RoleService(StubbedContext context) + { + _context = new UnitOfWork(context); + } + + public RoleService(UnitOfWork context) { _context = context; } public async Task Add(RoleDTO role) { var roleEntity = role.ToEntity(); - if(roleEntity == null) - { - throw new ArgumentNullException(); - } - var res = _context.Roles.Add(roleEntity); + _context.RoleRepository.Insert(roleEntity); await _context.SaveChangesAsync(); - return res.Entity.ToDTO(); + return role; } public async Task Delete(object id) { - RoleEntity? role = await _context.Roles.FirstOrDefaultAsync(r => r.Id == (int)id); + RoleEntity role = _context.RoleRepository.GetById((long)id); if (role == null) { throw new Exception("Role not found"); } - var res = _context.Roles.Remove(role); + _context.RoleRepository.Delete((long)id); await _context.SaveChangesAsync(); - return res.Entity.ToDTO(); + return role.ToDTO(); } public async Task GetById(object id) { - RoleEntity? role = await _context.Roles.FirstOrDefaultAsync(r => r.Id == (int)id); + RoleEntity? role = _context.RoleRepository.GetById((long)id); if (role == null) { throw new Exception("Role not found"); @@ -55,8 +57,8 @@ namespace DTOToEntity public async Task> Gets(int index, int count) { - IEnumerable roles = await _context.Roles.Skip(index * count).Take(count).ToListAsync(); - return new PageResponse(roles.ToList().Select(r => r.ToDTO()), _context.Roles.Count()); + IEnumerable roles = _context.RoleRepository.GetItems(index, count); + return new PageResponse(roles.ToList().Select(r => r.ToDTO()), _context.RoleRepository.GetItems(0,1000000000).Count()); } public async Task Update(RoleDTO role) @@ -65,12 +67,13 @@ namespace DTOToEntity { throw new ArgumentNullException(); } - var roleEntity = await _context.Roles.FindAsync(role.Id); + var roleEntity = _context.RoleRepository.GetById(role.Id); if (roleEntity != null) { throw new Exception("role not found"); } roleEntity.Name = role.Name; + _context.RoleRepository.Update(roleEntity); await _context.SaveChangesAsync(); return roleEntity.ToDTO(); } diff --git a/Project/EntityFramework/DTOToEntity/TranslateService.cs b/Project/EntityFramework/DTOToEntity/TranslateService.cs index 7739b82..4bdca81 100644 --- a/Project/EntityFramework/DTOToEntity/TranslateService.cs +++ b/Project/EntityFramework/DTOToEntity/TranslateService.cs @@ -1,4 +1,5 @@ -using DTO; +using DbContextLib; +using DTO; using Microsoft.EntityFrameworkCore; using StubbedContextLib; using System; @@ -11,18 +12,18 @@ namespace DTOToEntity { public class TranslateService : ITranslateService { - private readonly StubbedContext _context = new StubbedContext(); + private readonly UnitOfWork _context = new UnitOfWork(); public TranslateService() { } public TranslateService(StubbedContext context) { - _context = context; + _context = new UnitOfWork(context); } public async Task Add(TranslateDTO translate) { var translateEntity = translate.ToEntity(); - _context.Translates.Add(translateEntity); + _context.TranslateRepository.Insert(translateEntity); await _context.SaveChangesAsync(); return translateEntity.ToDTO(); @@ -30,12 +31,12 @@ namespace DTOToEntity public async Task AddVocabToTranslate(string vocabId, long translateId) { - var vocab = _context.Vocabularys.Find(vocabId); + var vocab = _context.VocabularyRepository.GetById(vocabId); if (vocab == null) { throw new Exception("Vocabulary not found"); } - var translate = _context.Translates.Find(translateId); + var translate = _context.TranslateRepository.GetById(translateId); if (translate == null) { throw new Exception("Translate not found"); @@ -47,10 +48,10 @@ namespace DTOToEntity public async Task Delete(object id) { - var translate = await _context.Translates.FirstOrDefaultAsync(t => t.Id == (int)id); + var translate = _context.TranslateRepository.GetById((long)id); if (translate != null) { - _context.Translates.Remove(translate); + _context.TranslateRepository.Delete(translate); await _context.SaveChangesAsync(); } else @@ -62,7 +63,7 @@ namespace DTOToEntity public async Task GetById(object id) { - var translate = await _context.Translates.FirstOrDefaultAsync(t => t.Id == (int)id); + var translate = _context.TranslateRepository.GetById((long)id); if (translate == null) { throw new Exception("Translate not found"); @@ -72,24 +73,24 @@ namespace DTOToEntity public async Task> Gets(int index, int count) { - var translates = await _context.Translates.Skip(index * count).Take(count).ToListAsync(); + var translates = _context.TranslateRepository.GetItems(index, count); if(translates == null) { throw new Exception("No translates found"); } - return new PageResponse( translates.Select(t => t.ToDTO()), _context.Translates.Count()); + return new PageResponse( translates.Select(t => t.ToDTO()), _context.TranslateRepository.GetItems(0, 100000000).Count()); } public async Task Update(TranslateDTO translate) { - var translateEntity = await _context.Translates.FirstOrDefaultAsync(t => t.Id == translate.Id); + var translateEntity = _context.TranslateRepository.GetById(translate.Id); if (translateEntity == null) { throw new Exception("Translate not found"); } translateEntity.WordsId = translate.WordsId; translateEntity.VocabularyListVocId = translate.VocabularyListVocId; - _context.Translates.Update(translateEntity); + _context.TranslateRepository.Update(translateEntity); await _context.SaveChangesAsync(); return translateEntity.ToDTO(); } diff --git a/Project/EntityFramework/DTOToEntity/UserService.cs b/Project/EntityFramework/DTOToEntity/UserService.cs index 676430d..47febdc 100644 --- a/Project/EntityFramework/DTOToEntity/UserService.cs +++ b/Project/EntityFramework/DTOToEntity/UserService.cs @@ -1,9 +1,11 @@ -using DTO; +using DbContextLib; +using DTO; using Entities; using Microsoft.EntityFrameworkCore; using StubbedContextLib; using System; using System.Collections.Generic; +using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -12,12 +14,12 @@ namespace DTOToEntity { public class UserService : IUserService { - private StubbedContext _context = new StubbedContext(); + private UnitOfWork _context = new UnitOfWork(); public UserService() { } public UserService(StubbedContext context) { - _context = context; + _context = new UnitOfWork(context); } public async Task Add(UserDTO user) { @@ -26,32 +28,32 @@ namespace DTOToEntity { throw new Exception("User Entity is null"); } - var result = _context.Users.Add(userEntity); + _context.UserRepository.Insert(userEntity); await _context.SaveChangesAsync(); - return result.Entity.ToDTO(); + return user; } public async Task Delete(object id) { - UserEntity? user = await _context.Users.FirstOrDefaultAsync(u => u.Id == (long)id); + UserEntity? user = _context.UserRepository.GetById((long)id); if(user == null) { throw new Exception("User not found"); } - _context.Users.Remove(user); + _context.UserRepository.Delete((long)id); await _context.SaveChangesAsync(); return user.ToDTO(); } public async Task> GetByGroup(int index, int count, long group) { - var users = _context.Users.Where(u => u.GroupId == group).Skip(index * count).Take(count); - return new PageResponse(users.Select(u => u.ToDTO()), _context.Users.Count()); + var users = _context.UserRepository.GetItems(index,count).Where(u => u.GroupId == group); + return new PageResponse(users.Select(u => u.ToDTO()), _context.UserRepository.GetItems(0,1000000000).Count()); } public async Task GetById(object id) { - var user = await _context.Users.FirstOrDefaultAsync(u => u.Id == (long)id); + var user = _context.UserRepository.GetById((long)id); if(user == null) { throw new Exception("User not found"); @@ -62,14 +64,16 @@ namespace DTOToEntity public async Task> GetByRole(int index, int count, string role) { - var users = _context.Users.Where(u => u.Role.Name == role).Skip(index * count).Take(count); - return new PageResponse(users.Select(u => u.ToDTO()), _context.Users.Count()); + var users = _context.UserRepository.GetItems(filter: u => u.Role != null && u.Role.Name == role, + index: index, + count: count); + return new PageResponse(users.Select(u => u.ToDTO()), _context.UserRepository.GetItems(0, 1000000000).Count()); } public async Task> Gets(int index, int count) { - IEnumerable users = await _context.Users.Skip(index * count).Take(count).ToListAsync(); - return new PageResponse( users.Select(u => u.ToDTO()),_context.Users.Count()); + IEnumerable users = _context.UserRepository.GetItems(index, count) ; + return new PageResponse( users.Select(u => u.ToDTO()),_context.UserRepository.GetItems(0, 1000000000).Count()); } public async Task Update(UserDTO user) @@ -78,7 +82,7 @@ namespace DTOToEntity { throw new Exception("User is null"); } - var existingUser = await _context.Users.FirstOrDefaultAsync(u => u.Id == user.Id); + var existingUser = _context.UserRepository.GetById(user.Id); if(existingUser == null) { throw new Exception("User not found"); diff --git a/Project/EntityFramework/DTOToEntity/VocabularyListService.cs b/Project/EntityFramework/DTOToEntity/VocabularyListService.cs index f9999b1..0f0bde6 100644 --- a/Project/EntityFramework/DTOToEntity/VocabularyListService.cs +++ b/Project/EntityFramework/DTOToEntity/VocabularyListService.cs @@ -1,4 +1,5 @@ -using DTO; +using DbContextLib; +using DTO; using Microsoft.EntityFrameworkCore; using StubbedContextLib; using System; @@ -11,7 +12,7 @@ namespace DTOToEntity { public class VocabularyListService : IVocabularyListService { - private StubbedContext _context = new StubbedContext(); + private UnitOfWork _context = new UnitOfWork(); public VocabularyListService() { @@ -19,24 +20,24 @@ namespace DTOToEntity public VocabularyListService(StubbedContext context) { - _context = context; + _context = new UnitOfWork(context); } public async Task Add(VocabularyListDTO group) { var groupEntity = group.ToEntity(); - _context.VocabularyLists.Add(groupEntity); + _context.VocabularyListRepository.Insert(groupEntity); await _context.SaveChangesAsync(); return groupEntity.ToDTO(); } public async Task AddGroupToVocabularyList(long groupId, long vocabId) { - var group = _context.Groups.Find(groupId); + var group = _context.GroupRepository.GetById(groupId); if (group == null) { throw new Exception("Group not found"); } - var vocab = _context.VocabularyLists.Find(vocabId); + var vocab = _context.VocabularyListRepository.GetById(vocabId); if (vocab == null) { throw new Exception("Vocabulary List not found"); @@ -49,10 +50,10 @@ namespace DTOToEntity public async Task Delete(object id) { - var group = await _context.VocabularyLists.FindAsync(id); + var group = _context.VocabularyListRepository.GetById((long)id); if (group != null) { - _context.VocabularyLists.Remove(group); + _context.VocabularyListRepository.Delete(group); await _context.SaveChangesAsync(); } else @@ -64,7 +65,7 @@ namespace DTOToEntity public async Task GetById(object id) { - var group = await _context.VocabularyLists.FindAsync(id); + var group = _context.VocabularyListRepository.GetById((long)id); if (group == null) { throw new Exception("Group not found"); @@ -74,15 +75,15 @@ namespace DTOToEntity public async Task> GetByUser(int index, int count, int user) { - var groups = _context.VocabularyLists.Where(g => g.UserId == user).Skip(index * count).Take(count); - return new PageResponse(groups.Select(g => g.ToDTO()), _context.VocabularyLists.Count()); + var groups = _context.VocabularyListRepository.GetItems(filter: u => u.Id == user, index, count); + return new PageResponse(groups.Select(g => g.ToDTO()), _context.VocabularyListRepository.GetItems(0,1000000000).Count()); } public async Task> Gets(int index, int count) { - var groups = await _context.VocabularyLists.Skip(index * count).Take(count).ToListAsync(); - return new PageResponse(groups.Select(g => g.ToDTO()), _context.VocabularyLists.Count()); + var groups = _context.VocabularyListRepository.GetItems(index, count); + return new PageResponse(groups.Select(g => g.ToDTO()), _context.VocabularyListRepository.GetItems(0, 1000000000).Count()); } public async Task Update(VocabularyListDTO group) @@ -92,9 +93,9 @@ namespace DTOToEntity { throw new Exception("Group Entity is null"); } - var res = _context.VocabularyLists.Update(groupEntity); + _context.VocabularyListRepository.Update(groupEntity); await _context.SaveChangesAsync(); - return res.Entity.ToDTO(); + return group; } } } diff --git a/Project/EntityFramework/DTOToEntity/VocabularyService.cs b/Project/EntityFramework/DTOToEntity/VocabularyService.cs index 7f1ac16..54f766f 100644 --- a/Project/EntityFramework/DTOToEntity/VocabularyService.cs +++ b/Project/EntityFramework/DTOToEntity/VocabularyService.cs @@ -1,4 +1,5 @@ -using DTO; +using DbContextLib; +using DTO; using Entities; using Microsoft.EntityFrameworkCore; using StubbedContextLib; @@ -12,13 +13,13 @@ namespace DTOToEntity { public class VocabularyService : IVocabularyService { - private readonly StubbedContext _context = new StubbedContext(); + private readonly UnitOfWork _context = new UnitOfWork(); public VocabularyService() { } public VocabularyService(StubbedContext context) { - _context = context; + _context = new UnitOfWork(context); } public async Task Add(VocabularyDTO vocabulary) { @@ -27,19 +28,19 @@ namespace DTOToEntity { throw new ArgumentNullException(); } - await _context.Vocabularys.AddAsync(vocabularyEntity); + _context.VocabularyRepository.Insert(vocabularyEntity); await _context.SaveChangesAsync(); return vocabularyEntity.ToDTO(); } public async Task AddTranslationToVocabulary(string vocabId, long translateId) { - var vocabulary = _context.Vocabularys.Find(vocabId); + var vocabulary = _context.VocabularyRepository.GetById(vocabId); if(vocabulary == null) { throw new Exception("Vocabulary not found"); } - var translate = _context.Translates.Find(translateId); + var translate = _context.TranslateRepository.GetById(translateId); if(translate == null) { throw new Exception("Translate not found"); @@ -51,19 +52,19 @@ namespace DTOToEntity public async Task Delete(object id) { - var vocabulary = await _context.Vocabularys.FirstOrDefaultAsync(v => v.word == (string)id); + var vocabulary = _context.VocabularyRepository.GetById((string)id); if(vocabulary == null) { throw new Exception("Vocabulary not found"); } - _context.Vocabularys.Remove(vocabulary); + _context.VocabularyRepository.Delete(vocabulary); await _context.SaveChangesAsync(); return vocabulary.ToDTO(); } public async Task GetById(object id) { - var vocabulary = await _context.Vocabularys.FirstOrDefaultAsync(v => v.word == (string)id); + var vocabulary = _context.VocabularyRepository.GetById((string)id); if(vocabulary == null) { throw new Exception("Vocabulary not found"); @@ -73,15 +74,15 @@ namespace DTOToEntity public async Task> GetByLangue(int index, int count, string langue) { - var vocabularies = _context.Vocabularys.Where(v => v.LangueName == langue).Skip(index * count).Take(count); - return new PageResponse(vocabularies.ToList().Select(v => v.ToDTO()), _context.Vocabularys.Count()); + var vocabularies = _context.VocabularyRepository.GetItems(filter: v => v.LangueName == langue, index, count); + return new PageResponse(vocabularies.ToList().Select(v => v.ToDTO()), _context.VocabularyRepository.GetItems(0,100000000).Count()); } public async Task> Gets(int index, int count) { - var vocabulary = await _context.Vocabularys.Skip(index * count).Take(count).ToListAsync(); - return new PageResponse(vocabulary.Select(v => v.ToDTO()), _context.Vocabularys.Count()); + var vocabulary = _context.VocabularyRepository.GetItems(index, count); + return new PageResponse(vocabulary.Select(v => v.ToDTO()), _context.VocabularyRepository.GetItems(0, 100000000).Count()); } public async Task Update(VocabularyDTO vocabulary) @@ -91,7 +92,7 @@ namespace DTOToEntity { throw new Exception("vocabulary not valid"); } - var VocabToUpdate = _context.Vocabularys.FirstOrDefault(v => v.word == (string)vocabularyEntity.word); + var VocabToUpdate = _context.VocabularyRepository.GetById(vocabulary.word); if(VocabToUpdate == null) { throw new Exception("vocabulary not found"); diff --git a/Project/EntityFramework/DbContext/SAEContext.cs b/Project/EntityFramework/DbContext/SAEContext.cs index 043b054..62d17c3 100644 --- a/Project/EntityFramework/DbContext/SAEContext.cs +++ b/Project/EntityFramework/DbContext/SAEContext.cs @@ -17,9 +17,6 @@ namespace DbContextLib - //permet de créer une base de donnée (fichier .db) ici en Sqlite avec le nom Db.Books.db - - public SAEContext(DbContextOptions options): base(options) { } diff --git a/Project/EntityFramework/StubbedContext/GenericRepository.cs b/Project/EntityFramework/StubbedContext/GenericRepository.cs new file mode 100644 index 0000000..d39ae65 --- /dev/null +++ b/Project/EntityFramework/StubbedContext/GenericRepository.cs @@ -0,0 +1,169 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Linq.Expressions; +using System.Text; +using System.Threading.Tasks; + +namespace DbContextLib +{ + public class GenericRepository where TEntity : class + { + private DbContext Context { get; set; } + private DbSet Set { get; set; } + + public GenericRepository(DbContext context) + { + Context = context; + Set = Context.Set(); + } + + public virtual TEntity? GetById(object id) + { + return Context.Set().Find(id); + } + + public virtual IEnumerable GetItems(Expression>? filter = null, + int index = 0, int count = 10, + params string[] includeProperties) + => GetItems(filter, null, index, count, includeProperties); + + public virtual IEnumerable GetItems(Func, IOrderedQueryable>? orderBy = null, + int index = 0, int count = 10, + params string[] includeProperties) + => GetItems(null, orderBy, index, count, includeProperties); + + public virtual IEnumerable GetItems(int index = 0, int count = 10, + params string[] includeProperties) + => GetItems(null, null, index, count, includeProperties); + + public virtual IEnumerable GetItems(Expression>? filter = null, + Func, IOrderedQueryable>? orderBy = null, + int index = 0, int count = 10, + params string[] includeProperties) + { + IQueryable query = Set; + if (filter != null) + { + query = query.Where(filter); + } + foreach (string includeProperty in includeProperties) + { + query = query.Include(includeProperty); + } + if (orderBy != null) + { + query = orderBy(query); + } + return query.Skip(index * count) + .Take(count) + .ToList(); + } + + public virtual void Insert(TEntity entity) + { + var primaryKeyProperty = Context.Model.FindEntityType(typeof(TEntity)).FindPrimaryKey().Properties.FirstOrDefault(); + if (primaryKeyProperty != null) + { + var parameter = Expression.Parameter(typeof(TEntity), "x"); + var property = Expression.Property(parameter, primaryKeyProperty.Name); + var idValue = Expression.Constant(primaryKeyProperty.GetGetter().GetClrValue(entity)); + var equal = Expression.Equal(property, idValue); + var lambda = Expression.Lambda>(equal, parameter); + + var existingEntity = Set.Local.FirstOrDefault(lambda.Compile()); + + if (existingEntity != null) + { + Set.Entry(existingEntity).Property("Count").CurrentValue = (int)Set.Entry(existingEntity).Property("Count").CurrentValue + 1; + } + else + { + Set.Add(entity); + } + } + else + { + throw new InvalidOperationException("Cannot find primary key property for entity type."); + } + } + + public virtual void Insert(params TEntity[] entities) + { + foreach (var entity in entities) + { + Insert(entity); + } + } + + public virtual void Delete(object id) + { + TEntity? entity = Set.Find(id); + if (entity == null) return; + Delete(entity); + } + + public virtual void Delete(TEntity entity) + { + var primaryKeyProperty = Context.Model.FindEntityType(typeof(TEntity)).FindPrimaryKey().Properties.FirstOrDefault(); + if (primaryKeyProperty != null) + { + var parameter = Expression.Parameter(typeof(TEntity), "x"); + var property = Expression.Property(parameter, primaryKeyProperty.Name); + var idValue = Expression.Constant(primaryKeyProperty.GetGetter().GetClrValue(entity)); + var equal = Expression.Equal(property, idValue); + var lambda = Expression.Lambda>(equal, parameter); + + var existingEntity = Set.Local.FirstOrDefault(lambda.Compile()); + + if (existingEntity == null) + { + if (Context.Entry(entity).State == EntityState.Detached) + { + Set.Attach(entity); + } + Set.Remove(entity); + } + else + { + Set.Remove(existingEntity); + } + } + else + { + throw new InvalidOperationException("Cannot find primary key property for entity type."); + } + } + + public virtual void Update(TEntity entity) + { + var primaryKeyProperty = Context.Model.FindEntityType(typeof(TEntity)).FindPrimaryKey().Properties.FirstOrDefault(); + if (primaryKeyProperty != null) + { + var parameter = Expression.Parameter(typeof(TEntity), "x"); + var property = Expression.Property(parameter, primaryKeyProperty.Name); + var idValue = Expression.Constant(primaryKeyProperty.GetGetter().GetClrValue(entity)); + var equal = Expression.Equal(property, idValue); + var lambda = Expression.Lambda>(equal, parameter); + + var existingEntity = Set.Local.FirstOrDefault(lambda.Compile()); + + if (existingEntity != null) + { + Context.Entry(existingEntity).CurrentValues.SetValues(entity); + } + else + { + Set.Attach(entity); + Context.Entry(entity).State = EntityState.Modified; + } + } + else + { + throw new InvalidOperationException("Cannot find primary key property for entity type."); + } + } + + } +} diff --git a/Project/EntityFramework/StubbedContext/Migrations/20240330163827_newMigs.Designer.cs b/Project/EntityFramework/StubbedContext/Migrations/20240331221744_newMigs.Designer.cs similarity index 99% rename from Project/EntityFramework/StubbedContext/Migrations/20240330163827_newMigs.Designer.cs rename to Project/EntityFramework/StubbedContext/Migrations/20240331221744_newMigs.Designer.cs index b550929..70330de 100644 --- a/Project/EntityFramework/StubbedContext/Migrations/20240330163827_newMigs.Designer.cs +++ b/Project/EntityFramework/StubbedContext/Migrations/20240331221744_newMigs.Designer.cs @@ -10,7 +10,7 @@ using StubbedContextLib; namespace StubbedContextLib.Migrations { [DbContext(typeof(StubbedContext))] - [Migration("20240330163827_newMigs")] + [Migration("20240331221744_newMigs")] partial class newMigs { /// diff --git a/Project/EntityFramework/StubbedContext/Migrations/20240330163827_newMigs.cs b/Project/EntityFramework/StubbedContext/Migrations/20240331221744_newMigs.cs similarity index 100% rename from Project/EntityFramework/StubbedContext/Migrations/20240330163827_newMigs.cs rename to Project/EntityFramework/StubbedContext/Migrations/20240331221744_newMigs.cs diff --git a/Project/EntityFramework/StubbedContext/UnitOfWork.cs b/Project/EntityFramework/StubbedContext/UnitOfWork.cs new file mode 100644 index 0000000..0bf1ffd --- /dev/null +++ b/Project/EntityFramework/StubbedContext/UnitOfWork.cs @@ -0,0 +1,197 @@ +using Entities; +using Microsoft.EntityFrameworkCore; +using StubbedContextLib; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DbContextLib +{ + public class UnitOfWork : IDisposable + { + private bool disposed = false; + + private GenericRepository? groupRepository; + + private GenericRepository? langueRepository; + + private GenericRepository? roleRepository; + + private GenericRepository? translateRepository; + + private GenericRepository? userRepository; + + private GenericRepository? vocabularyRepository; + + private GenericRepository? vocabularyListRepository; + + + + + private SAEContext _context { get; set; } + public UnitOfWork(SAEContext context) { + _context = context; + + _context.Database.EnsureCreated(); + } + + public UnitOfWork(StubbedContext context) + { + _context = context; + + _context.Database.EnsureCreated(); + } + + public UnitOfWork(DbContextOptions options) +: this(new StubbedContext(options)) + { + } + + + public UnitOfWork() + : this(new StubbedContext()) + { + } + + protected virtual void Dispose(bool disposing) + { + if (!this.disposed && disposing) + { + _context.Dispose(); + } + this.disposed = true; + } + + public void Dispose() + { + Dispose(true); + GC.SuppressFinalize(this); + } + + public GenericRepository GroupRepository + { + get + { + if (groupRepository == null) + { + groupRepository = new GenericRepository(_context); + } + return groupRepository; + } + } + + public GenericRepository LangueRepository + { + get + { + if (langueRepository == null) + { + langueRepository = new GenericRepository(_context); + } + return langueRepository; + } + } + + public GenericRepository RoleRepository + { + get + { + if (roleRepository == null) + { + roleRepository = new GenericRepository(_context); + } + return roleRepository; + } + } + + public GenericRepository TranslateRepository + { + get + { + if (translateRepository == null) + { + translateRepository = new GenericRepository(_context); + } + return translateRepository; + } + } + + public GenericRepository UserRepository + { + get + { + if (userRepository == null) + { + userRepository = new GenericRepository(_context); + } + return userRepository; + } + } + + public GenericRepository VocabularyRepository + { + get + { + if (vocabularyRepository == null) + { + vocabularyRepository = new GenericRepository(_context); + } + return vocabularyRepository; + } + } + + public GenericRepository VocabularyListRepository + { + get + { + if (vocabularyListRepository == null) + { + vocabularyListRepository = new GenericRepository(_context); + } + return vocabularyListRepository; + } + } + + public async Task SaveChangesAsync() + { + int result = 0; + try + { + result = await _context.SaveChangesAsync(); + } + catch + { + RejectChanges(); + return -1; + } + foreach (var entity in _context.ChangeTracker.Entries() + .Where(e => e.State != EntityState.Detached)) + { + entity.State = EntityState.Detached; + } + return result; + } + + public void RejectChanges() + { + foreach (var entry in _context.ChangeTracker.Entries() + .Where(e => e.State != EntityState.Unchanged)) + { + switch (entry.State) + { + case EntityState.Added: + entry.State = EntityState.Detached; + break; + case EntityState.Modified: + case EntityState.Deleted: + entry.Reload(); + break; + } + } + } + + + } +} diff --git a/Project/EntityFramework/TU/GroupTU.cs b/Project/EntityFramework/TU/GroupTU.cs index 6670432..4897429 100644 --- a/Project/EntityFramework/TU/GroupTU.cs +++ b/Project/EntityFramework/TU/GroupTU.cs @@ -289,34 +289,8 @@ namespace TU } } - [TestMethod] - public async Task TestAddVocabListToGroup() - { - 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 GroupController(new GroupService(context), mockLogger.Object); - - var result = await controller.AddVocabularyListToGroup(1, 1); - Assert.IsNotNull(result.Value); - Assert.AreEqual(1, result.Value.Id); - - var test = await context.Groups.FirstOrDefaultAsync(g => g.Id == 1); - var testVocab = await context.VocabularyLists.FirstOrDefaultAsync(g => g.Id == 1); - Assert.IsNotNull(test); - Assert.IsNotNull(testVocab); - Assert.AreEqual(test.GroupVocabularyList.First(), testVocab); - } - } } } \ No newline at end of file diff --git a/Project/EntityFramework/TU/RoleTU.cs b/Project/EntityFramework/TU/RoleTU.cs index 2a94e01..417ea9c 100644 --- a/Project/EntityFramework/TU/RoleTU.cs +++ b/Project/EntityFramework/TU/RoleTU.cs @@ -31,7 +31,7 @@ namespace TU var controller = new RoleController(new RoleService(context), mockLogger.Object); - var newRole = new RoleDTO { Id = 100, Name = "test" }; + var newRole = new RoleDTO { Id = 1, Name = "test" }; var result = await controller.AddRole(newRole); @@ -39,7 +39,7 @@ namespace TU //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); - var test = await context.Roles.FirstOrDefaultAsync(r => r.Id == 4); + var test = context.Roles.Find((long)4); Assert.IsNotNull(test); } } @@ -60,12 +60,11 @@ namespace TU var mockLogger = new Mock>(); var controller = new RoleController(new RoleService(context), mockLogger.Object); - - var result = await controller.DeleteRole(3); + var result = await controller.DeleteRole(1); Assert.IsNotNull(result.Value); - Assert.AreEqual(3, result.Value.Id); - Assert.AreEqual("Student", result.Value.Name); - var test = await context.Roles.FirstOrDefaultAsync(r => r.Id == 3); + Assert.AreEqual(1, result.Value.Id); + Assert.AreEqual("Admin", result.Value.Name); + var test = context.Roles.Find((long)1); Assert.IsNull(test); } } @@ -86,10 +85,11 @@ namespace TU var controller = new RoleController(new RoleService(context), mockLogger.Object); - var result = await controller.GetRole(3); + + var result = await controller.GetRole((long)1); Assert.IsNotNull(result.Value); - Assert.AreEqual(3, result.Value.Id); - Assert.AreEqual("Student", result.Value.Name); + Assert.AreEqual(1, result.Value.Id); + Assert.AreEqual("Admin", result.Value.Name); } } diff --git a/Project/EntityFramework/TU/TU_API.csproj b/Project/EntityFramework/TU/TU_API.csproj index 5997b08..7811f42 100644 --- a/Project/EntityFramework/TU/TU_API.csproj +++ b/Project/EntityFramework/TU/TU_API.csproj @@ -21,7 +21,6 @@ - diff --git a/Project/EntityFramework/TU/TranslateTU.cs b/Project/EntityFramework/TU/TranslateTU.cs index 7783bdf..ca2845f 100644 --- a/Project/EntityFramework/TU/TranslateTU.cs +++ b/Project/EntityFramework/TU/TranslateTU.cs @@ -64,7 +64,6 @@ namespace TU var controller = new TranslateController(new TranslateService(context), mockLogger.Object); - var result = await controller.GetTranslate(1); Assert.IsNotNull(result.Value); Assert.AreEqual(1, result.Value.Id); @@ -148,6 +147,5 @@ namespace TU } } - } } diff --git a/Project/EntityFramework/TestConsole/Program.cs b/Project/EntityFramework/TestConsole/Program.cs index d58ca71..56ed813 100644 --- a/Project/EntityFramework/TestConsole/Program.cs +++ b/Project/EntityFramework/TestConsole/Program.cs @@ -15,21 +15,10 @@ var options = new DbContextOptionsBuilder() .UseSqlite(connection) .Options; Console.WriteLine("Test Users : \n\n"); -using (var context = new StubbedContext(options)) +Console.WriteLine("\n\nTest Users : \n\n"); +using (var uow = new UnitOfWork(options)) { - context.Database.EnsureCreated(); - - var users = context.Users.ToList(); - Console.WriteLine("Users: " + users.Count); - var roles = context.Roles.ToList(); - Console.WriteLine("Roles: " + roles.Count); - - Console.WriteLine("\ntest show 5 first Users : \n"); - foreach (var user in context.Users.Take(5)) - { - Console.WriteLine(user.toString()); - } - + var users = uow.UserRepository; var user1 = new UserEntity { Name = "name4", @@ -43,101 +32,110 @@ using (var context = new StubbedContext(options)) image = "image4", }; - context.Users.Add(user1); - context.SaveChanges(); - - Console.WriteLine("\ntest ajout (1 user suplementaire normalement) \n"); - foreach (var user in context.Users.Take(5)) + users.Insert(user1); + await uow.SaveChangesAsync(); + Console.WriteLine("\ntest ajout (1 user supplémentaire normalement) \n"); + foreach (var user in users.GetItems(0, 5)) { Console.WriteLine(user.toString()); } - user1.Name = "updated"; - context.Users.Update(user1); - context.SaveChanges(); - Console.WriteLine("\ntest update (le nom doit etre 'updated') \n"); + users.Update(user1); + await uow.SaveChangesAsync(); + Console.WriteLine("\ntest update (le nom doit être 'updated') \n"); Console.WriteLine(user1.toString()); - context.Users.Remove(user1); - context.SaveChanges(); + users.Delete(user1); + await uow.SaveChangesAsync(); Console.WriteLine("\ntest suppression\n"); - foreach (var user in context.Users.Take(5)) + foreach (var user in users.GetItems(0, 5)) { Console.WriteLine(user.toString()); } +} - Console.WriteLine("\n\nTest VocsGroups : \n\n"); - var groups = context.Groups.ToList(); - Console.WriteLine("VocsGroups: " + groups.Count); - Console.WriteLine("\ntest show 5 first VocsGroups : \n"); - foreach (var group in context.Groups.Take(5)) +Console.WriteLine("\n\nTest VocsGroups : \n\n"); +using (var uow = new UnitOfWork(options)) +{ + var groups = uow.GroupRepository; + var users = uow.UserRepository; + var user1 = new UserEntity { - Console.WriteLine(group.toString()); - } - + Name = "name4", + UserName = "username4", + NickName = "nickname4", + ExtraTime = true, + GroupId = 1, + Password = "12344", + Email = "", + RoleId = 3, + image = "image4", + }; var group1 = new GroupEntity { - Id = 4, Num = 4, year = 4, sector = "sector4", - Users = [user1], - + Users = new List { user1 } }; - context.Groups.Add(group1); - context.SaveChanges(); + groups.Insert(group1); + await uow.SaveChangesAsync(); - Console.WriteLine("\ntest ajout (1 group suplementaire normalement) \n"); - foreach (var group in context.Groups.Take(5)) + Console.WriteLine("\ntest ajout (1 group supplémentaire normalement) \n"); + foreach (var group in groups.GetItems(0, 5)) { Console.WriteLine(group.toString()); } + group1.Num = 5; group1.sector = "updated"; - context.Update(group1); - context.SaveChanges(); - Console.WriteLine("\ntest update (le nom doit etre 'updated' et le Num = 5) \n"); + groups.Update(group1); + await uow.SaveChangesAsync(); + Console.WriteLine("\ntest update (le nom doit être 'updated' et le Num = 5) \n"); Console.WriteLine(group1.toString()); Console.WriteLine("\n test utilisateur du groupe normalement le user : updated "); - foreach (var group in context.Groups.Take(5)) + foreach (var group in groups.GetItems(0, 5)) { - foreach (var user in group.Users.Take(5)) + foreach (var user in users.GetItems(0, 5)) { Console.WriteLine(user.toString()); } } - context.Remove(group1); - context.SaveChanges(); + groups.Delete(group1); + await uow.SaveChangesAsync(); Console.WriteLine("\ntest suppression\n"); - foreach (var group in context.Groups.Take(5)) + foreach (var group in groups.GetItems(0, 5)) { Console.WriteLine(group.toString()); } +} - Console.WriteLine("\n\nTest Langue : \n\n"); +Console.WriteLine("\n\nTest Langue : \n\n"); +using (var uow = new UnitOfWork(options)) +{ + var langues = uow.LangueRepository; - context.Database.EnsureCreated(); - var langues = context.Langues.ToList(); - Console.WriteLine("Langues: " + langues.Count); Console.WriteLine("\ntest show 5 first Langues : \n"); - foreach (var langue in context.Langues.Take(5)) + foreach (var langue in langues.GetItems(0, 5)) { Console.WriteLine(langue.name); } + Console.WriteLine("\n ajout Langue (normalement chinese\n"); var langue1 = new LangueEntity { name = "Chinese" }; - context.Langues.Add(langue1); - context.SaveChanges(); - foreach (var langue in context.Langues.Take(5)) + langues.Insert(langue1); + await uow.SaveChangesAsync(); + foreach (var langue in langues.GetItems(0, 5)) { Console.WriteLine(langue.name); } + Console.WriteLine("\n test des liens langues <=> vocabulaires (resultat attendu : 'word1 Chinese'\n"); var vocabularyList1 = new VocabularyEntity { @@ -145,205 +143,200 @@ using (var context = new StubbedContext(options)) LangueName = "Chinese", Langue = langue1 }; - context.Vocabularys.Add(vocabularyList1); - foreach (var langue in context.Langues.Take(5)) + uow.VocabularyRepository.Insert(vocabularyList1); + await uow.SaveChangesAsync(); + foreach (var langue in langues.GetItems(0, 5)) { - foreach (var vocabularyList in langue.vocabularys.Take(5)) + foreach (var vocabularyList in langue.vocabularys) { Console.WriteLine(vocabularyList.toString()); - } } - context.Langues.Remove(langue1); - context.SaveChanges(); + langues.Delete(langue1); + await uow.SaveChangesAsync(); Console.WriteLine("\ntest suppression (il n'y a normalement plus la langue 'Chinese'\n"); - foreach (var langue in context.Langues.Take(5)) + foreach (var langue in langues.GetItems(0, 5)) { Console.WriteLine(langue.name); } - Console.WriteLine("\n\nTest Roles : \n\n"); - context.Database.EnsureCreated(); - var roles2 = context.Roles.ToList(); - Console.WriteLine("Roles: " + roles2.Count); +} + +Console.WriteLine("\n\nTest Roles : \n\n"); +using (var uow = new UnitOfWork(options)) +{ + var roles = uow.RoleRepository; + Console.WriteLine("\ntest show 5 first Roles : \n"); - foreach (var role in context.Roles.Take(5)) + foreach (var role in roles.GetItems(0, 5)) { Console.WriteLine(role.toString()); } + var role1 = new RoleEntity { - Id = 4, Name = "Role4" }; - context.Roles.Add(role1); - context.SaveChanges(); + roles.Insert(role1); + await uow.SaveChangesAsync(); Console.WriteLine("\n ajout Role (normalement Role4\n"); - foreach (var role in context.Roles.Take(5)) + foreach (var role in roles.GetItems(0, 5)) { Console.WriteLine(role.toString()); } + role1.Name = "updated"; - context.Roles.Update(role1); - context.SaveChanges(); - Console.WriteLine("\ntest update (le nom doit etre 'updated') \n"); + roles.Update(role1); + await uow.SaveChangesAsync(); + Console.WriteLine("\ntest update (le nom doit être 'updated') \n"); Console.WriteLine(role1.toString()); - Console.WriteLine("\n test utilisateur du role normalement"); - foreach (var role in context.Roles.Take(5)) + foreach (var role in roles.GetItems(0, 5)) { - foreach (var user in role.Users.Take(5)) + foreach (var user in role.Users) { Console.WriteLine(user.toString()); } } - context.Roles.Remove(role1); - context.SaveChanges(); + roles.Delete(role1); + await uow.SaveChangesAsync(); Console.WriteLine("\ntest suppression (il n'y a normalement plus le role 'Role4'\n"); - foreach (var role in context.Roles.Take(5)) + foreach (var role in roles.GetItems(0, 5)) { Console.WriteLine(role.toString()); } - - Console.WriteLine("\n\nTest Translate : \n\n"); - context.Database.EnsureCreated(); - var translates = context.Translates.ToList(); - Console.WriteLine("Translates: " + translates.Count); +} + +Console.WriteLine("\n\nTest Translate : \n\n"); +using (var uow = new UnitOfWork(options)) +{ + var translates = uow.TranslateRepository; + Console.WriteLine("\ntest show 5 first Translates : \n"); - foreach (var translate in context.Translates.Take(5)) + foreach (var translate in translates.GetItems(0, 5)) { Console.WriteLine(translate.toString()); } + var translate1 = new TranslateEntity { - Id = 4, WordsId = "Bonjour", VocabularyListVocId = 1 -}; - context.Translates.Add(translate1); - context.SaveChanges(); + }; + translates.Insert(translate1); + await uow.SaveChangesAsync(); Console.WriteLine("\n ajout Translate (normalement word4\n"); - foreach (var translate in context.Translates.Take(5)) + foreach (var translate in translates.GetItems(0, 5)) { Console.WriteLine(translate.toString()); } + var translate2 = new VocabularyEntity { word = "word4", LangueName = "English", }; - context.Vocabularys.Add(translate2); - context.SaveChanges(); + uow.VocabularyRepository.Insert(translate2); + await uow.SaveChangesAsync(); translate1.WordsId = "word4"; - context.Translates.Update(translate1); - context.SaveChanges(); - Console.WriteLine("\ntest update (le mot doit etre 'updated') \n"); + translates.Update(translate1); + await uow.SaveChangesAsync(); + Console.WriteLine("\ntest update (le mot doit être 'updated') \n"); Console.WriteLine(translate1.toString()); - context.Translates.Remove(translate1); - context.SaveChanges(); + translates.Delete(translate1); + await uow.SaveChangesAsync(); Console.WriteLine("\ntest suppression (il n'y a normalement plus le mot 'word4'\n"); - foreach (var translate in context.Translates.Take(5)) + foreach (var translate in translates.GetItems(0, 5)) { Console.WriteLine(translate.toString()); } - Console.WriteLine("\n\nTest Vocabulary : \n\n"); - context.Database.EnsureCreated(); +} + +Console.WriteLine("\n\nTest Vocabulary : \n\n"); +using (var uow = new UnitOfWork(options)) +{ + var vocabularies = uow.VocabularyRepository; - var vocabularys = context.Vocabularys.ToList(); - Console.WriteLine("Vocabularys: " + vocabularys.Count); Console.WriteLine("\ntest show 5 first Vocabularys : \n"); - foreach (var vocabulary in context.Vocabularys.Take(5)) + foreach (var vocabulary in vocabularies.GetItems(0, 5)) { Console.WriteLine(vocabulary.toString()); } - var vocabulary1 = new VocabularyEntity - { + + var vocabulary1 = new VocabularyEntity + { word = "NewWord", LangueName = "English" }; - context.Vocabularys.Add(vocabulary1); - context.SaveChanges(); + vocabularies.Insert(vocabulary1); + await uow.SaveChangesAsync(); Console.WriteLine("\n ajout Vocabulary (normalement NewWord) \n"); - foreach (var vocabulary in context.Vocabularys.Take(5)) + foreach (var vocabulary in vocabularies.GetItems(0, 5)) { Console.WriteLine(vocabulary.toString()); } + vocabulary1.LangueName = "French"; - context.Vocabularys.Update(vocabulary1); - context.SaveChanges(); - Console.WriteLine("\ntest update (la langue doit etre 'French') \n"); + vocabularies.Update(vocabulary1); + await uow.SaveChangesAsync(); + Console.WriteLine("\ntest update (la langue doit être 'French') \n"); Console.WriteLine(vocabulary1.toString()); - context.Vocabularys.Remove(vocabulary1); - context.SaveChanges(); + vocabularies.Delete(vocabulary1); + await uow.SaveChangesAsync(); Console.WriteLine("\ntest suppression (il n'y a normalement plus le mot 'NewWord'\n"); - foreach (var vocabulary in context.Vocabularys.Take(5)) + foreach (var vocabulary in vocabularies.GetItems(0, 5)) { Console.WriteLine(vocabulary.toString()); } +} + +Console.WriteLine("\n\nTest GroupVocabularyList : \n\n"); +using (var uow = new UnitOfWork(options)) +{ + var vocabularyLists = uow.VocabularyListRepository; - Console.WriteLine("\n\nTest GroupVocabularyList : \n\n"); - context.Database.EnsureCreated(); - var vocabularyLists = context.VocabularyLists.ToList(); - Console.WriteLine("VocabularyLists: " + vocabularyLists.Count); Console.WriteLine("\ntest show 5 first VocabularyLists : \n"); - foreach (var vocabularyList in context.VocabularyLists.Take(5)) + foreach (var vocabularyList in vocabularyLists.GetItems(0, 5)) { Console.WriteLine(vocabularyList.toString()); } + var vocabularyList2 = new VocabularyListEntity { - Id = 4, Name = "name4", Image = "image4", UserId = 3 }; - context.VocabularyLists.Add(vocabularyList2); - context.SaveChanges(); + vocabularyLists.Insert(vocabularyList2); + await uow.SaveChangesAsync(); Console.WriteLine("\n ajout GroupVocabularyList (normalement name4) \n"); - foreach (var vocabularyList in context.VocabularyLists.Take(5)) + foreach (var vocabularyList in vocabularyLists.GetItems(0, 5)) { Console.WriteLine(vocabularyList.toString()); } + vocabularyList2.Name = "updated"; - context.VocabularyLists.Update(vocabularyList2); - context.SaveChanges(); - Console.WriteLine("\ntest update (le nom doit etre 'updated') \n"); + vocabularyLists.Update(vocabularyList2); + await uow.SaveChangesAsync(); + Console.WriteLine("\ntest update (le nom doit être 'updated') \n"); Console.WriteLine(vocabularyList2.toString()); + Console.WriteLine("\n test affichage des owner (user) des listes normalement"); - foreach (var vocabularyList in context.VocabularyLists.Take(5)) + foreach (var vocabularyList in vocabularyLists.GetItems(0, 5)) { - - Console.WriteLine(vocabularyList.User.toString()); + if(vocabularyList.User != null) + Console.WriteLine(vocabularyList.User.toString()); } - - context.VocabularyLists.Remove(vocabularyList2); - context.SaveChanges(); + + vocabularyLists.Delete(vocabularyList2); + await uow.SaveChangesAsync(); Console.WriteLine("\ntest suppression (il n'y a normalement plus la liste 'name4'\n"); - foreach (var vocabularyList in context.VocabularyLists.Take(5)) + foreach (var vocabularyList in vocabularyLists.GetItems(0, 5)) { Console.WriteLine(vocabularyList.toString()); } - - Console.WriteLine("\n\nTest Many to Many (si pas d'erreur alors test reussi) : \n\n"); - - context.Database.EnsureCreated(); - var groupVoc = new GroupEntity { Id = 2, Num = 1, year = 1, sector = "sector1" }; - VocabularyListEntity VocListGroup = new VocabularyListEntity { Id = 2, Name = "name1", Image = "image1", UserId = 1 }; - context.AddRange(groupVoc, VocListGroup); - context.SaveChanges(); - - var transVoc = new TranslateEntity { Id = 2, WordsId = "1", VocabularyListVocId = 1 }; - var VocTrans = new VocabularyEntity { word = "Test", LangueName = "French" }; - context.AddRange(transVoc, VocTrans); - context.SaveChanges(); - - - - - - +} -} \ No newline at end of file diff --git a/README.md b/README.md index d62426a..ad427fe 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,8 @@ -# SAE 2A Anglais +# SAE 2A Anglais Ce projet vise à faciliter l'apprentissage de l'anglais dans le cadre d'études supérieures grâce à un site internet et une application mobile. + ## Exécuter localement Clonez le projet @@ -18,6 +19,16 @@ Allez dans le répertoire Suivez les différentes instructions suivant la partie que vous souhaitez lancer. +### EF et WebAPI + +Bonjour cette partie a été réalisé par Patrick Brugière seul. + +Le .sln se trouve dans le repertoir Project/EntityFramework + +Il est normalement précisé dans le nom de des tests à quelles parties ils appartiennent + +Pour utiliser un jeton JWT dans l'en-tête "Authorize", il suffit d'entrer simplement sa valeur, sans mettre ni guillemets ni le mot "Bearer" avant. + ### PHP Changez de branche