Création du repository collaborateur selon les routes de la doc swagger.

pull/2/head
Clement FERRERE 3 years ago
parent 35cdc15f32
commit fcdff5d566
  1. 2
      .gitignore
  2. 39
      Collaborateur_Epa_Back/espacecollab.backend.api/Program.cs
  3. 56
      Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs
  4. 6
      Collaborateur_Epa_Back/espacecollab.backend.api/appsettings.json
  5. 4
      Collaborateur_Epa_Back/espacecollab.backend.api/espacecollab.backend.api.csproj
  6. 7
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/CollaborateurSqlDto.cs
  7. 2
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/EnumGenre.cs
  8. 2
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/EnumStatut.cs
  9. 4
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/TechnologieSqlDto.cs
  10. 24
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs
  11. 14
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Options/SqlOption.cs
  12. 52
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/CollaborateurRepository.cs
  13. 15
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/ICollaborateurRepository.cs
  14. 2
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/db/db_1_0_0.sql
  15. 16
      Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/espacecollab.backend.infrastructure.sql.csproj

2
.gitignore vendored

@ -387,4 +387,6 @@ FodyWeavers.xsd
.idea/ .idea/
*.sln.iml *.sln.iml
appsettings.Development.json
*.Development
*.Development.json *.Development.json

@ -1,25 +1,18 @@
var builder = WebApplication.CreateBuilder(args); namespace Api
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{ {
app.UseSwagger(); public static class Program
app.UseSwaggerUI(); {
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
} }
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();

@ -0,0 +1,56 @@
using espacecollab.backend.infrastructure.sql;
using espacecollab.backend.infrastructure.sql.Options;
namespace Api
{
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
IConfigurationSection sqlSection = Configuration.GetSection(SqlOption.Key);
services.Configure<SqlOption>(sqlSection);
SqlOption contextOptions = sqlSection.Get<SqlOption>();
services.AddControllers();
services.AddRouting(options => options.LowercaseUrls = true);
services.AddScoped<MainDbContext>();
services.AddEndpointsApiExplorer();
services.AddSwaggerGen();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseSwagger();
app.UseSwaggerUI();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

@ -5,5 +5,9 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"Sql": {
"LoadFake": false,
"ConnectionString": "Server=localhost;Database=collaborateur_epa;Uid=root;Pwd=root;"
}
} }

@ -14,4 +14,8 @@
<Folder Include="Controllers\" /> <Folder Include="Controllers\" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\espacecollab.backend.infrastructure.sql\espacecollab.backend.infrastructure.sql.csproj" />
</ItemGroup>
</Project> </Project>

@ -1,5 +1,4 @@
using espacecollab.backend.infrastructure.sql.dtos.Enums; using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.CodeAnalysis;
namespace espacecollab.backend.infrastructure.sql.dtos namespace espacecollab.backend.infrastructure.sql.dtos
{ {
@ -18,13 +17,14 @@ namespace espacecollab.backend.infrastructure.sql.dtos
public string ApsideMail { get; set; } public string ApsideMail { get; set; }
public DateTime ResignationDate { get; set; } public DateTime ResignationDate { get; set; }
public Guid ReferrerId { get; set; } public Guid ReferrerId { get; set; }
public Guid BusinessUnitId { get; set; }
[ExcludeFromCodeCoverage] [ExcludeFromCodeCoverage]
private CollaborateurSqlDto() private CollaborateurSqlDto()
{ {
} }
public CollaborateurSqlDto(Guid id, string name, string firstName, DateTime birthDate, EnumGenre gender, EnumStatut status, int childrenNumber, string address, string telephone, string personalMail, string apsideMail, DateTime resignationDate, Guid referrerId) public CollaborateurSqlDto(Guid id, string name, string firstName, DateTime birthDate, EnumGenre gender, EnumStatut status, int childrenNumber, string address, string telephone, string personalMail, string apsideMail, DateTime resignationDate, Guid referrerId, Guid businessUnitId)
{ {
Id = id; Id = id;
Name = name; Name = name;
@ -39,6 +39,7 @@ namespace espacecollab.backend.infrastructure.sql.dtos
ApsideMail = apsideMail; ApsideMail = apsideMail;
ResignationDate = resignationDate; ResignationDate = resignationDate;
ReferrerId = referrerId; ReferrerId = referrerId;
BusinessUnitId = businessUnitId;
} }
} }
} }

@ -1,4 +1,4 @@
namespace espacecollab.backend.infrastructure.sql.dtos.Enums namespace espacecollab.backend.infrastructure.sql.dtos
{ {
public enum EnumGenre public enum EnumGenre
{ {

@ -1,4 +1,4 @@
namespace espacecollab.backend.infrastructure.sql.dtos.Enums namespace espacecollab.backend.infrastructure.sql.dtos
{ {
public enum EnumStatut public enum EnumStatut
{ {

@ -1,4 +1,6 @@
namespace espacecollab.backend.infrastructure.sql.dtos using System.Diagnostics.CodeAnalysis;
namespace espacecollab.backend.infrastructure.sql.dtos
{ {
public class TechnologieSqlDto public class TechnologieSqlDto
{ {

@ -0,0 +1,24 @@
using espacecollab.backend.infrastructure.sql.dtos;
using espacecollab.backend.infrastructure.sql.Options;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace espacecollab.backend.infrastructure.sql
{
public class MainDbContext : DbContext
{
private IOptions<SqlOption> SqlSettings { get; }
public DbSet<CollaborateurSqlDto>? Collaborateur { get; set; }
public MainDbContext(IOptions<SqlOption> sqlSettings)
{
SqlSettings = sqlSettings;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(SqlSettings.Value.ConnectionString);
}
}
}

@ -0,0 +1,14 @@
namespace espacecollab.backend.infrastructure.sql.Options
{
public class SqlOption
{
public static string Key => "Sql";
public bool LoadFake { get; set; }
public string ConnectionString { get; set; }
public SqlOption()
{
ConnectionString = string.Empty;
}
}
}

@ -0,0 +1,52 @@
using espacecollab.backend.infrastructure.sql.dtos;
using espacecollab.backend.infrastructure.sql.SqlRepo.Interface;
namespace espacecollab.backend.infrastructure.sql.SqlRepo
{
public class CollaborateurRepository : ICollaborateurRepository
{
private MainDbContext Context { get; }
public CollaborateurRepository(MainDbContext context)
{
Context = context;
}
/* TODO Récupération des collaborateurs par agence directement dans le service Collaborateur */
public CollaborateurSqlDto? AddCollaborateur(CollaborateurSqlDto newCollaborateur)
{
CollaborateurSqlDto? collaborateurAlreadyExists = Context.Collaborateur?.FirstOrDefault(c => c.Id == newCollaborateur.Id);
if (collaborateurAlreadyExists != null)
return null;
Context.Set<CollaborateurSqlDto>().Add(newCollaborateur);
Context.SaveChanges();
return Context.Set<CollaborateurSqlDto>().FirstOrDefault(c => c.Id == newCollaborateur.Id);
}
public IList<CollaborateurSqlDto>? GetAllCollaborateurs()
=> Context.Set<CollaborateurSqlDto>().ToList();
public IList<CollaborateurSqlDto>? GetCollaborateursByBusinessUnit(Guid businessUnitId)
=> Context.Set<CollaborateurSqlDto>().Where(collaborateur => collaborateur.BusinessUnitId == businessUnitId).ToList();
public IList<CollaborateurSqlDto>? GetCollaborateursByReferrer(Guid referrerId)
=> Context.Set<CollaborateurSqlDto>().Where(collaborateur => collaborateur.ReferrerId == referrerId).ToList();
public CollaborateurSqlDto? GetCollaborateurById(Guid id)
=> Context?.Set<CollaborateurSqlDto>().SingleOrDefault(collaborateur => collaborateur.Id == id);
public CollaborateurSqlDto? GetCollaborateurByApsideMail(string apsideMail)
=> Context?.Set<CollaborateurSqlDto>().SingleOrDefault(collaborateur => collaborateur.ApsideMail == apsideMail);
public CollaborateurSqlDto? UpdateCollaborateur(CollaborateurSqlDto newCollaborateur)
{
Context.Set<CollaborateurSqlDto>().Update(newCollaborateur);
Context.SaveChanges();
return Context.Set<CollaborateurSqlDto>().FirstOrDefault(c => c.Id == newCollaborateur.Id);
}
}
}

@ -0,0 +1,15 @@
using espacecollab.backend.infrastructure.sql.dtos;
namespace espacecollab.backend.infrastructure.sql.SqlRepo.Interface
{
public interface ICollaborateurRepository
{
CollaborateurSqlDto? AddCollaborateur(CollaborateurSqlDto newCollaborateur);
IList<CollaborateurSqlDto>? GetAllCollaborateurs();
CollaborateurSqlDto? GetCollaborateurByApsideMail(string apsideMail);
CollaborateurSqlDto? GetCollaborateurById(Guid id);
IList<CollaborateurSqlDto>? GetCollaborateursByBusinessUnit(Guid businessUnitId);
IList<CollaborateurSqlDto>? GetCollaborateursByReferrer(Guid referrerId);
CollaborateurSqlDto? UpdateCollaborateur(CollaborateurSqlDto newCollaborateur);
}
}

@ -69,7 +69,7 @@ CREATE TABLE IF NOT EXISTS Fonction(
DROP TABLE IF EXISTS Collaborateur; DROP TABLE IF EXISTS Collaborateur;
CREATE TABLE IF NOT EXISTS Collaborateur( CREATE TABLE IF NOT EXISTS Collaborateur(
Id int NOT NULL AUTO_INCREMENT, Id int NOT NULL AUTO_INCREMENT,
Name varchar(100) NOT NULL UNIQUE, Name varchar(100) NOT NULL,
FirstName varchar(100) NOT NULL, FirstName varchar(100) NOT NULL,
BirthDate date NOT NULL, BirthDate date NOT NULL,
Gender ENUM('MASCULIN','FEMININ','AUTRE') NOT NULL, Gender ENUM('MASCULIN','FEMININ','AUTRE') NOT NULL,

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
@ -6,4 +6,18 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="MySql.EntityFrameworkCore" Version="5.0.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\espacecollab.backend.infrastructure.sql.dtos\espacecollab.backend.infrastructure.sql.dtos.csproj" />
</ItemGroup>
</Project> </Project>

Loading…
Cancel
Save