From 1071204c1b61a4e2321ef4a938d52d7c82800b15 Mon Sep 17 00:00:00 2001 From: Clement FERRERE Date: Tue, 7 Dec 2021 12:44:54 +0100 Subject: [PATCH 1/3] fix route put base controller --- .../espacecollab.backend.api/Controllers/BaseController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/BaseController.cs b/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/BaseController.cs index 0ec5b6c..9fcc857 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/BaseController.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/BaseController.cs @@ -51,7 +51,7 @@ namespace espacecollab.backend.api.Controllers return Ok(); } - [HttpPut("{collaborateurId:int:min(1)}")] + [HttpPut("{id:int:min(1)}")] public ActionResult Update(uint id, TO apiDto) { if (apiDto.Id != id) From 209760275b747241f151c45fcdc3a1ba39e0b35d Mon Sep 17 00:00:00 2001 From: Clement FERRERE Date: Thu, 9 Dec 2021 14:05:21 +0100 Subject: [PATCH 2/3] =?UTF-8?q?Fix=20de=20l'utilisation=20de=20la=20BDD,?= =?UTF-8?q?=20reste=20=C3=A0=20revoir=20le=20fonctionnement=20du=20parrain?= =?UTF-8?q?age=20et=20des=20r=C3=A9f=C3=A9rents?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../espacecollab.backend.api/Startup.cs | 6 ++++- .../espacecollab.backend.api/appsettings.json | 4 ++-- .../MainDbContext.cs | 24 ++++++++++++++----- .../Repository/GenericSqlRepository.cs | 5 +++- .../db/db_1_0_0.sql | 16 +++++++++---- ...cecollab.backend.infrastructure.sql.csproj | 8 +------ 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs b/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs index 1231a37..d50198e 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs @@ -1,4 +1,5 @@ using espacecollab.backend.infrastructure.sql.Options; +using System.Text.Json.Serialization; namespace espacecollab.backend.api { @@ -17,7 +18,10 @@ namespace espacecollab.backend.api services.Configure(sqlSection); SqlOption sqlOptions = sqlSection.Get(); - services.AddControllers(); + services.AddControllers().AddJsonOptions(opt => + { + opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); + }); ; services.AddRouting(options => options.LowercaseUrls = true); services.AddEndpointsApiExplorer(); diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/appsettings.json b/Collaborateur_Epa_Back/espacecollab.backend.api/appsettings.json index eb754e8..bc26e00 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/appsettings.json +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/appsettings.json @@ -7,7 +7,7 @@ }, "AllowedHosts": "*", "Sql": { - "LoadFake": true, - "ConnectionString": "server=127.0.0.1;uid=root;pwd=root;database=collaborateur_epa" + "LoadFake": false, + "ConnectionString": "server=localhost;user=root;password=root;database=collaborateur_epa" } } diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs index 8642097..a13400b 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs @@ -1,11 +1,13 @@ -using espacecollab.backend.infrastructure.sql.Options; +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 + public class MainDbContext : DbContext { private IOptions SqlSettings { get; } + private DbSet? collaborateur { get; set; } public MainDbContext(IOptions sqlSettings) { @@ -14,10 +16,20 @@ namespace espacecollab.backend.infrastructure.sql protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { - optionsBuilder.UseSqlServer(SqlSettings.Value.ConnectionString, builder => - { - builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null); - }); + optionsBuilder.UseMySql(SqlSettings.Value.ConnectionString, + ServerVersion.AutoDetect(SqlSettings.Value.ConnectionString)); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder + .Entity() + .Property(e => e.Gender) + .HasConversion(); + modelBuilder + .Entity() + .Property(e => e.Status) + .HasConversion(); } } } diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs index f1986bf..808a605 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs @@ -14,7 +14,10 @@ namespace espacecollab.backend.infrastructure.sql.Repository public virtual T? Add(T entity) { - return Context.Set().Add(entity) as T; + T? addedEntity = Context.Set().Add(entity) as T; + Context.SaveChanges(); + return addedEntity; + } public virtual bool Delete(int entityId) 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 aa2f461..9ca60e7 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 @@ -59,15 +59,17 @@ CREATE TABLE IF NOT EXISTS Collaborateur( Name varchar(100) NOT NULL, FirstName varchar(100) NOT NULL, BirthDate date NOT NULL, - Gender ENUM('MASCULIN','FEMININ','AUTRE') NOT NULL, - Status ENUM('CADRE','NONCADRE','ALTERNANT','STAGIAIRE') NOT NULL, + Gender ENUM('masculin','feminin','autre') NOT NULL DEFAULT 'masculin', + Status ENUM('cadre','noncadre','alternant','stagiaire') NOT NULL DEFAULT 'noncadre', ChildrenNumber smallint NOT NULL, Address varchar(200) NOT NULL, Telephone varchar(15) NOT NULL, PersonalMail varchar(100) NOT NULL, ApsideMail varchar(100) NOT NULL, - ResignationDate date NOT NULL, - ReferrerId int NOT NULL, + ResignationDate date, + BusinessUnitId int NOT NUll, + ReferrerId int, + CONSTRAINT FK_COLLABORATEUR_BUSINESSUNIT FOREIGN KEY (BusinessUnitId) REFERENCES BusinessUnit(Id), CONSTRAINT FK_COLLABORATEUR_PARRAIN FOREIGN KEY (ReferrerId) REFERENCES Collaborateur(Id), PRIMARY KEY (Id) ); @@ -151,3 +153,9 @@ CREATE TABLE IF NOT EXISTS CollaborateurCollaboreProjet( CONSTRAINT FK_COLLABORATEUR_COLLABORE_PROJET FOREIGN KEY (CollaborateurId) REFERENCES Collaborateur(Id), CONSTRAINT FK_PROJET_COLLABORE_COLLABORATEUR FOREIGN KEY (ProjetId) REFERENCES Projet(Id) ); + +INSERT INTO Agence(Id, Name) VALUES (1,'Agence1'); +INSERT INTO BusinessUnit(Id, Name, AgenceId) VALUES (1,'BusinessUnit1',1); +INSERT INTO Collaborateur(Id, Name, FirstName, BirthDate, Gender, Status, ChildrenNumber, Address, Telephone, PersonalMail, + ApsideMail, ResignationDate, BusinessUnitId, ReferrerId) +VALUES (1,'Collab1','Collab1','2000-30-08','masculin','cadre',0,'adresse','tel','pmail','amail',NULL,1,NULL); \ No newline at end of file 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 66c4cb9..d2b3a93 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 @@ -7,13 +7,7 @@ - - - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - - + From 43f790493d02784e9a120a7f3452e55e25b0d4ef Mon Sep 17 00:00:00 2001 From: Clement FERRERE Date: Wed, 29 Dec 2021 10:13:08 +0100 Subject: [PATCH 3/3] Multiples fix pour permettre au front d'utiliser l'API --- .../espacecollab.backend.api/Register.cs | 1 + .../espacecollab.backend.api/Startup.cs | 4 +++- .../espacecollab.backend.api.csproj | 3 ++- .../CollaborateurApiDto.cs | 4 ++-- .../Mappers/CollaborateurApiDtoMapper.cs | 9 +++++---- .../espacecollab.backend.appservices/GenericsServices.cs | 4 ---- .../CollaborateurSqlDto.cs | 2 ++ .../MainDbContext.cs | 6 ++++-- .../Repository/GenericSqlRepository.cs | 6 +++--- 9 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/Register.cs b/Collaborateur_Epa_Back/espacecollab.backend.api/Register.cs index 603e14a..4616de7 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/Register.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/Register.cs @@ -11,6 +11,7 @@ internal static class Register { public static void InjectDependencies(IServiceCollection services, SqlOption contextOptions) { + services.AddCors(); services.AddScoped(); services.AddScoped(); diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs b/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs index d50198e..e7ee256 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/Startup.cs @@ -37,7 +37,9 @@ namespace espacecollab.backend.api { app.UseDeveloperExceptionPage(); } - + app.UseCors( + options => options.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader() + ); app.UseSwagger(); app.UseSwaggerUI(); 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 eacb4b7..0c9099a 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/espacecollab.backend.api.csproj +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/espacecollab.backend.api.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -7,6 +7,7 @@ + diff --git a/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/CollaborateurApiDto.cs b/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/CollaborateurApiDto.cs index ceb58c5..914f775 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/CollaborateurApiDto.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/CollaborateurApiDto.cs @@ -2,8 +2,8 @@ namespace espacecollab.backend.appservices.dtos { - public record CollaborateurApiDto(uint Id, string Name, string FirstName, DateTime BirthDate, EnumGenreApi Gender, EnumStatutApi Status, int ChildrenNumber, string Address, - string Telephone, string PersonalMail, string ApsideMail, DateTime ResignationDate, int ReferrerId, int BusinessUnitId) : IGenericIdApiDto + public record CollaborateurApiDto(uint Id, string Name, string FirstName, string BirthDate, EnumGenreApi Gender, EnumStatutApi Status, int ChildrenNumber, string Address, + string Telephone, string PersonalMail, string ApsideMail, string ResignationDate, int ReferrerId, int BusinessUnitId) : IGenericIdApiDto { public uint Id { get; set; } = Id; } diff --git a/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/Mappers/CollaborateurApiDtoMapper.cs b/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/Mappers/CollaborateurApiDtoMapper.cs index b604f55..11ab39e 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/Mappers/CollaborateurApiDtoMapper.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/Mappers/CollaborateurApiDtoMapper.cs @@ -1,4 +1,5 @@ using espacecollab.backend.infrastructure.sql.dtos; +using System.Globalization; namespace espacecollab.backend.appservices.dtos.Mappers { @@ -7,17 +8,17 @@ namespace espacecollab.backend.appservices.dtos.Mappers public static CollaborateurApiDto ToApi(this CollaborateurSqlDto collaborateurSql) { return new CollaborateurApiDto((uint)collaborateurSql.Id, collaborateurSql.Name, collaborateurSql.FirstName, - collaborateurSql.BirthDate, collaborateurSql.Gender.ToEnumGenreApi(), collaborateurSql.Status.ToEnumStatutApi(), collaborateurSql.ChildrenNumber, + collaborateurSql.BirthDate.ToShortDateString(), collaborateurSql.Gender.ToEnumGenreApi(), collaborateurSql.Status.ToEnumStatutApi(), collaborateurSql.ChildrenNumber, collaborateurSql.Address, collaborateurSql.Telephone, collaborateurSql.PersonalMail, collaborateurSql.ApsideMail, - collaborateurSql.ResignationDate, collaborateurSql.ReferrerId, collaborateurSql.BusinessUnitId); + collaborateurSql.ResignationDate.ToShortDateString(), collaborateurSql.ReferrerId, collaborateurSql.BusinessUnitId); } public static CollaborateurSqlDto ToSql(this CollaborateurApiDto collaborateurApi) { return new CollaborateurSqlDto((int)collaborateurApi.Id, collaborateurApi.Name, collaborateurApi.FirstName, - collaborateurApi.BirthDate, collaborateurApi.Gender.ToEnumGenreSql(), collaborateurApi.Status.ToEnumStatutSql(), collaborateurApi.ChildrenNumber, + DateTime.ParseExact(collaborateurApi.BirthDate,"dd/MM/yyyy",CultureInfo.InvariantCulture), collaborateurApi.Gender.ToEnumGenreSql(), collaborateurApi.Status.ToEnumStatutSql(), collaborateurApi.ChildrenNumber, collaborateurApi.Address, collaborateurApi.Telephone, collaborateurApi.PersonalMail, collaborateurApi.ApsideMail, - collaborateurApi.ResignationDate, collaborateurApi.ReferrerId, collaborateurApi.BusinessUnitId); + DateTime.ParseExact(collaborateurApi.ResignationDate, "dd/MM/yyyy", CultureInfo.InvariantCulture), collaborateurApi.ReferrerId, collaborateurApi.BusinessUnitId); } } } diff --git a/Collaborateur_Epa_Back/espacecollab.backend.appservices/GenericsServices.cs b/Collaborateur_Epa_Back/espacecollab.backend.appservices/GenericsServices.cs index d1c65c2..9a0e774 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.appservices/GenericsServices.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.appservices/GenericsServices.cs @@ -47,10 +47,6 @@ namespace espacecollab.backend.appservices public TO? Update(TO apiDto) { - if (GetById(apiDto.Id) == null) - { - return null; - } T sqlDto = MapperToSqlDto(apiDto); T? sqlDtoValidation = GenericRepository.Update(sqlDto); 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 e1adf0f..9226760 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/CollaborateurSqlDto.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql.dtos/CollaborateurSqlDto.cs @@ -1,11 +1,13 @@ using espacecollab.backend.infrastructure.sql.dtos.Values; using System.Diagnostics.CodeAnalysis; using espacecollab.backend.infrastructure.sql.dtos.Interfaces; +using System.ComponentModel.DataAnnotations; namespace espacecollab.backend.infrastructure.sql.dtos { public class CollaborateurSqlDto : IGenericIdSqlDto { + [Key] public int Id { get; set; } public string Name { get; set; } public string FirstName { get; set; } diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs index a13400b..247426c 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/MainDbContext.cs @@ -7,7 +7,7 @@ namespace espacecollab.backend.infrastructure.sql public class MainDbContext : DbContext { private IOptions SqlSettings { get; } - private DbSet? collaborateur { get; set; } + private DbSet? Collaborateur { get; set; } public MainDbContext(IOptions sqlSettings) { @@ -17,11 +17,13 @@ namespace espacecollab.backend.infrastructure.sql protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { optionsBuilder.UseMySql(SqlSettings.Value.ConnectionString, - ServerVersion.AutoDetect(SqlSettings.Value.ConnectionString)); + ServerVersion.AutoDetect(SqlSettings.Value.ConnectionString)) + .EnableSensitiveDataLogging(); } protected override void OnModelCreating(ModelBuilder modelBuilder) { + modelBuilder.Entity().Property(p=> p.Id).UseMySqlIdentityColumn(); modelBuilder .Entity() .Property(e => e.Gender) diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs index 808a605..77d9023 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs @@ -1,5 +1,6 @@ using espacecollab.backend.infrastructure.interfaces; using espacecollab.backend.infrastructure.sql.dtos.Interfaces; +using Microsoft.EntityFrameworkCore; namespace espacecollab.backend.infrastructure.sql.Repository { @@ -14,9 +15,9 @@ namespace espacecollab.backend.infrastructure.sql.Repository public virtual T? Add(T entity) { - T? addedEntity = Context.Set().Add(entity) as T; + Context.Set().Add(entity); Context.SaveChanges(); - return addedEntity; + return entity; } @@ -43,7 +44,6 @@ namespace espacecollab.backend.infrastructure.sql.Repository { Context.Set().Update(entity); Context.SaveChanges(); - return Context.Set().FirstOrDefault(e => e.Id == entity.Id); } }