Implementation of Unit of Work + Tests UserENtity + fix problem API userArticle
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
parent
739f157fbc
commit
43c30f5277
@ -1,7 +1,6 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="VcsDirectoryMappings">
|
<component name="VcsDirectoryMappings">
|
||||||
<mapping directory="$PROJECT_DIR$/../.." vcs="Git" />
|
<mapping directory="$PROJECT_DIR$" vcs="" />
|
||||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
|
||||||
</component>
|
</component>
|
||||||
</project>
|
</project>
|
@ -1,122 +0,0 @@
|
|||||||
using API_Mapping;
|
|
||||||
using API_Services;
|
|
||||||
using Entities;
|
|
||||||
using Microsoft.AspNetCore.Mvc;
|
|
||||||
|
|
||||||
namespace API.Controllers;
|
|
||||||
|
|
||||||
[Route("api/[controller]")]
|
|
||||||
[ApiController]
|
|
||||||
public class ArticleUserController : ControllerBase
|
|
||||||
{
|
|
||||||
|
|
||||||
private readonly IArticleUserService _us;
|
|
||||||
private readonly ILogger<ArticleUserController> _logger;
|
|
||||||
public ArticleUserController(IArticleUserService us, ILogger<ArticleUserController> logger)
|
|
||||||
{
|
|
||||||
this._us = us;
|
|
||||||
this._logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
[HttpGet("/articleUsers")]
|
|
||||||
public async Task<IActionResult> GetAllArticleUsers()
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetAllArticleUsers), "");
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var result = (await _us.GetAllArticleUsers()).Select(u => u.ToDTO());
|
|
||||||
if (result == null)
|
|
||||||
{
|
|
||||||
return NoContent();
|
|
||||||
}
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
catch (Exception error)
|
|
||||||
{
|
|
||||||
_logger.LogError(error.Message);
|
|
||||||
return BadRequest(error.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpGet("/user/{pseudo}/article")]
|
|
||||||
public async Task<IActionResult> GetArticleUser(string pseudo)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(GetArticleUser), pseudo);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var result = await _us.GetArticleUser(pseudo);
|
|
||||||
if (result == null)
|
|
||||||
{
|
|
||||||
return NoContent();
|
|
||||||
}
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
catch (Exception error)
|
|
||||||
{
|
|
||||||
_logger.LogError(error.Message);
|
|
||||||
return BadRequest(error.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPost("/user/{pseudo}/article")]
|
|
||||||
public async Task<IActionResult> CreateArticleUser(ArticleUserEntity articleUser)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(CreateArticleUser), articleUser);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var result = await _us.CreateArticleUser(articleUser);
|
|
||||||
if (result == null)
|
|
||||||
{
|
|
||||||
return BadRequest($"ArticleUser {articleUser.UserEntityPseudo} already exists");
|
|
||||||
}
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
catch (Exception error)
|
|
||||||
{
|
|
||||||
_logger.LogError(error.Message);
|
|
||||||
return BadRequest(error.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpDelete("/user/{pseudo}/article")]
|
|
||||||
public async Task<IActionResult> DeleteArticleUser(string pseudo)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(DeleteArticleUser), pseudo);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var result = await _us.DeleteArticleUser(pseudo);
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return BadRequest($"ArticleUser {pseudo} does not exist");
|
|
||||||
}
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
catch (Exception error)
|
|
||||||
{
|
|
||||||
_logger.LogError(error.Message);
|
|
||||||
return BadRequest(error.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[HttpPut("/user/{pseudo}/article")]
|
|
||||||
public async Task<IActionResult> UpdateArticleUser(ArticleUserEntity articleUser)
|
|
||||||
{
|
|
||||||
_logger.LogInformation("Executing {Action} - with parameters: {Parameters}",nameof(UpdateArticleUser), articleUser);
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var result = await _us.UpdateArticleUser(articleUser);
|
|
||||||
if (!result)
|
|
||||||
{
|
|
||||||
return BadRequest($"ArticleUser {articleUser.UserEntityPseudo} does not exist");
|
|
||||||
}
|
|
||||||
return Ok(result);
|
|
||||||
}
|
|
||||||
catch (Exception error)
|
|
||||||
{
|
|
||||||
_logger.LogError(error.Message);
|
|
||||||
return BadRequest(error.Message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Binary file not shown.
@ -1,12 +1,30 @@
|
|||||||
|
using API_Services;
|
||||||
using DbContextLib;
|
using DbContextLib;
|
||||||
|
using Model;
|
||||||
|
|
||||||
namespace DbDataManager;
|
namespace DbDataManager;
|
||||||
|
|
||||||
public class DbManager
|
public class DbManager : IDataManager
|
||||||
{
|
{
|
||||||
protected LibraryContext _context;
|
protected LibraryContext _context { get; set; }
|
||||||
|
|
||||||
public DbManager()
|
public DbManager()
|
||||||
{
|
{
|
||||||
_context = new LibraryContext();
|
_context = new LibraryContext();
|
||||||
|
ArticleService = new DbManagerArticle(_context);
|
||||||
|
UserService = new DbManagerUser(_context);
|
||||||
|
FormulaireService = new DbManagerFormulaire(_context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DbManager(LibraryContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
ArticleService = new DbManagerArticle(_context);
|
||||||
|
UserService = new DbManagerUser(_context);
|
||||||
|
FormulaireService = new DbManagerFormulaire(_context);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IArticleService ArticleService { get; set; }
|
||||||
|
public IUserService UserService { get; set; }
|
||||||
|
public IFormulaireService FormulaireService { get; set; }
|
||||||
}
|
}
|
@ -1,74 +0,0 @@
|
|||||||
using API_Services;
|
|
||||||
using DbContextLib;
|
|
||||||
using Entities;
|
|
||||||
using Microsoft.Extensions.Logging;
|
|
||||||
using Model;
|
|
||||||
|
|
||||||
namespace DbDataManager;
|
|
||||||
|
|
||||||
public class DbManagerArticleUser : IArticleUserService
|
|
||||||
{
|
|
||||||
private readonly LibraryContext _context;
|
|
||||||
|
|
||||||
public DbManagerArticleUser(LibraryContext context)
|
|
||||||
{
|
|
||||||
_context = context;
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<IEnumerable<User?>> GetAllArticleUsers()
|
|
||||||
{
|
|
||||||
var entities = _context.ArticleUserSet.ToList();
|
|
||||||
List<UserEntity> users = new List<UserEntity>();
|
|
||||||
foreach( var articleUser in entities)
|
|
||||||
{
|
|
||||||
var user = _context.UserSet.FirstOrDefault(u => u.Pseudo.Equals(articleUser.UserEntityPseudo));
|
|
||||||
if (user != null) users.Add(user);
|
|
||||||
}
|
|
||||||
if (users == null) return await Task.FromResult<IEnumerable<User?>>(null);
|
|
||||||
return await Task.FromResult(users.Select(u => u.ToModel()).AsEnumerable());
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<ArticleUserEntity?> GetArticleUser(string pseudo)
|
|
||||||
{
|
|
||||||
var entity = _context.ArticleUserSet.FirstOrDefault(a => a.UserEntityPseudo.Equals(pseudo));
|
|
||||||
if (entity == null) return await Task.FromResult<ArticleUserEntity?>(null);
|
|
||||||
return await Task.FromResult(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public async Task<ArticleUserEntity?> CreateArticleUser(ArticleUserEntity articleUser)
|
|
||||||
{
|
|
||||||
var result = await GetArticleUser(articleUser.UserEntityPseudo);
|
|
||||||
if (result != null) return await Task.FromResult<ArticleUserEntity?>(null);
|
|
||||||
var entity = new ArticleUserEntity()
|
|
||||||
{
|
|
||||||
ArticleEntityId = articleUser.ArticleEntityId,
|
|
||||||
UserEntityPseudo = articleUser.UserEntityPseudo
|
|
||||||
};
|
|
||||||
if (entity == null) return await Task.FromResult<ArticleUserEntity?>(null);
|
|
||||||
_context.ArticleUserSet.Add(entity);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
return await Task.FromResult(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<bool> DeleteArticleUser(string pseudo)
|
|
||||||
{
|
|
||||||
var entity = _context.ArticleUserSet.FirstOrDefault(a => a.UserEntityPseudo.Equals(pseudo));
|
|
||||||
if (entity == null) return await Task.FromResult(false);
|
|
||||||
_context.ArticleUserSet.Remove(entity);
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
return await Task.FromResult(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task<bool> UpdateArticleUser(ArticleUserEntity articleUser)
|
|
||||||
{
|
|
||||||
var entity = _context.ArticleUserSet.FirstOrDefault(a => a.UserEntityPseudo.Equals(articleUser.UserEntityPseudo));
|
|
||||||
if (entity == null) return await Task.FromResult(false);
|
|
||||||
entity.ArticleEntityId = articleUser.ArticleEntityId;
|
|
||||||
entity.UserEntityPseudo = articleUser.UserEntityPseudo;
|
|
||||||
await _context.SaveChangesAsync();
|
|
||||||
return await Task.FromResult(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,10 @@
|
|||||||
|
namespace Model;
|
||||||
|
using API_Services;
|
||||||
|
|
||||||
|
public interface IDataManager
|
||||||
|
{
|
||||||
|
IArticleService ArticleService { get; }
|
||||||
|
IUserService UserService { get; }
|
||||||
|
IFormulaireService FormulaireService { get; }
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
|
||||||
|
<IsPackable>false</IsPackable>
|
||||||
|
<IsTestProject>true</IsTestProject>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
|
||||||
|
<PackageReference Include="xunit" Version="2.4.2"/>
|
||||||
|
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
<PackageReference Include="coverlet.collector" Version="6.0.0">
|
||||||
|
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||||
|
<PrivateAssets>all</PrivateAssets>
|
||||||
|
</PackageReference>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
@ -0,0 +1 @@
|
|||||||
|
global using Xunit;
|
@ -0,0 +1,9 @@
|
|||||||
|
namespace API_Unit_Test;
|
||||||
|
|
||||||
|
public class UnitTest1
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Test1()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
@ -0,0 +1,182 @@
|
|||||||
|
using DbContextLib;
|
||||||
|
using Entities;
|
||||||
|
using Microsoft.Data.Sqlite;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
|
namespace Tests;
|
||||||
|
|
||||||
|
public class TestsUserEntity
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Add_Test()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
|
||||||
|
using (var context = new LibraryContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
UserEntity u1 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Tofgasy", Prenom = "Tony", Nom = "Fages", Mail = "he@gmail.com", Mdp = "1234", Role = "Admin"
|
||||||
|
};
|
||||||
|
UserEntity u2 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Blizzard", Prenom = "Louis", Nom = "Laborie", Mail = "he@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Admin"
|
||||||
|
};
|
||||||
|
UserEntity u3 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "TomS", Prenom = "Tom", Nom = "Smith", Mail = "TomS@gmail.com", Mdp = "1234", Role = "User"
|
||||||
|
};
|
||||||
|
UserEntity u4 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Siwa", Prenom = "Jean", Nom = "Marcillac", Mail = "occitan@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Amin"
|
||||||
|
};
|
||||||
|
UserEntity u5 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Sha", Prenom = "Shana", Nom = "Cascarra", Mail = "shacas@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Modérator"
|
||||||
|
};
|
||||||
|
UserEntity u6 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "NoaSil", Prenom = "Noa", Nom = "Sillard", Mail = "noaSillar@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Admin"
|
||||||
|
};
|
||||||
|
context.UserSet.Add(u1);
|
||||||
|
context.UserSet.Add(u2);
|
||||||
|
context.UserSet.Add(u3);
|
||||||
|
context.UserSet.Add(u4);
|
||||||
|
context.UserSet.Add(u5);
|
||||||
|
context.UserSet.Add(u6);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
Assert.Equal(6, context.UserSet.Count());
|
||||||
|
Assert.Equal("Blizzard", context.UserSet.First().Pseudo);
|
||||||
|
connection.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Modify_Test()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
using (var context = new LibraryContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
UserEntity u1 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Tofgasy", Prenom = "Tony", Nom = "Fages", Mail = "he@gmail.com", Mdp = "1234", Role = "Admin"
|
||||||
|
};
|
||||||
|
UserEntity u2 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Blizzard", Prenom = "Louis", Nom = "Laborie", Mail = "he@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Admin"
|
||||||
|
};
|
||||||
|
UserEntity u3 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "TomS", Prenom = "Tom", Nom = "Smith", Mail = "TomS@gmail.com", Mdp = "1234", Role = "User"
|
||||||
|
};
|
||||||
|
UserEntity u4 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Siwa", Prenom = "Jean", Nom = "Marcillac", Mail = "occitan@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Amin"
|
||||||
|
};
|
||||||
|
UserEntity u5 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Sha", Prenom = "Shana", Nom = "Cascarra", Mail = "shacas@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Modérator"
|
||||||
|
};
|
||||||
|
UserEntity u6 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "NoaSil", Prenom = "Noa", Nom = "Sillard", Mail = "noaSillar@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Admin"
|
||||||
|
};
|
||||||
|
context.UserSet.Add(u1);
|
||||||
|
context.UserSet.Add(u2);
|
||||||
|
context.UserSet.Add(u3);
|
||||||
|
context.UserSet.Add(u4);
|
||||||
|
context.UserSet.Add(u5);
|
||||||
|
context.UserSet.Add(u6);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
var user = context.UserSet.First(u => u.Pseudo.Equals("Tofgasy"));
|
||||||
|
user.Prenom = "Tof";
|
||||||
|
context.SaveChanges();
|
||||||
|
string persRemove = "Tony";
|
||||||
|
string persNew = "Tof";
|
||||||
|
Assert.Equal(1, context.UserSet.Count(u => u.Prenom.Equals(persNew)));
|
||||||
|
Assert.Equal(0, context.UserSet.Count(u => u.Prenom.Equals(persRemove)));
|
||||||
|
connection.Close();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Remove_Test()
|
||||||
|
{
|
||||||
|
var connection = new SqliteConnection("DataSource=:memory:");
|
||||||
|
connection.Open();
|
||||||
|
|
||||||
|
var options = new DbContextOptionsBuilder<LibraryContext>()
|
||||||
|
.UseSqlite(connection)
|
||||||
|
.Options;
|
||||||
|
using (var context = new LibraryContext(options))
|
||||||
|
{
|
||||||
|
context.Database.EnsureCreated();
|
||||||
|
UserEntity u1 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Tofgasy", Prenom = "Tony", Nom = "Fages", Mail = "he@gmail.com", Mdp = "1234", Role = "Admin"
|
||||||
|
};
|
||||||
|
UserEntity u2 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Blizzard", Prenom = "Louis", Nom = "Laborie", Mail = "he@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Admin"
|
||||||
|
};
|
||||||
|
UserEntity u3 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "TomS", Prenom = "Tom", Nom = "Smith", Mail = "TomS@gmail.com", Mdp = "1234", Role = "User"
|
||||||
|
};
|
||||||
|
UserEntity u4 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Siwa", Prenom = "Jean", Nom = "Marcillac", Mail = "occitan@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Amin"
|
||||||
|
};
|
||||||
|
UserEntity u5 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "Sha", Prenom = "Shana", Nom = "Cascarra", Mail = "shacas@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Modérator"
|
||||||
|
};
|
||||||
|
UserEntity u6 = new UserEntity
|
||||||
|
{
|
||||||
|
Pseudo = "NoaSil", Prenom = "Noa", Nom = "Sillard", Mail = "noaSillar@gmail.com", Mdp = "1234",
|
||||||
|
Role = "Admin"
|
||||||
|
};
|
||||||
|
context.UserSet.Add(u1);
|
||||||
|
context.UserSet.Add(u2);
|
||||||
|
context.UserSet.Add(u3);
|
||||||
|
context.UserSet.Add(u4);
|
||||||
|
context.UserSet.Add(u5);
|
||||||
|
context.UserSet.Add(u6);
|
||||||
|
context.SaveChanges();
|
||||||
|
|
||||||
|
Assert.Equal(6, context.UserSet.Count());
|
||||||
|
var user = context.UserSet.First(u => u.Pseudo.Equals("Tofgasy"));
|
||||||
|
context.Remove(user);
|
||||||
|
context.SaveChanges();
|
||||||
|
Assert.Equal(5, context.UserSet.Count());
|
||||||
|
Assert.Equal(0, context.UserSet.Count(u => u.Pseudo.Equals("Tofgasy")));
|
||||||
|
connection.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue