diff --git a/.gitignore b/.gitignore index ade385a..af8f539 100644 --- a/.gitignore +++ b/.gitignore @@ -387,4 +387,6 @@ FodyWeavers.xsd .idea/ *.sln.iml +appsettings.Development.json +*.Development *.Development.json \ No newline at end of file diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/Program.cs b/Collaborateur_Epa_Back/espacecollab.backend.api/Program.cs index 48863a6..cd1ba0a 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/Program.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/Program.cs @@ -1,25 +1,18 @@ -var builder = WebApplication.CreateBuilder(args); - -// 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()) +namespace Api { - app.UseSwagger(); - app.UseSwaggerUI(); + public static class Program + { + public static void Main(string[] args) + { + CreateHostBuilder(args).Build().Run(); + } + + public static IHostBuilder CreateHostBuilder(string[] args) => + Host.CreateDefaultBuilder(args) + .ConfigureWebHostDefaults(webBuilder => + { + + webBuilder.UseStartup(); + }); + } } - -app.UseHttpsRedirection(); - -app.UseAuthorization(); - -app.MapControllers(); - -app.Run(); diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs b/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs new file mode 100644 index 0000000..3a242e4 --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs @@ -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(sqlSection); + + SqlOption contextOptions = sqlSection.Get(); + + services.AddControllers(); + services.AddRouting(options => options.LowercaseUrls = true); + + services.AddScoped(); + + 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(); + }); + } + } +} diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/appsettings.json b/Collaborateur_Epa_Back/espacecollab.backend.api/appsettings.json index 10f68b8..2c9c17d 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/appsettings.json +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/appsettings.json @@ -5,5 +5,9 @@ "Microsoft.AspNetCore": "Warning" } }, - "AllowedHosts": "*" + "AllowedHosts": "*", + "Sql": { + "LoadFake": false, + "ConnectionString": "Server=localhost;Database=collaborateur_epa;Uid=root;Pwd=root;" + } } diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/espacecollab.backend.api.csproj b/Collaborateur_Epa_Back/espacecollab.backend.api/espacecollab.backend.api.csproj index 14554a8..19cd919 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/espacecollab.backend.api.csproj +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/espacecollab.backend.api.csproj @@ -14,4 +14,8 @@ + + + + diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/CollaborateurSqlDto.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/CollaborateurSqlDto.cs index b7917d2..3c84b33 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/CollaborateurSqlDto.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/CollaborateurSqlDto.cs @@ -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 { @@ -18,13 +17,14 @@ namespace espacecollab.backend.infrastructure.sql.dtos public string ApsideMail { get; set; } public DateTime ResignationDate { get; set; } public Guid ReferrerId { get; set; } + public Guid BusinessUnitId { get; set; } [ExcludeFromCodeCoverage] 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; Name = name; @@ -39,6 +39,7 @@ namespace espacecollab.backend.infrastructure.sql.dtos ApsideMail = apsideMail; ResignationDate = resignationDate; ReferrerId = referrerId; + BusinessUnitId = businessUnitId; } } } diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/EnumGenre.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/EnumGenre.cs index 83c9bef..2170465 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/EnumGenre.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/EnumGenre.cs @@ -1,4 +1,4 @@ -namespace espacecollab.backend.infrastructure.sql.dtos.Enums +namespace espacecollab.backend.infrastructure.sql.dtos { public enum EnumGenre { diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/EnumStatut.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/EnumStatut.cs index 2db3de5..1f1d737 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/EnumStatut.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/EnumStatut.cs @@ -1,4 +1,4 @@ -namespace espacecollab.backend.infrastructure.sql.dtos.Enums +namespace espacecollab.backend.infrastructure.sql.dtos { public enum EnumStatut { diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/TechnologieSqlDto.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/TechnologieSqlDto.cs index 73f5028..21fa7e5 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/TechnologieSqlDto.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/TechnologieSqlDto.cs @@ -1,4 +1,6 @@ -namespace espacecollab.backend.infrastructure.sql.dtos +using System.Diagnostics.CodeAnalysis; + +namespace espacecollab.backend.infrastructure.sql.dtos { public class TechnologieSqlDto { diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs new file mode 100644 index 0000000..2688dd6 --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs @@ -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 SqlSettings { get; } + + public DbSet? Collaborateur { get; set; } + + public MainDbContext(IOptions sqlSettings) + { + SqlSettings = sqlSettings; + } + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlServer(SqlSettings.Value.ConnectionString); + } + + + } +} diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Options/SqlOption.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Options/SqlOption.cs new file mode 100644 index 0000000..f741343 --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Options/SqlOption.cs @@ -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; + } + } +} diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/CollaborateurRepository.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/CollaborateurRepository.cs new file mode 100644 index 0000000..e0161ec --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/CollaborateurRepository.cs @@ -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().Add(newCollaborateur); + Context.SaveChanges(); + + return Context.Set().FirstOrDefault(c => c.Id == newCollaborateur.Id); + } + + public IList? GetAllCollaborateurs() + => Context.Set().ToList(); + + public IList? GetCollaborateursByBusinessUnit(Guid businessUnitId) + => Context.Set().Where(collaborateur => collaborateur.BusinessUnitId == businessUnitId).ToList(); + + public IList? GetCollaborateursByReferrer(Guid referrerId) + => Context.Set().Where(collaborateur => collaborateur.ReferrerId == referrerId).ToList(); + + public CollaborateurSqlDto? GetCollaborateurById(Guid id) + => Context?.Set().SingleOrDefault(collaborateur => collaborateur.Id == id); + + public CollaborateurSqlDto? GetCollaborateurByApsideMail(string apsideMail) + => Context?.Set().SingleOrDefault(collaborateur => collaborateur.ApsideMail == apsideMail); + + public CollaborateurSqlDto? UpdateCollaborateur(CollaborateurSqlDto newCollaborateur) + { + Context.Set().Update(newCollaborateur); + Context.SaveChanges(); + + return Context.Set().FirstOrDefault(c => c.Id == newCollaborateur.Id); + } + } +} diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/ICollaborateurRepository.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/ICollaborateurRepository.cs new file mode 100644 index 0000000..7271a87 --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/SqlRepo/Interface/ICollaborateurRepository.cs @@ -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? GetAllCollaborateurs(); + CollaborateurSqlDto? GetCollaborateurByApsideMail(string apsideMail); + CollaborateurSqlDto? GetCollaborateurById(Guid id); + IList? GetCollaborateursByBusinessUnit(Guid businessUnitId); + IList? GetCollaborateursByReferrer(Guid referrerId); + CollaborateurSqlDto? UpdateCollaborateur(CollaborateurSqlDto newCollaborateur); + } +} \ No newline at end of file diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/db/db_1_0_0.sql b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/db/db_1_0_0.sql index 344c6bd..5aaeeb4 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/db/db_1_0_0.sql +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/db/db_1_0_0.sql @@ -69,7 +69,7 @@ CREATE TABLE IF NOT EXISTS Fonction( DROP TABLE IF EXISTS Collaborateur; CREATE TABLE IF NOT EXISTS Collaborateur( Id int NOT NULL AUTO_INCREMENT, - Name varchar(100) NOT NULL UNIQUE, + Name varchar(100) NOT NULL, FirstName varchar(100) NOT NULL, BirthDate date NOT NULL, Gender ENUM('MASCULIN','FEMININ','AUTRE') NOT NULL, diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/espacecollab.backend.infrastructure.sql.csproj b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/espacecollab.backend.infrastructure.sql.csproj index 132c02c..d919b65 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/espacecollab.backend.infrastructure.sql.csproj +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/espacecollab.backend.infrastructure.sql.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -6,4 +6,18 @@ enable + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + +