feat : création d'une question entity

API
Damien NORTIER 1 year ago
parent 1667bdd3c0
commit 075569cb16

@ -8,18 +8,18 @@
/// </summary>
public class AnswerDto
{
public long Id { get; set; }
public int Id { get; set; }
public string Content { get; set; } = null!;
/// <summary>
/// constructor of an answer
/// </summary>
/// <param name="id">the id of an answer</param>
/// <param name="content">the content of an answer</param>
public AnswerDto(long id, string content)
/// <param name="id">the id of an answer</param>
public AnswerDto(string content, int id = -1)
{
Id = id;
Content = content;
Id = id;
}
}
}

@ -1,24 +1,24 @@
using Entities;
using Microsoft.EntityFrameworkCore;
namespace DbConnectionLibrairie
{
public class MyDbContext : DbContext
{
public DbSet<AnswerEntity> Answers { get; set; }
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)
{
if (!options.IsConfigured)
{
options.UseSqlite(@"Data source = database.db");
}
}
}
}
{ }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (!options.IsConfigured)
{
options.UseSqlite(@"Data source = database.db");
}
}
}
}

@ -1,4 +1,6 @@
namespace Entities
using System.ComponentModel.DataAnnotations.Schema;
namespace Entities
{
public class AnswerEntity
{
@ -8,31 +10,10 @@
/// Id : identifier in the database
/// Content : content of the answer
/// </summary>
public long Id { get; set; }
public int 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();
}
[ForeignKey(nameof(QuestionEntity))]
public int IdQuestion { get; set; }
}
}

@ -3,6 +3,7 @@ using Entities;
using ManagerInterfaces;
using Microsoft.EntityFrameworkCore;
using OrderCriterias;
using System.Collections.ObjectModel;
namespace EntityManagers
{
@ -32,11 +33,6 @@ namespace EntityManagers
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)
{
if ((nb - 1) * count >= getNbElement()) throw new Exception("too many page skiped");
@ -50,6 +46,11 @@ namespace EntityManagers
}
}
private AnswerEntity? getAnswer(long id)
{
return dbContext.Answers.Where(a => a.Id == id).FirstOrDefaultAsync().Result;
}
public AnswerEntity? modifierAnswer(long id, AnswerEntity answer)
{
var tmp = getAnswer(id);
@ -67,5 +68,16 @@ namespace EntityManagers
dbContext.SaveChangesAsync();
return tmp;
}
public void supprimerAnswer(AnswerEntity answer)
{
dbContext.Answers.Remove(answer);
dbContext.SaveChangesAsync();
}
public ReadOnlyCollection<AnswerEntity>? getAnswersByIdQuestion(long id)
{
throw new NotImplementedException();
}
}
}

@ -1,4 +1,5 @@
using OrderCriterias;
using System.Collections.ObjectModel;
namespace ManagerInterfaces
{
@ -28,12 +29,11 @@ namespace ManagerInterfaces
/// <summary>
/// get a T element with an id
/// </summary>
/// <param name="id">the id of the T element</param>
/// <param name="id">the id of the T element question</param>
/// <returns>
/// the T element that corresponde to
/// the id or null if there isn't any
/// a set of all T element that correspond to this question
/// </returns>
public T? getAnswer(long id);
public ReadOnlyCollection<T>? getAnswersByIdQuestion(long id);
/// <summary>
/// modified a T element with an id
/// </summary>
@ -54,6 +54,11 @@ namespace ManagerInterfaces
/// </returns>
public T? supprimerAnswer(long id);
/// <summary>
/// delete a T element
/// </summary>
/// <param name="answer">the T element to delete</param>
public void supprimerAnswer(T answer);
/// <summary>
/// add a T element
/// </summary>
/// <param name="answer">the answer to add</param>

@ -8,12 +8,12 @@
/// </summary>
public class Answer
{
private long id;
private int id;
private string? content;
/// <summary>
/// property for id manipulations
/// </summary>
public long Id
public int Id
{
get
{
@ -44,7 +44,7 @@
/// </summary>
/// <param name="content">the content of an answer</param>
/// <param name="id">the id of an answer</param>
private Answer(string content, long id = -1)
public Answer(string content, int id = -1)
{
Id = id;
Content = content;

@ -127,21 +127,12 @@ namespace UnitTestsEntityManagers
}
}
[Fact]
public void testGetAnswer()
{
// with an id which correspond of
// an answer which is in the database
foreach (var a in answers)
{
var tmp = mgr.getAnswer(a.Id+2);
Assert.NotNull(tmp);
Assert.Equal(a.Content, tmp.Content);
Assert.Equal(a.Id, tmp.Id);
}
}
// getAnswers
// modifierAnswer
// supprimerAnswer (les 2)
// getAnswersByIdQuestion
}
}

@ -22,6 +22,10 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FakeDatas", "FakeDatas", "{
fake-Answers.json = fake-Answers.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{95E60A3C-AB3C-4B8F-BAA2-EEB899E12CE7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ExtensionsClassLibrairie", "ExtensionsClassLibrairie\ExtensionsClassLibrairie.csproj", "{66AAAC2F-10B0-4F29-971C-3548EB34B621}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -56,6 +60,14 @@ Global
{6E2E46BB-91F0-4F5C-B378-A54E4F80ED0C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6E2E46BB-91F0-4F5C-B378-A54E4F80ED0C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6E2E46BB-91F0-4F5C-B378-A54E4F80ED0C}.Release|Any CPU.Build.0 = Release|Any CPU
{95E60A3C-AB3C-4B8F-BAA2-EEB899E12CE7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{95E60A3C-AB3C-4B8F-BAA2-EEB899E12CE7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{95E60A3C-AB3C-4B8F-BAA2-EEB899E12CE7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{95E60A3C-AB3C-4B8F-BAA2-EEB899E12CE7}.Release|Any CPU.Build.0 = Release|Any CPU
{66AAAC2F-10B0-4F29-971C-3548EB34B621}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{66AAAC2F-10B0-4F29-971C-3548EB34B621}.Debug|Any CPU.Build.0 = Debug|Any CPU
{66AAAC2F-10B0-4F29-971C-3548EB34B621}.Release|Any CPU.ActiveCfg = Release|Any CPU
{66AAAC2F-10B0-4F29-971C-3548EB34B621}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

Loading…
Cancel
Save