Ca marche pas

testTony
Tony Fages 1 year ago
parent 25712e7513
commit 3e6bdbb85a

@ -12,6 +12,10 @@
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.1"/>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>

@ -29,7 +29,7 @@ namespace API.Controllers
}
[HttpGet("/article/{id}")]
public async Task<Article?> GetArticleById(int id)
public async Task<ArticleEntity?> GetArticleById(int id)
{
var result = await _articleService.GetArticleById(id);
if (result == null)
@ -42,19 +42,19 @@ namespace API.Controllers
[HttpPost("/article")]
public async Task<Article?> CreateArticle(long id, string title, string description, string author, string date, int lectureTime)
public async Task<ArticleEntity?> CreateArticle(long id, string title, string description, string author, string date, int lectureTime)
{
return await _articleService.CreateArticle(id, title, description, author, date, lectureTime);
}
[HttpDelete("/article/{id}")]
public async Task<Article?> DeleteArticle(long id)
public async Task<ArticleEntity?> DeleteArticle(long id)
{
return await _articleService.DeleteArticle(id);
}
[HttpPut("/article/{id}")]
public async Task<bool> UpdateArticle(long id, Article? a)
public async Task<bool> UpdateArticle(long id, ArticleEntity? a)
{
return await _articleService.UpdateArticle(id, a);
}

@ -2,6 +2,7 @@ using API_Services;
using DbContextLib;
using DbDataManager;
using Microsoft.EntityFrameworkCore;
using StubbedContextLib;
var builder = WebApplication.CreateBuilder(args);
@ -14,8 +15,10 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<LibraryContext>();
builder.Services.AddDbContext<LibraryContext>(options =>
{
options.UseSqlite("Data Source=Entity_FrameWork.Article.db");
});
builder.Services.AddScoped<IArticleService, DbManagerArticle>();
builder.Services.AddScoped<IUserService, DbManagerUser>();
builder.Services.AddScoped<IFormulaireService, DbManagerFormulaire>();
@ -31,7 +34,5 @@ if (app.Environment.IsDevelopment())
app.UseHttpsRedirection();
app.Run();

@ -2,6 +2,7 @@ using API_Services;
using DbContextLib;
using Entities;
using Model;
using ArticleEntity = Model.ArticleEntity;
namespace DbDataManager;
@ -11,12 +12,13 @@ public class DbManagerArticle : IArticleService
public DbManagerArticle(LibraryContext context)
{
_context = new LibraryContext();
_context = context;
}
public async Task<Article?> CreateArticle(long id, string title, string description, string author, string date, int lectureTime)
public async Task<ArticleEntity?> CreateArticle(long id, string title, string description, string author, string date, int lectureTime)
{
var entity = new ArticleEntity()
var entity = new Entities.ArticleEntity()
{
Id = id,
Title = title,
@ -31,7 +33,7 @@ public class DbManagerArticle : IArticleService
return entity.ToModel();
}
public async Task<Article?> DeleteArticle(long id)
public async Task<ArticleEntity?> DeleteArticle(long id)
{
var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id);
Console.WriteLine(entity);
@ -41,7 +43,7 @@ public class DbManagerArticle : IArticleService
return entity.ToModel();
}
public async Task<bool> UpdateArticle(long id, Article? a)
public async Task<bool> UpdateArticle(long id, ArticleEntity? a)
{
var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id);
if (entity == null) return false;
@ -54,15 +56,16 @@ public class DbManagerArticle : IArticleService
return true;
}
public Task<Article?> GetArticleById(int id)
public Task<ArticleEntity?> GetArticleById(int id)
{
var entity = _context.ArticleSet.FirstOrDefault(a => a.Id == id);
return Task.FromResult(entity.ToModel());
}
public async Task<IEnumerable<Article?>> GetAllArticles()
public async Task<IEnumerable<ArticleEntity?>> GetAllArticles()
{
Console.WriteLine("GetAllArticles");
return await Task.FromResult(_context.ArticleSet.Select(a => a.ToModel()).AsEnumerable());
}
}

@ -5,8 +5,16 @@ using Model;
namespace DbDataManager;
public class DbManagerFormulaire(LibraryContext _context) : IFormulaireService
public class DbManagerFormulaire : IFormulaireService
{
private readonly LibraryContext _context;
public DbManagerFormulaire(LibraryContext context)
{
_context = context;
}
public async Task<IEnumerable<Formulaire?>> GetAllForm()
{
return await Task.FromResult(_context.FormSet.Select(f => f.ToModel()).AsEnumerable());

@ -5,8 +5,16 @@ using Model;
namespace DbDataManager;
public class DbManagerUser(LibraryContext _context): IUserService
public class DbManagerUser: IUserService
{
private readonly LibraryContext _context;
public DbManagerUser(LibraryContext context)
{
_context = context;
}
public async Task<bool> Create(User user)
{
var entity = new UserEntity()

@ -1,19 +1,20 @@
using Entities;
using Model;
using ArticleEntity = Model.ArticleEntity;
namespace DbDataManager;
public static class Extensions
{
public static ArticleEntity ToEntity(this Article article)
=> new ArticleEntity
public static Entities.ArticleEntity ToEntity(this ArticleEntity articleEntity)
=> new Entities.ArticleEntity
{
Id = article.Id, Author = article.Author, Description = article.Description, Title = article.Title,
DatePublished = article.DatePublished, LectureTime = article.LectureTime
Id = articleEntity.Id, Author = articleEntity.Author, Description = articleEntity.Description, Title = articleEntity.Title,
DatePublished = articleEntity.DatePublished, LectureTime = articleEntity.LectureTime
};
public static Article ToModel(this ArticleEntity article)
=> new Article
public static ArticleEntity ToModel(this Entities.ArticleEntity article)
=> new ArticleEntity
{
Id = article.Id, Author = article.Author, Description = article.Description, Title = article.Title,
DatePublished = article.DatePublished, LectureTime = article.LectureTime

@ -5,7 +5,7 @@ namespace API_Mapping;
public static class ArticleMapper
{
public static ArticleDTO ToDTO(this Article a) => new()
public static ArticleDTO ToDTO(this ArticleEntity a) => new()
{
Id = a.Id,
Title = a.Title,
@ -15,7 +15,7 @@ public static class ArticleMapper
Author = a.Author
};
public static Article ToModel(this ArticleDTO a) => new()
public static ArticleEntity ToModel(this ArticleDTO a) => new()
{
Id = a.Id,
Title = a.Title,

@ -4,16 +4,16 @@ namespace API_Services
{
public interface IArticleService
{
Task<Article?> CreateArticle(long id, string title, string description, string author, string date,
Task<ArticleEntity?> CreateArticle(long id, string title, string description, string author, string date,
int lectureTime);
Task<Article?> DeleteArticle(long id);
Task<ArticleEntity?> DeleteArticle(long id);
Task<bool> UpdateArticle(long id, Article? a);
Task<bool> UpdateArticle(long id, ArticleEntity? a);
Task<Article?> GetArticleById(int id);
Task<ArticleEntity?> GetArticleById(int id);
Task<IEnumerable<Article?>> GetAllArticles();
Task<IEnumerable<ArticleEntity?>> GetAllArticles();
}
}

@ -35,5 +35,83 @@ public class LibraryContext : DbContext
.HasMany(a => a.Users)
.WithMany(a => a.Articles)
.UsingEntity<ArticleUserEntity>();
modelBuilder.Entity<ArticleEntity>().HasData(
new ArticleEntity
{
Id = 1,
Title = "Breaking News Elisabeth 2 Died",
Description = "The queen of England died today at the age of 95",
DatePublished = "2022-02-06",
LectureTime = 2,
Author = "Tom Smith"
},
new ArticleEntity
{
Id = 2,
Title = "The new iPhone 15",
Description = "The new iPhone 15 is out and it's the best phone ever",
DatePublished = "2022-02-06",
LectureTime = 3,
Author = "Tom Smith"
},
new ArticleEntity
{
Id = 3,
Title = "M&M's new recipe",
Description = "M&M's new recipe is out and it's the best chocolate ever",
DatePublished = "2022-02-06",
LectureTime = 1,
Author = "M&M's Red"
}
);
modelBuilder.Entity<UserEntity>().HasData(
new UserEntity
{
Id = 1, Nom = "Fages", Prenom = "Tony", Pseudo = "TonyF", Mail = "tony@gmail.com", Mdp = "1234", Role = "Admin"
},
new UserEntity
{
Id = 2, Nom = "Smith", Prenom = "Tom", Pseudo = "TomS", Mail = "tom@mail.com", Mdp = "1234",
Role = "User"
},
new UserEntity
{
Id = 3, Nom = "M&M's", Prenom = "Red", Pseudo = "RedM", Mail = "M&M#mail.com", Mdp = "1234", Role = "Modérator"
}
);
modelBuilder.Entity<ArticleUserEntity>().HasData(
new ArticleUserEntity
{
ArticleEntityId = 1,
UserEntityId = 1
},
new ArticleUserEntity
{
ArticleEntityId = 2,
UserEntityId = 2
},
new ArticleUserEntity
{
ArticleEntityId = 3,
UserEntityId = 3
},
new ArticleUserEntity
{
ArticleEntityId = 3,
UserEntityId = 1
},
new ArticleUserEntity
{
ArticleEntityId = 2,
UserEntityId = 3
}
);
}
}

@ -1,6 +1,6 @@
namespace Model;
public class Article
public class ArticleEntity
{
public long Id { get; set; }
public string Title { get; set; } = string.Empty;

@ -0,0 +1,27 @@
using Entities;
using Model;
using ArticleEntity = Model.ArticleEntity;
namespace StubbedContextLib;
public class StubTest
{
private List<ArticleEntity> _article;
public List<ArticleEntity> StubArticle()
{
_article = new List<ArticleEntity>
{
new ArticleEntity
{
Id = 1,
Title = "Test",
Description = "Test",
Author = "Test",
DatePublished = "Test",
LectureTime = 1
}
};
return _article;
}
}

@ -8,6 +8,7 @@
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
<ItemGroup>

Loading…
Cancel
Save