diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/AgenceController.cs b/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/AgenceController.cs new file mode 100644 index 0000000..dc96075 --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/AgenceController.cs @@ -0,0 +1,12 @@ +using espacecollab.backend.appservices; +using espacecollab.backend.appservices.dtos; + +namespace espacecollab.backend.api.Controllers +{ + public class AgenceController : BaseController + { + public AgenceController(AgenceService agenceService) : base(agenceService) + { + } + } +} diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/BaseController.cs b/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/BaseController.cs new file mode 100644 index 0000000..0ec5b6c --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/BaseController.cs @@ -0,0 +1,68 @@ +using espacecollab.backend.appservices.dtos.Interfaces; +using espacecollab.backend.appservices.interfaces; +using Microsoft.AspNetCore.Mvc; + +namespace espacecollab.backend.api.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public abstract class BaseController : Controller where TO : class, IGenericIdApiDto + { + protected IGenericsServices Services { get; } + + protected BaseController(IGenericsServices services) + { + Services = services; + } + + [HttpGet] + public ActionResult> GetAll() + { + return Ok(Services.GetAll()); + } + + [HttpGet("{id:int:min(1)}")] + public ActionResult GetById(uint id) + { + TO? apiDtos = Services.GetById(id); + if (apiDtos == null) + return NotFound(); + + return Ok(apiDtos); + } + + [HttpPost] + public ActionResult Add(TO apiDto) + { + TO? addedApiDto = Services.Add(apiDto); + if (addedApiDto == null) + return Problem(); + + return Ok(addedApiDto); + } + + [HttpDelete("{id:int:min(1)}")] + public ActionResult Delete(uint id) + { + bool isDeleted = Services.Delete(id); + if (!isDeleted) + return Problem(); + + return Ok(); + } + + [HttpPut("{collaborateurId:int:min(1)}")] + public ActionResult Update(uint id, TO apiDto) + { + if (apiDto.Id != id) + return Unauthorized(); + + TO? updatedApiDto = Services.Update(apiDto); + if (updatedApiDto == null) + return Problem(); + + return Ok(updatedApiDto); + + } + } +} diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/CollaborateursController.cs b/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/CollaborateursController.cs index 27cdc9e..d23bff9 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/CollaborateursController.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/Controllers/CollaborateursController.cs @@ -4,71 +4,22 @@ using Microsoft.AspNetCore.Mvc; namespace espacecollab.backend.api.Controllers { - [Route("api/[controller]")] - [ApiController] - public class CollaborateursController : ControllerBase + //[Route("api/[controller]")] + //[ApiController] + public class CollaborateursController : BaseController { private CollaborateursServices CollaborateursServices { get; } - public CollaborateursController(CollaborateursServices collaborateursServices) + public CollaborateursController(CollaborateursServices collaborateursServices) : base(collaborateursServices) { CollaborateursServices = collaborateursServices; } - [HttpGet] - public ActionResult> GetCollaborateurs() - { - return Ok(CollaborateursServices.GetAll()); - } - - [HttpGet("{collaborateurId:int:min(1)}")] - public ActionResult GetCollaborateurById(uint collaborateurId) - { - CollaborateurApiDto? collaborateur = CollaborateursServices.GetById(collaborateurId); - if (collaborateur == null) - return NotFound(); - - return Ok(collaborateur); - } - - [HttpPost] - public ActionResult AddCollaborateur(CollaborateurApiDto collaborateurApi) - { - CollaborateurApiDto? addedCollaborateur = CollaborateursServices.Add(collaborateurApi); - if (addedCollaborateur == null) - return Problem(); - - return Ok(addedCollaborateur); - } - - [HttpDelete("{collaborateurId:int:min(1)}")] - public ActionResult DeleteCollaborateur(uint collaborateurId) - { - bool isCollaborateurDeleted = CollaborateursServices.Delete(collaborateurId); - if (!isCollaborateurDeleted) - return Problem(); - - return Ok(); - } - - [HttpPut("{collaborateurId:int:min(1)}")] - public ActionResult UpdateCollaborateur(uint collaborateurId, CollaborateurApiDto collaborateurApi) - { - if (collaborateurApi.Id != collaborateurId) - return Unauthorized(); - CollaborateurApiDto? updatedCollaborateur = CollaborateursServices.Update(collaborateurApi); - if (updatedCollaborateur == null) - return Problem(); - - return Ok(updatedCollaborateur); - - } - [HttpGet("businessunit/{businessUnitId:int:min(1)}")] public ActionResult> GetCollaborateursByBusinessUnit(uint businessUnitId) { IEnumerable collaborateurs = CollaborateursServices.GetCollaborateursByBusinessUnit(businessUnitId); - if (collaborateurs == null) + if (!collaborateurs.Any()) return NotFound(); return Ok(collaborateurs); @@ -78,7 +29,7 @@ namespace espacecollab.backend.api.Controllers public ActionResult> GetCollaborateursByReferrer(uint referrerId) { IEnumerable collaborateurs = CollaborateursServices.GetCollaborateursByReferrer(referrerId); - if (collaborateurs == null) + if (!collaborateurs.Any()) return NotFound(); return Ok(collaborateurs); diff --git a/Collaborateur_Epa_Back/espacecollab.backend.api/Register.cs b/Collaborateur_Epa_Back/espacecollab.backend.api/Register.cs index b12e464..603e14a 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.api/Register.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.api/Register.cs @@ -12,15 +12,18 @@ internal static class Register public static void InjectDependencies(IServiceCollection services, SqlOption contextOptions) { services.AddScoped(); + services.AddScoped(); if (contextOptions.LoadFake) { services.AddSingleton(); + services.AddSingleton(); } else { services.AddScoped(); services.AddScoped(); + services.AddScoped(); } } } \ No newline at end of file diff --git a/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/AgenceApiDto.cs b/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/AgenceApiDto.cs new file mode 100644 index 0000000..630761b --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/AgenceApiDto.cs @@ -0,0 +1,8 @@ +using espacecollab.backend.appservices.dtos.Interfaces; + +namespace espacecollab.backend.appservices.dtos; + +public record AgenceApiDto(uint Id, string Name) : IGenericIdApiDto +{ + public uint Id { get; set; } = Id; +} \ No newline at end of file diff --git a/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/CollaborateurApiDto.cs b/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/CollaborateurApiDto.cs index af539c5..ceb58c5 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/CollaborateurApiDto.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/CollaborateurApiDto.cs @@ -2,43 +2,9 @@ namespace espacecollab.backend.appservices.dtos { - public class CollaborateurApiDto : IGenericIdApiDto + 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 uint Id { get; set; } - public string Name { get; set; } - public string FirstName { get; set; } - public DateTime BirthDate { get; set; } - public EnumGenreApi Gender { get; set; } - public EnumStatutApi Status { get; set; } - public int ChildrenNumber { get; set; } - public string Address { get; set; } - public string Telephone { get; set; } - public string PersonalMail { get; set; } - public string ApsideMail { get; set; } - public DateTime ResignationDate { get; set; } - public int ReferrerId { get; set; } - public int BusinessUnitId { get; set; } - - public CollaborateurApiDto() - { - } - - public 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) - { - Id = id; - Name = name; - FirstName = firstName; - BirthDate = birthDate; - Gender = gender; - Status = status; - ChildrenNumber = childrenNumber; - Address = address; - Telephone = telephone; - PersonalMail = personalMail; - ApsideMail = apsideMail; - ResignationDate = resignationDate; - ReferrerId = referrerId; - BusinessUnitId = businessUnitId; - } + public uint Id { get; set; } = Id; } } diff --git a/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/Mappers/AgenceMapper.cs b/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/Mappers/AgenceMapper.cs new file mode 100644 index 0000000..bb078b5 --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.appservices.dtos/Mappers/AgenceMapper.cs @@ -0,0 +1,15 @@ +using espacecollab.backend.infrastructure.sql.dtos; + +namespace espacecollab.backend.appservices.dtos.Mappers; + +public static class AgenceMapper +{ + public static AgenceApiDto ToApi(this AgenceSqlDto agenceSqlDto) + { + return new AgenceApiDto((uint)agenceSqlDto.Id, agenceSqlDto.Name); + } + public static AgenceSqlDto ToSql(this AgenceApiDto agenceApiDto) + { + return new AgenceSqlDto((int)agenceApiDto.Id, agenceApiDto.Name); + } +} \ No newline at end of file diff --git a/Collaborateur_Epa_Back/espacecollab.backend.appservices.interfaces/IGenericsServices.cs b/Collaborateur_Epa_Back/espacecollab.backend.appservices.interfaces/IGenericsServices.cs index 5f14f76..eeeddae 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.appservices.interfaces/IGenericsServices.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.appservices.interfaces/IGenericsServices.cs @@ -11,5 +11,7 @@ namespace espacecollab.backend.appservices.interfaces T? GetById(uint id); T? Update(T apiDto); + + bool Delete(uint apiDtoId); } } diff --git a/Collaborateur_Epa_Back/espacecollab.backend.appservices/AgenceService.cs b/Collaborateur_Epa_Back/espacecollab.backend.appservices/AgenceService.cs new file mode 100644 index 0000000..fb7bfef --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.appservices/AgenceService.cs @@ -0,0 +1,14 @@ +using espacecollab.backend.appservices.dtos; +using espacecollab.backend.appservices.dtos.Mappers; +using espacecollab.backend.infrastructure.interfaces; +using espacecollab.backend.infrastructure.sql.dtos; + +namespace espacecollab.backend.appservices; + +public class AgenceService : GenericsServices +{ + public AgenceService(IAgenceRepository agenceRepository) + :base(agenceRepository, AgenceMapper.ToApi, AgenceMapper.ToSql) + { + } +} \ No newline at end of file diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.fake/FakeAgenceRepository.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.fake/FakeAgenceRepository.cs new file mode 100644 index 0000000..5cdd3ad --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.fake/FakeAgenceRepository.cs @@ -0,0 +1,18 @@ +using espacecollab.backend.infrastructure.interfaces; +using espacecollab.backend.infrastructure.sql.dtos; + +namespace espacecollab.backend.infrastructure.fake +{ + public class FakeAgenceRepository : GenericFakeRepository, IAgenceRepository + { + public FakeAgenceRepository() + { + Context = new List + { + new(1, "Tours"), + + new(2, "Clermont-Ferrand") + }; + } + } +} diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/AgenceSqlRepository.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/AgenceSqlRepository.cs new file mode 100644 index 0000000..a5f569b --- /dev/null +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/AgenceSqlRepository.cs @@ -0,0 +1,11 @@ +using espacecollab.backend.infrastructure.interfaces; +using espacecollab.backend.infrastructure.sql.dtos; + +namespace espacecollab.backend.infrastructure.sql.Repository; + +public class AgenceSqlRepository : GenericSqlRepository, IAgenceRepository +{ + public AgenceSqlRepository(MainDbContext context) : base(context) + { + } +} \ No newline at end of file diff --git a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/CollaborateurSqlRepository.cs b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/CollaborateurSqlRepository.cs index 6519c36..f24bf5f 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/CollaborateurSqlRepository.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/CollaborateurSqlRepository.cs @@ -5,8 +5,6 @@ namespace espacecollab.backend.infrastructure.sql.Repository { public class CollaborateurSqlRepository : GenericSqlRepository, ICollaborateurRepository { - private MainDbContext Context { get; } - public CollaborateurSqlRepository(MainDbContext context) : base(context) { Context = context; 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 7b68b69..f1986bf 100644 --- a/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs +++ b/Collaborateur_Epa_Back/espacecollab.backend.infrastructure.sql/Repository/GenericSqlRepository.cs @@ -5,7 +5,7 @@ namespace espacecollab.backend.infrastructure.sql.Repository { public class GenericSqlRepository : IGenericRepository where T : class, IGenericIdSqlDto { - private MainDbContext Context { get; } + protected MainDbContext Context { get; set; } public GenericSqlRepository(MainDbContext context) {