Lien avec la base de données pgsql fait pour UserEntity. L'api renvoie bien la liste des utilisateurs pour le GET

pull/26/head
Johnny RATTON 1 year ago
parent c681a72c84
commit c06c038c08

4
.gitignore vendored

@ -491,3 +491,7 @@ fabric.properties
# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser
# Migrations et fichiers db
**/Migrations/**
*.db

@ -8,7 +8,21 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Services\Services.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,26 @@
using DbContextLib;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.DTO;
using Services;
namespace API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserController : Controller
{
private IDataService _userDataService;
public UserController(IDataService userDataService)
{
_userDataService = userDataService;
}
[HttpGet("users/{page}/{number}")]
public IActionResult GetUsers(int page, int number)
{
return Ok(_userDataService.GetUsers(page, number));
}
}
}

@ -1,3 +1,7 @@
using DbContextLib;
using Microsoft.EntityFrameworkCore;
using Services;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
@ -6,6 +10,8 @@ builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IDataService, UserDataService>();
builder.Services.AddDbContext<DbContext, UserDbContext>();
var app = builder.Build();

@ -5,6 +5,18 @@ VisualStudioVersion = 17.8.34408.163
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API", "API\API.csproj", "{65F4AE69-E1CA-4B87-BDF0-946A37351BD1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "EntityFramework\Entities.csproj", "{6D079CDA-C000-4833-98A0-D07D153EA264}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Model\Model.csproj", "{ADCC427D-A3CD-431C-A90B-F9369C4584E8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestAPI", "TestAPI\TestAPI.csproj", "{17025B90-8B2A-49E9-97D3-1A84A208DF50}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestEF", "TestEF\TestEF.csproj", "{54FAD8AF-7601-4C54-8406-D81476A8D7D7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DbContextLib", "DbContextLib\DbContextLib.csproj", "{BDCB3BFD-B744-4BC0-BCFC-78E08600203A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Services", "Services\Services.csproj", "{9BD3DCBA-AFD8-47FA-B07C-613B53E63968}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -15,6 +27,30 @@ Global
{65F4AE69-E1CA-4B87-BDF0-946A37351BD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{65F4AE69-E1CA-4B87-BDF0-946A37351BD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{65F4AE69-E1CA-4B87-BDF0-946A37351BD1}.Release|Any CPU.Build.0 = Release|Any CPU
{6D079CDA-C000-4833-98A0-D07D153EA264}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6D079CDA-C000-4833-98A0-D07D153EA264}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6D079CDA-C000-4833-98A0-D07D153EA264}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6D079CDA-C000-4833-98A0-D07D153EA264}.Release|Any CPU.Build.0 = Release|Any CPU
{ADCC427D-A3CD-431C-A90B-F9369C4584E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{ADCC427D-A3CD-431C-A90B-F9369C4584E8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ADCC427D-A3CD-431C-A90B-F9369C4584E8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ADCC427D-A3CD-431C-A90B-F9369C4584E8}.Release|Any CPU.Build.0 = Release|Any CPU
{17025B90-8B2A-49E9-97D3-1A84A208DF50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{17025B90-8B2A-49E9-97D3-1A84A208DF50}.Debug|Any CPU.Build.0 = Debug|Any CPU
{17025B90-8B2A-49E9-97D3-1A84A208DF50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{17025B90-8B2A-49E9-97D3-1A84A208DF50}.Release|Any CPU.Build.0 = Release|Any CPU
{54FAD8AF-7601-4C54-8406-D81476A8D7D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{54FAD8AF-7601-4C54-8406-D81476A8D7D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{54FAD8AF-7601-4C54-8406-D81476A8D7D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{54FAD8AF-7601-4C54-8406-D81476A8D7D7}.Release|Any CPU.Build.0 = Release|Any CPU
{BDCB3BFD-B744-4BC0-BCFC-78E08600203A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BDCB3BFD-B744-4BC0-BCFC-78E08600203A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BDCB3BFD-B744-4BC0-BCFC-78E08600203A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BDCB3BFD-B744-4BC0-BCFC-78E08600203A}.Release|Any CPU.Build.0 = Release|Any CPU
{9BD3DCBA-AFD8-47FA-B07C-613B53E63968}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9BD3DCBA-AFD8-47FA-B07C-613B53E63968}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9BD3DCBA-AFD8-47FA-B07C-613B53E63968}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9BD3DCBA-AFD8-47FA-B07C-613B53E63968}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

@ -6,4 +6,22 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Cryptography.KeyDerivation" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EntityFramework\Entities.csproj" />
</ItemGroup>
</Project>

@ -1,21 +1,70 @@
using Microsoft.EntityFrameworkCore;
using Entities.SQLudeoDB;
using Microsoft.AspNetCore.Cryptography.KeyDerivation;
using Microsoft.EntityFrameworkCore;
using System.Security.Cryptography;
namespace DbContextLib
{
public class UserDbContext : DbContext
{
public DbSet<UserEntity> Users { get; set; }
public DbSet<BlackListEntity> BlackList { get; set; }
public DbSet<InquiryEntity> Inquiry { get; set; }
public DbSet<InquiryTableEntity> InquiryTable { get; set; }
public DbSet<LessonEntity> Lesson { get; set; }
public DbSet<ContentLessonEntity> ContentLessons { get; set; }
public DbSet<ParagraphEntity> Paragraph { get; set; }
public DbSet<SolutionEntity> Solutions { get; set; }
public DbSet<SuccessEntity> Success { get; set; }
public DbSet<NotepadEntity> Notepad { get; set; }
public UserDbContext(DbContextOptions<UserDbContext> options) : base(options) { }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseNpgsql("Host=localhost;Database=SQLuedo;Username=admin;Password=password");
optionsBuilder.UseNpgsql("Host=localhost;Database=SQLuedo;Username=admin;Password=motdepasse");
}
base.OnConfiguring(optionsBuilder);
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserEntity>().ToTable("user");
modelBuilder.Entity<UserEntity>().HasData(
new UserEntity(1, "johnny", Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Johnny.RATTON@etu.uca.fr", true),
new UserEntity(2, "maxime", Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Maxime.SAPOUNTZIS@etu.uca.fr", true),
new UserEntity(3, "clement", Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Clement.CHIEU@etu.uca.fr", true),
new UserEntity(4, "erwan", Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Erwan.MENAGER@etu.uca.fr", true),
new UserEntity(5, "victor", Convert.ToBase64String(KeyDerivation.Pbkdf2(
password: "motdepasse",
salt: RandomNumberGenerator.GetBytes(128 / 8),
prf: KeyDerivationPrf.HMACSHA256,
iterationCount: 100000,
numBytesRequested: 256 / 8)), "Victor.GABORIT@etu.uca.fr", true));
modelBuilder.Entity<ContentLessonEntity>().HasKey(c => c.LessonId);
modelBuilder.Entity<ContentLessonEntity>().HasKey(c => c.LessonPartId);
modelBuilder.Entity<SuccessEntity>().HasKey(s => s.UserId);
modelBuilder.Entity<SuccessEntity>().HasKey(s => s.InquiryId);
base.OnModelCreating(modelBuilder);
}
}

@ -6,4 +6,17 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
</ItemGroup>
</Project>

@ -6,16 +6,14 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entities
namespace Entities.SQLudeoDB
{
internal class ContentLessonEntity
public class ContentLessonEntity
{
[Key, Column(Order = 0)]
[ForeignKey(nameof(Lesson))]
public int LessonId { get; set; }
public LessonEntity Lesson { get; set; }
[Key, Column(Order = 1)]
[ForeignKey(nameof(Paragraph))]
public int LessonPartId { get; set; }
public ParagraphEntity Paragraph { get; set; }

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entities
namespace Entities.SQLudeoDB
{
public class InquiryEntity
{

@ -1,7 +1,7 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
namespace Entities
namespace Entities.SQLudeoDB
{
public class InquiryTableEntity
{

@ -4,7 +4,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entities
namespace Entities.SQLudeoDB
{
public class LessonEntity
{

@ -1,4 +1,4 @@
namespace Entities
namespace Entities.SQLudeoDB
{
public class ParagraphEntity
{

@ -2,7 +2,7 @@
using System.ComponentModel.DataAnnotations;
using System.Security.Cryptography.X509Certificates;
namespace Entities
namespace Entities.SQLudeoDB
{
public class SolutionEntity
{

@ -10,11 +10,11 @@ namespace Entities.SQLudeoDB
{
public class SuccessEntity
{
[Key, Column(Order = 0), ForeignKey(nameof(User))]
[ForeignKey(nameof(User))]
public int UserId { get; set; }
public UserEntity User { get; set; }
[Key, Column(Order = 1), ForeignKey(nameof(Inquiry))]
[ForeignKey(nameof(Inquiry))]
public int InquiryId { get; set; }
public InquiryEntity Inquiry { get; set; }
public bool IsFinished { get; set; }

@ -1,5 +1,8 @@
namespace EntityFramework
using System.ComponentModel.DataAnnotations.Schema;
namespace Entities.SQLudeoDB
{
[Table("user")]
public class UserEntity
{
public int Id { get; set; }

@ -1,7 +1,21 @@
namespace Model.Business
{
public class UserDTO
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public bool IsAdmin { get; set; }
public User() { }
public User(int id, string username, string password, string email, bool isAdmin)
{
Id = id;
Username = username;
Password = password;
Email = email;
IsAdmin = isAdmin;
}
}
}

@ -8,5 +8,20 @@ namespace Model.DTO
{
public class UserDTO
{
public int Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Email { get; set; }
public bool IsAdmin { get; set; }
public UserDTO() { }
public UserDTO(int id, string username, string password, string email, bool isAdmin)
{
Id = id;
Username = username;
Password = password;
Email = email;
IsAdmin = isAdmin;
}
}
}

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.ObjectiveC;
using System.Text;
using System.Threading.Tasks;
@ -8,9 +9,9 @@ namespace Model.Mappers
{
public interface IMapper
{
public Object FromEntityToModel();
public Object FromModelToDTO();
public Object FromDTOToModel();
public Object FromModelToEntity();
public Object FromEntityToModel(object o);
public Object FromModelToDTO(object o);
public Object FromDTOToModel(object o);
public Object FromModelToEntity(object o);
}
}

@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace Model.Mappers
{
public class InquiryMapper : IMapper
public class InquiryMapper
{
public object FromDTOToModel()
{

@ -1,31 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Entities.SQLudeoDB;
using Model.Business;
using Model.DTO;
namespace Model.Mappers
{
public class UserMapper : IMapper
public static class UserMapper
{
public object FromDTOToModel()
public static User FromDTOToModel(this UserDTO dto)
{
throw new NotImplementedException();
return new User(dto.Id, dto.Username, dto.Password, dto.Email, dto.IsAdmin);
}
public object FromEntityToModel()
public static User FromEntityToModel(this UserEntity entity)
{
throw new NotImplementedException();
return new User(entity.Id, entity.Username, entity.Password, entity.Email, entity.IsAdmin);
}
public object FromModelToDTO()
public static UserDTO FromModelToDTO(this User user)
{
throw new NotImplementedException();
return new UserDTO(user.Id, user.Username, user.Password, user.Email, user.IsAdmin);
}
public object FromModelToEntity()
public static UserEntity FromModelToEntity(this User user)
{
throw new NotImplementedException();
return new UserEntity(user.Id, user.Username, user.Password, user.Email, user.IsAdmin);
}
}
}

@ -6,4 +6,21 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EntityFramework\Entities.csproj" />
</ItemGroup>
</Project>

@ -6,4 +6,23 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\EntityFramework\Entities.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,41 @@
using DbContextLib;
using Model.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model.Mappers;
using Model.Business;
namespace Services
{
public class UserDataService : IDataService
{
private UserDbContext DbContext { get; set; }
public UserDataService(UserDbContext context)
{
DbContext = context;
context.Database.EnsureCreated();
}
public IEnumerable<InquiryDTO> GetInquiries()
{
throw new NotImplementedException();
}
public UserDTO GetUserById(int id)
{
throw new NotImplementedException();
}
public UserDTO GetUserByUsername(string username)
{
throw new NotImplementedException();
}
public IEnumerable<UserDTO> GetUsers(int page, int number)
{
return DbContext.Users.Skip((page - 1) * number).Take(number).ToList().Select(u => u.FromEntityToModel().FromModelToDTO());
}
}
}

@ -10,7 +10,17 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.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>

@ -10,7 +10,17 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.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>

Loading…
Cancel
Save