ajout du AnswerEntityManager
continuous-integration/drone/push Build is passing Details

API
Damien NORTIER 1 year ago
parent b3f2c4de58
commit 70f7d6d316

@ -1,10 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Entities;
using Microsoft.EntityFrameworkCore;
namespace DbConnectionLibrairie
{
public class MyDbContext : DbContext
{
public DbSet<AnswerEntity> Answers { get; set; }
public MyDbContext()
{ }
public MyDbContext(DbContextOptions<MyDbContext> contextOptions) :
base(contextOptions)
{ }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{

@ -1,14 +1,38 @@
namespace Entities
{
public class AnswerEntity
{
/// <summary>
/// define an entity of an answer for a Question with Mutiple Choice
/// properties :
/// Id : identifier in the database
/// Content : content of the answer
/// </summary>
long Id { get; set; }
string Content { get; set; } = null!;
}
}
namespace Entities
{
public class AnswerEntity
{
/// <summary>
/// define an entity of an answer for a Question with Mutiple Choice
/// properties :
/// Id : identifier in the database
/// Content : content of the answer
/// </summary>
public long Id { get; set; }
public string Content { get; set; } = null!;
/// <summary>
/// equality protocole
/// </summary>
/// <param name="obj">an object</param>
/// <returns>
/// 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
/// </returns>
public override bool Equals(object? obj)
{
return obj != null && obj is AnswerEntity other
&& other.Content == Content;
}
/// <summary>
/// GetHashCode method
/// </summary>
/// <returns>base.GetHashCode</returns>
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}

@ -0,0 +1,60 @@
using DbConnectionLibrairie;
using Entities;
using ManagerInterfaces;
using Microsoft.EntityFrameworkCore;
using OrderCriterias;
namespace EntityManagers
{
public class AnswerEntityManager : IAnswerManager<AnswerEntity>
{
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<AnswerEntity> 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;
}
}
}

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DbConnectionLibrairie\DbConnectionLibrairie.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\ManagerInterfaces\ManagerInterfaces.csproj" />
<ProjectReference Include="..\OrderCriterias\OrderCriterias.csproj" />
</ItemGroup>
</Project>

@ -1,7 +1,66 @@
namespace ManagerInterfaces
using OrderCriterias;
namespace ManagerInterfaces
{
/// <summary>
/// All methods to handle answers
/// </summary>
/// <typeparam name="T">a DTO or Entity type answer</typeparam>
public interface IAnswerManager<T>
{
public T getAnswers(int nb, int count);
/// <summary>
/// get the number of T element
/// </summary>
/// <returns>the number of T element</returns>
abstract int getNbElement();
/// <summary>
/// get a part of all answers
/// </summary>
/// <param name="nb">the actual page</param>
/// <param name="count">number of T element in a page</param>
/// <param name="orderCriteria">the order criteria</param>
/// <returns>
/// all T element of the database for
/// this page (or null if (nb-1)*count
/// is outside boundaries (0, getNbElement()-1)
/// </returns>
public IEnumerable<T>? getAnswers(int nb, int count, AnswerOrderCriteria orderCriteria = AnswerOrderCriteria.ById);
/// <summary>
/// get a T element with an id
/// </summary>
/// <param name="id">the id of the T element</param>
/// <returns>
/// the T element that corresponde to
/// the id or null if there isn't any
/// </returns>
public T? getAnswer(long id);
/// <summary>
/// modified a T element with an id
/// </summary>
/// <param name="id">the id of the T element</param>
/// <param name="answer">an answer that contains all modified properties</param>
/// <returns>
/// the T element (modified) that corresponde
/// to the id or null if there isn't any
/// </returns>
public T? modifierAnswer(long id, T answer);
/// <summary>
/// delete a T element with an id
/// </summary>
/// <param name="id">the id of the T element</param>
/// <returns>
/// the T element deleted that corresponde
/// to the id or null if there isn't any
/// </returns>
public T? supprimerAnswer(long id);
/// <summary>
/// add a T element
/// </summary>
/// <param name="answer">the answer to add</param>
/// <returns>
/// the T element (modified) that corresponde
/// to the id or null if there isn't any
/// </returns>
public T ajouterAnswer(T answer);
}
}

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\OrderCriterias\OrderCriterias.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OrderCriterias
{
/// <summary>
/// answer order criteria
/// </summary>
public enum AnswerOrderCriteria
{
ById = 0,
ByContent = 1
}
}

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -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

Loading…
Cancel
Save