diff --git a/WebApi/DbConnectionLibrairie/MyDbContext.cs b/WebApi/DbConnectionLibrairie/MyDbContext.cs index 81a5a21..5de4c45 100644 --- a/WebApi/DbConnectionLibrairie/MyDbContext.cs +++ b/WebApi/DbConnectionLibrairie/MyDbContext.cs @@ -1,10 +1,17 @@ -using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Options; +using Entities; +using Microsoft.EntityFrameworkCore; namespace DbConnectionLibrairie { public class MyDbContext : DbContext { + public DbSet Answers { get; set; } + public MyDbContext() + { } + + public MyDbContext(DbContextOptions contextOptions) : + base(contextOptions) + { } protected override void OnConfiguring(DbContextOptionsBuilder options) { diff --git a/WebApi/Entities/AnswerEntity.cs b/WebApi/Entities/AnswerEntity.cs index 81f44e5..9fbcc66 100644 --- a/WebApi/Entities/AnswerEntity.cs +++ b/WebApi/Entities/AnswerEntity.cs @@ -1,14 +1,38 @@ -namespace Entities -{ - public class AnswerEntity - { - /// - /// define an entity of an answer for a Question with Mutiple Choice - /// properties : - /// Id : identifier in the database - /// Content : content of the answer - /// - long Id { get; set; } - string Content { get; set; } = null!; - } -} +namespace Entities +{ + public class AnswerEntity + { + /// + /// define an entity of an answer for a Question with Mutiple Choice + /// properties : + /// Id : identifier in the database + /// Content : content of the answer + /// + public long Id { get; set; } + public string Content { get; set; } = null!; + + /// + /// equality protocole + /// + /// an object + /// + /// true if the object is an AnswerEntity + /// and the two contents are equals + /// (we don't care about the id because + /// he's set by the database + /// + public override bool Equals(object? obj) + { + return obj != null && obj is AnswerEntity other + && other.Content == Content; + } + /// + /// GetHashCode method + /// + /// base.GetHashCode + public override int GetHashCode() + { + return base.GetHashCode(); + } + } +} diff --git a/WebApi/EntityManagers/AnswerEntityManager.cs b/WebApi/EntityManagers/AnswerEntityManager.cs new file mode 100644 index 0000000..119f676 --- /dev/null +++ b/WebApi/EntityManagers/AnswerEntityManager.cs @@ -0,0 +1,60 @@ +using DbConnectionLibrairie; +using Entities; +using ManagerInterfaces; +using Microsoft.EntityFrameworkCore; +using OrderCriterias; + +namespace EntityManagers +{ + public class AnswerEntityManager : IAnswerManager + { + private MyDbContext dbContext; + + public int getNbElement() + { + return dbContext.Answers.CountAsync().Result; + } + + public AnswerEntityManager(MyDbContext dbContext) + { + this.dbContext = dbContext; + } + + public AnswerEntity ajouterAnswer(AnswerEntity answer) + { + var tmp = dbContext.Answers.Where(a => a.Equals(answer)).FirstOrDefaultAsync().Result; + if (tmp != null) return tmp; + dbContext.Answers.Add(answer); + dbContext.SaveChangesAsync(); + return dbContext.Answers.Where(a => a.Equals(answer)).FirstAsync().Result; + } + + public AnswerEntity? getAnswer(long id) + { + return dbContext.Answers.Where(a => a.Id == id).FirstOrDefaultAsync().Result; + } + + public IEnumerable getAnswers(int nb, int count, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById) + { + return dbContext.Answers.OrderBy(a => a.Id).Skip((nb - 1) * count).Take(count).ToListAsync().Result; + } + + public AnswerEntity? modifierAnswer(long id, AnswerEntity answer) + { + var tmp = dbContext.Answers.Where(a => a.Id == id).FirstOrDefaultAsync().Result; + if (tmp == null) return null; + tmp.Content = answer.Content; + dbContext.SaveChangesAsync(); + return tmp; + } + + public AnswerEntity? supprimerAnswer(long id) + { + var tmp = dbContext.Answers.Where(a => a.Id == id).FirstOrDefaultAsync().Result; + if (tmp == null) return null; + dbContext.Answers.Remove(tmp); + dbContext.SaveChangesAsync(); + return tmp; + } + } +} \ No newline at end of file diff --git a/WebApi/EntityManagers/EntityManagers.csproj b/WebApi/EntityManagers/EntityManagers.csproj new file mode 100644 index 0000000..9418224 --- /dev/null +++ b/WebApi/EntityManagers/EntityManagers.csproj @@ -0,0 +1,16 @@ + + + + net8.0 + enable + enable + + + + + + + + + + diff --git a/WebApi/ManagerInterfaces/IAnswerManager.cs b/WebApi/ManagerInterfaces/IAnswerManager.cs index 0f9610f..bf5cc69 100644 --- a/WebApi/ManagerInterfaces/IAnswerManager.cs +++ b/WebApi/ManagerInterfaces/IAnswerManager.cs @@ -1,7 +1,66 @@ -namespace ManagerInterfaces +using OrderCriterias; + +namespace ManagerInterfaces { + /// + /// All methods to handle answers + /// + /// a DTO or Entity type answer public interface IAnswerManager { - public T getAnswers(int nb, int count); + /// + /// get the number of T element + /// + /// the number of T element + abstract int getNbElement(); + /// + /// get a part of all answers + /// + /// the actual page + /// number of T element in a page + /// the order criteria + /// + /// all T element of the database for + /// this page (or null if (nb-1)*count + /// is outside boundaries (0, getNbElement()-1) + /// + public IEnumerable? getAnswers(int nb, int count, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById); + /// + /// get a T element with an id + /// + /// the id of the T element + /// + /// the T element that corresponde to + /// the id or null if there isn't any + /// + public T? getAnswer(long id); + /// + /// modified a T element with an id + /// + /// the id of the T element + /// an answer that contains all modified properties + /// + /// the T element (modified) that corresponde + /// to the id or null if there isn't any + /// + public T? modifierAnswer(long id, T answer); + /// + /// delete a T element with an id + /// + /// the id of the T element + /// + /// the T element deleted that corresponde + /// to the id or null if there isn't any + /// + public T? supprimerAnswer(long id); + /// + /// add a T element + /// + /// the answer to add + /// + /// the T element (modified) that corresponde + /// to the id or null if there isn't any + /// + public T ajouterAnswer(T answer); } } diff --git a/WebApi/ManagerInterfaces/ManagerInterfaces.csproj b/WebApi/ManagerInterfaces/ManagerInterfaces.csproj index fa71b7a..0ae86fb 100644 --- a/WebApi/ManagerInterfaces/ManagerInterfaces.csproj +++ b/WebApi/ManagerInterfaces/ManagerInterfaces.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/WebApi/OrderCriterias/AnswerOrderCriteria.cs b/WebApi/OrderCriterias/AnswerOrderCriteria.cs new file mode 100644 index 0000000..de76240 --- /dev/null +++ b/WebApi/OrderCriterias/AnswerOrderCriteria.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrderCriterias +{ + /// + /// answer order criteria + /// + public enum AnswerOrderCriteria + { + ById = 0, + ByContent = 1 + } +} diff --git a/WebApi/OrderCriterias/OrderCriterias.csproj b/WebApi/OrderCriterias/OrderCriterias.csproj new file mode 100644 index 0000000..bb23fb7 --- /dev/null +++ b/WebApi/OrderCriterias/OrderCriterias.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/WebApi/WebApi.sln b/WebApi/WebApi.sln index 9c0923d..c164e9f 100644 --- a/WebApi/WebApi.sln +++ b/WebApi/WebApi.sln @@ -1,55 +1,61 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.9.34607.119 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebApplication", "WebApplication\WebApplication.csproj", "{9F05B995-3079-4905-A9B1-7B3E8621ECC1}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "Entities\Entities.csproj", "{19A1AA55-0FDF-427F-97EA-157E816C93CE}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbConnectionLibrairie", "DbConnectionLibrairie\DbConnectionLibrairie.csproj", "{8224E470-B008-4738-88FD-7DEDCAA4AE44}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ManagerInterfaces", "ManagerInterfaces\ManagerInterfaces.csproj", "{35D8DDF1-93B1-4064-9205-BB745D300BCC}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTOs", "DTOs\DTOs.csproj", "{F911181D-6194-4CA9-A302-7A055652E5FA}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Model\Model.csproj", "{1C517096-268C-478C-BB98-5ACB8DD0692A}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Release|Any CPU.Build.0 = Release|Any CPU - {19A1AA55-0FDF-427F-97EA-157E816C93CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {19A1AA55-0FDF-427F-97EA-157E816C93CE}.Debug|Any CPU.Build.0 = Debug|Any CPU - {19A1AA55-0FDF-427F-97EA-157E816C93CE}.Release|Any CPU.ActiveCfg = Release|Any CPU - {19A1AA55-0FDF-427F-97EA-157E816C93CE}.Release|Any CPU.Build.0 = Release|Any CPU - {8224E470-B008-4738-88FD-7DEDCAA4AE44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {8224E470-B008-4738-88FD-7DEDCAA4AE44}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8224E470-B008-4738-88FD-7DEDCAA4AE44}.Release|Any CPU.ActiveCfg = Release|Any CPU - {8224E470-B008-4738-88FD-7DEDCAA4AE44}.Release|Any CPU.Build.0 = Release|Any CPU - {35D8DDF1-93B1-4064-9205-BB745D300BCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {35D8DDF1-93B1-4064-9205-BB745D300BCC}.Debug|Any CPU.Build.0 = Debug|Any CPU - {35D8DDF1-93B1-4064-9205-BB745D300BCC}.Release|Any CPU.ActiveCfg = Release|Any CPU - {35D8DDF1-93B1-4064-9205-BB745D300BCC}.Release|Any CPU.Build.0 = Release|Any CPU - {F911181D-6194-4CA9-A302-7A055652E5FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {F911181D-6194-4CA9-A302-7A055652E5FA}.Debug|Any CPU.Build.0 = Debug|Any CPU - {F911181D-6194-4CA9-A302-7A055652E5FA}.Release|Any CPU.ActiveCfg = Release|Any CPU - {F911181D-6194-4CA9-A302-7A055652E5FA}.Release|Any CPU.Build.0 = Release|Any CPU - {1C517096-268C-478C-BB98-5ACB8DD0692A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {1C517096-268C-478C-BB98-5ACB8DD0692A}.Debug|Any CPU.Build.0 = Debug|Any CPU - {1C517096-268C-478C-BB98-5ACB8DD0692A}.Release|Any CPU.ActiveCfg = Release|Any CPU - {1C517096-268C-478C-BB98-5ACB8DD0692A}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {30D8C710-2DC7-401D-AC62-AB63591468C8} - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.9.34607.119 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebApplication", "WebApplication\WebApplication.csproj", "{9F05B995-3079-4905-A9B1-7B3E8621ECC1}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Entities", "Entities\Entities.csproj", "{19A1AA55-0FDF-427F-97EA-157E816C93CE}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DbConnectionLibrairie", "DbConnectionLibrairie\DbConnectionLibrairie.csproj", "{8224E470-B008-4738-88FD-7DEDCAA4AE44}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ManagerInterfaces", "ManagerInterfaces\ManagerInterfaces.csproj", "{35D8DDF1-93B1-4064-9205-BB745D300BCC}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTOs", "DTOs\DTOs.csproj", "{F911181D-6194-4CA9-A302-7A055652E5FA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityManagers", "EntityManagers\EntityManagers.csproj", "{FAAF96CF-33DD-406A-B42D-E5768D3C46BB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OrderCriterias", "OrderCriterias\OrderCriterias.csproj", "{EE565F87-6811-46C8-A03B-6550D692873A}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9F05B995-3079-4905-A9B1-7B3E8621ECC1}.Release|Any CPU.Build.0 = Release|Any CPU + {19A1AA55-0FDF-427F-97EA-157E816C93CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {19A1AA55-0FDF-427F-97EA-157E816C93CE}.Debug|Any CPU.Build.0 = Debug|Any CPU + {19A1AA55-0FDF-427F-97EA-157E816C93CE}.Release|Any CPU.ActiveCfg = Release|Any CPU + {19A1AA55-0FDF-427F-97EA-157E816C93CE}.Release|Any CPU.Build.0 = Release|Any CPU + {8224E470-B008-4738-88FD-7DEDCAA4AE44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8224E470-B008-4738-88FD-7DEDCAA4AE44}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8224E470-B008-4738-88FD-7DEDCAA4AE44}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8224E470-B008-4738-88FD-7DEDCAA4AE44}.Release|Any CPU.Build.0 = Release|Any CPU + {35D8DDF1-93B1-4064-9205-BB745D300BCC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {35D8DDF1-93B1-4064-9205-BB745D300BCC}.Debug|Any CPU.Build.0 = Debug|Any CPU + {35D8DDF1-93B1-4064-9205-BB745D300BCC}.Release|Any CPU.ActiveCfg = Release|Any CPU + {35D8DDF1-93B1-4064-9205-BB745D300BCC}.Release|Any CPU.Build.0 = Release|Any CPU + {F911181D-6194-4CA9-A302-7A055652E5FA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F911181D-6194-4CA9-A302-7A055652E5FA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F911181D-6194-4CA9-A302-7A055652E5FA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F911181D-6194-4CA9-A302-7A055652E5FA}.Release|Any CPU.Build.0 = Release|Any CPU + {FAAF96CF-33DD-406A-B42D-E5768D3C46BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {FAAF96CF-33DD-406A-B42D-E5768D3C46BB}.Debug|Any CPU.Build.0 = Debug|Any CPU + {FAAF96CF-33DD-406A-B42D-E5768D3C46BB}.Release|Any CPU.ActiveCfg = Release|Any CPU + {FAAF96CF-33DD-406A-B42D-E5768D3C46BB}.Release|Any CPU.Build.0 = Release|Any CPU + {EE565F87-6811-46C8-A03B-6550D692873A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EE565F87-6811-46C8-A03B-6550D692873A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EE565F87-6811-46C8-A03B-6550D692873A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EE565F87-6811-46C8-A03B-6550D692873A}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {30D8C710-2DC7-401D-AC62-AB63591468C8} + EndGlobalSection +EndGlobal