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) 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 1231a37..e7ee256 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(); @@ -33,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/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.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 8642097..247426c 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,22 @@ 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)) + .EnableSensitiveDataLogging(); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().Property(p=> p.Id).UseMySqlIdentityColumn(); + 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..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,7 +15,10 @@ namespace espacecollab.backend.infrastructure.sql.Repository public virtual T? Add(T entity) { - return Context.Set().Add(entity) as T; + Context.Set().Add(entity); + Context.SaveChanges(); + return entity; + } public virtual bool Delete(int entityId) @@ -40,7 +44,6 @@ namespace espacecollab.backend.infrastructure.sql.Repository { Context.Set().Update(entity); Context.SaveChanges(); - return Context.Set().FirstOrDefault(e => e.Id == entity.Id); } } 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 - - +