ajout console test + continuation dbmanager + injection fournisseur

pull/5/head
Kevin MONDEJAR 1 month ago
parent 61fa6f89d7
commit d0c0519ca0

@ -7,4 +7,12 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StubbedContextLib\StubbedContextLib.csproj" />
</ItemGroup>
</Project>

@ -1 +1,41 @@
Console.WriteLine("bonjour");
using Contextlib;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using StubbedContextLib;
using static System.Net.WebRequestMethods;
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<WTFContext>()
.UseSqlite(connection)
.Options;
using (var _context = new StubWTFContext(options))
{
_context.Database.EnsureCreated();
// ---- Test Image ---- //
var imageManager = new DbImagesManager(_context);
await imageManager.AddImage(new Entity.Images() { Id = 11, ImgPath = "https://www.bing.com/ck/a?!&&p=390428c2820add92760900204667aa721b17d4eb9e8537c91544d76283d06b14JmltdHM9MTc0MjQyODgwMA&ptn=3&ver=2&hsh=4&fclid=297ef5ed-ac44-66f2-2498-e058adb06776&u=a1aHR0cHM6Ly93d3cucG9rZXBlZGlhLmZyL01hamFzcGlj&ntb=1" });
// ---- Test Character ---- //
var characterManager = new DbCharacterManager(_context);
await characterManager.AddCharacter(new Entity.Character() { Id = 11, Name = "Majespic", IdImage = 11});
// recupération données
var characters = await characterManager.GetAll();
// affichage des dponnées récupérer
foreach (var charac in characters.items)
{
Console.WriteLine("(" + charac.Id + ") " + charac.Name + " / Image ref :" + charac.IdImage);
}
}

@ -73,7 +73,7 @@ namespace Contextlib
public async Task<PaginationResult<Commentary>> GetAllComment()
{
var comments = _context.comments.ToList();
var comments = await _context.comments.ToListAsync();
return new PaginationResult<Commentary>(comments.Count, 0, comments.Count, comments);
}
@ -133,7 +133,7 @@ namespace Contextlib
public async Task<int> LastCommentId()
{
var last = _context.comments.OrderByDescending(x => x.Id).FirstOrDefault();
var last = await _context.comments.OrderByDescending(x => x.Id).FirstOrDefaultAsync();
if(last == null)
{
return 0;
@ -154,7 +154,20 @@ namespace Contextlib
public async Task UpdateComment(int id, Commentary comment)
{
throw new NotImplementedException();
var com = await _context.comments.Where(x => x.Id == id).FirstOrDefaultAsync();
if (comment == null)
{
throw new ArgumentNullException(nameof(comment), "The updated comment data cannot be null.");
}
if (com == null)
{
throw new KeyNotFoundException($"No comments found with the given ID: {id}.");
}
com.Comment = comment.Comment;
com.DateCommentary = comment.DateCommentary;
com.IdQuote = comment.IdQuote;
com.IdUser = comment.IdUser;
await _context.SaveChangesAsync();
}
}
}

@ -0,0 +1,40 @@
using Entity;
using Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contextlib
{
public class DbFavoriteManager : IFavoriteService
{
private WTFContext _context;
public DbFavoriteManager(WTFContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
}
public async Task AddFavorite(int quoteid, int userId)
{
throw new NotImplementedException();
}
public async Task RemoveAllFavoriteForQuote(int quoteId)
{
throw new NotImplementedException();
}
public async Task RemoveAllFavoriteForUser(int userId)
{
throw new NotImplementedException();
}
public async Task RemoveFavorite(int quoteid, int userId)
{
throw new NotImplementedException();
}
}
}

@ -0,0 +1,101 @@
using Entity;
using Microsoft.EntityFrameworkCore;
using Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Contextlib
{
public class DbImagesManager : IImagesService<Images>
{
private WTFContext _context;
public DbImagesManager(WTFContext context)
{
_context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null.");
}
public async Task AddImage(Images image)
{
await _context.AddAsync(image);
await _context.SaveChangesAsync();
}
public async Task<PaginationResult<Images>> GetAllImage()
{
var images = await _context.images.ToListAsync();
return new PaginationResult<Images>(images.Count, 0, images.Count, images);
}
public async Task<Images> GetImageById(int id)
{
var image = await _context.images.Where(x => x.Id == id).FirstOrDefaultAsync();
if (image == null)
{
throw new KeyNotFoundException($"No image found with the given ID: {id}.");
}
return image;
}
public async Task<int> GetLastImageId()
{
var last = await _context.images.OrderByDescending(x => x.Id).FirstOrDefaultAsync();
if (last == null)
{
return 0;
}
return last.Id;
}
public async Task<PaginationResult<Images>> GetSomeImage(int index, int pageSize)
{
var images = await _context.images.ToListAsync();
if (!images.Any())
{
throw new KeyNotFoundException($"No images found");
}
if ((index * pageSize + pageSize) > images.Count)
{
if (pageSize > images.Count)
{
return new PaginationResult<Images>(images.Count(), index, pageSize, images);
}
else
{
return new PaginationResult<Images>(pageSize, index, pageSize, images.Skip(index * pageSize - (((index * pageSize) + pageSize) - images.Count)).Take(pageSize).ToList());
}
}
return new PaginationResult<Images>(images.Count, index, pageSize, images.Skip(index * pageSize).Take(pageSize).ToList());
}
public async Task RemoveImage(int id)
{
var image = await _context.images.Where(x => x.Id == id).FirstOrDefaultAsync();
if (image == null)
{
throw new KeyNotFoundException($"No image found with the given ID: {id}.");
}
_context.images.Remove(image);
await _context.SaveChangesAsync();
}
public async Task UpdateImage(int id, Images image)
{
var img = await _context.images.Where(x => x.Id == id).FirstOrDefaultAsync();
if (image == null)
{
throw new ArgumentNullException(nameof(image), "The updated image data cannot be null.");
}
if (img == null)
{
throw new KeyNotFoundException($"No image found with the given ID: {id}.");
}
img.ImgPath = image.ImgPath;
await _context.SaveChangesAsync();
}
}
}

@ -0,0 +1,73 @@
using Entity;
using Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Contextlib
{
public class DbQuestionSerice : IQuestionService<Question>
{
public Task AddQuestion(Question question)
{
throw new NotImplementedException();
}
public Task<int> CountQuestions()
{
throw new NotImplementedException();
}
public Task<PaginationResult<Question>> GetAllQuestion()
{
throw new NotImplementedException();
}
public Task<PaginationResult<Question>> GetInvalidQuestion(int index, int pageSize)
{
throw new NotImplementedException();
}
public Task<Question> GetQuestionById(int id)
{
throw new NotImplementedException();
}
public Task<Question> GetRandomQuestion()
{
throw new NotImplementedException();
}
public Task<Question> GetRandomQuestionQuoteToCharacter()
{
throw new NotImplementedException();
}
public Task<Question> GetRandomQuestionQuoteToSource()
{
throw new NotImplementedException();
}
public Task<PaginationResult<Question>> GetSomeQuestion(int index, int pageSize)
{
throw new NotImplementedException();
}
public Task RemoveQuestion(int id)
{
throw new NotImplementedException();
}
public Task UpdateQuestion(int id, Question question)
{
throw new NotImplementedException();
}
public Task ValidateQuestion(int id, bool isvalid)
{
throw new NotImplementedException();
}
}
}

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Entity;
@ -75,7 +76,19 @@ namespace Contextlib
);
}
public WTFContext()
{ }
public WTFContext(DbContextOptions<WTFContext> options)
: base(options)
{ }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Wf-Database.mdf;Trusted_Connection=True;");
{
if (!options.IsConfigured)
{
options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Wf-Database.mdf;Trusted_Connection=True;");
}
}
}
}

@ -10,7 +10,7 @@ namespace Shared
{
// Retrieves an image by its unique identifier (id).
// 'id' is the unique identifier of the image.
Task<TImage> GetImageById(string id);
Task<TImage> GetImageById(int id);
// Retrieves all images, with pagination support.
// This returns a list of all images in the system.

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using Contextlib;
using Entity;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace StubbedContextLib
{
@ -133,10 +134,21 @@ namespace StubbedContextLib
new Source() { Id = 4, Title = "Harry Potter à l'école des sorcier", TypeSrc = TypeSrcEnum.movie, Year = 1997 },
new Source() { Id = 5, Title = "Star Wars, épisode IV : Un nouvel espoir", TypeSrc = TypeSrcEnum.movie, Year = 1977 }
);
}
public StubWTFContext()
{ }
public StubWTFContext(DbContextOptions<WTFContext> options)
: base(options)
{ }
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (!options.IsConfigured)
{
options.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Wf-Database.mdf;Trusted_Connection=True;");
}
}
}
}

@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

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

@ -13,8 +13,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Contextlib", "Contextlib\Co
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubbedContextLib", "StubbedContextLib\StubbedContextLib.csproj", "{5CD69B14-C6AE-4628-A374-996C486E25F2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestModel2Entities", "TestModel2Entities\TestModel2Entities.csproj", "{2CF20FAC-C2F1-4048-9D46-F39081B0FBEF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model2Entities", "Model2entities\Model2Entities.csproj", "{4A1CBA3D-C798-4E19-865F-39F919F1205A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "XUnitTest", "XUnitTest\XUnitTest.csproj", "{48002CA2-7CFF-4077-90CF-392476320CE3}"
@ -49,10 +47,6 @@ Global
{5CD69B14-C6AE-4628-A374-996C486E25F2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5CD69B14-C6AE-4628-A374-996C486E25F2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5CD69B14-C6AE-4628-A374-996C486E25F2}.Release|Any CPU.Build.0 = Release|Any CPU
{2CF20FAC-C2F1-4048-9D46-F39081B0FBEF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2CF20FAC-C2F1-4048-9D46-F39081B0FBEF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2CF20FAC-C2F1-4048-9D46-F39081B0FBEF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2CF20FAC-C2F1-4048-9D46-F39081B0FBEF}.Release|Any CPU.Build.0 = Release|Any CPU
{4A1CBA3D-C798-4E19-865F-39F919F1205A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A1CBA3D-C798-4E19-865F-39F919F1205A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A1CBA3D-C798-4E19-865F-39F919F1205A}.Release|Any CPU.ActiveCfg = Release|Any CPU

@ -1,11 +0,0 @@
namespace XUnitTest
{
public class UnitTest1
{
[Fact]
public void Test1()
{
}
}
}

@ -11,11 +11,16 @@
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="9.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\StubbedContextLib\StubbedContextLib.csproj" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

Loading…
Cancel
Save