From 1354101310b0268068859fa2092f4c22c84754ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yana=C3=ABl=20GRETTE?= Date: Fri, 4 Sep 2020 17:36:18 +0200 Subject: [PATCH 1/5] =?UTF-8?q?Ajout=20de=20notes=20dans=20la=20base=20de?= =?UTF-8?q?=20donn=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Context/EpContext.cs | 127 ++++++++++++++++++++++++++++++++++++++ IServices/INoteService.cs | 12 ++++ Models/Notes/Note.cs | 2 + Startup.cs | 1 + 4 files changed, 142 insertions(+) create mode 100644 IServices/INoteService.cs diff --git a/Context/EpContext.cs b/Context/EpContext.cs index b0640c8..998a10b 100644 --- a/Context/EpContext.cs +++ b/Context/EpContext.cs @@ -820,5 +820,132 @@ namespace EPAServeur.Context this.SaveChanges(); } + + public void AjouterNotes() + { + Note note; + note = new Note() + { + Id = 1, + Titre = "Titre1", + Texte = "Texte1", + IdAuteur = "ecf528c3-e509-402f-87bb-c8821467e350", + IdCollaborateur = "779bf1cf-4d38-48fb-8550-3d583384523b", + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now + + }; + this.Note.Add(note); + note = new Note() + { + Id = 2, + Titre = "Titre2", + Texte = "Texte2", + IdAuteur = "ecf528c3-e509-402f-87bb-c8821467e350", + IdCollaborateur = "779bf1cf-4d38-48fb-8550-3d583384523b", + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now + + }; + this.Note.Add(note); + note = new Note() + { + Id = 3, + Titre = "Titre3", + Texte = "Texte3", + IdAuteur = "ecf528c3-e509-402f-87bb-c8821467e350", + IdCollaborateur = "779bf1cf-4d38-48fb-8550-3d583384523b", + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now + + }; + this.Note.Add(note); + note = new Note() + { + Id = 4, + Titre = "Titre4", + Texte = "Texte4", + IdAuteur = "ecf528c3-e509-402f-87bb-c8821467e350", + IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now + + }; + this.Note.Add(note); + note = new Note() + { + Id = 5, + Titre = "Titre5", + Texte = "Texte5", + IdAuteur = "ecf528c3-e509-402f-87bb-c8821467e350", + IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now + + }; + this.Note.Add(note); + note = new Note() + { + Id = 6, + Titre = "Titre6", + Texte = "Texte6", + IdAuteur = "6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb", + IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now + + }; + this.Note.Add(note); + note = new Note() + { + Id = 7, + Titre = "Titre7", + Texte = "Texte7", + IdAuteur = "6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb", + IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now + + }; + this.Note.Add(note); + + note = new Note() + { + Id = 8, + Titre = "Titre8", + Texte = "Texte8", + IdAuteur = "571463f3-b286-4a21-9eab-0707dc506dec", + IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now + + }; + this.Note.Add(note); + note = new Note() + { + Id = 9, + Titre = "Titre9", + Texte = "Texte9", + IdAuteur = "571463f3-b286-4a21-9eab-0707dc506dec", + IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now + + }; + this.Note.Add(note); + note = new Note() + { + Id = 10, + Titre = "Titre10", + Texte = "Texte10", + IdAuteur = "6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb", + IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now + + }; + this.Note.Add(note); + this.SaveChanges(); + } } } diff --git a/IServices/INoteService.cs b/IServices/INoteService.cs new file mode 100644 index 0000000..e3f855f --- /dev/null +++ b/IServices/INoteService.cs @@ -0,0 +1,12 @@ +using IO.Swagger.DTO; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.IServices +{ + interface INoteService + { + } +} diff --git a/Models/Notes/Note.cs b/Models/Notes/Note.cs index 9905843..3de824a 100644 --- a/Models/Notes/Note.cs +++ b/Models/Notes/Note.cs @@ -12,5 +12,7 @@ namespace EPAServeur.Models.Notes public string Texte { get; set; } public string IdAuteur { get; set; } public string IdCollaborateur { get; set; } + public DateTime DateCreation { get; set; } + public DateTime DateUpdate { get; set; } } } diff --git a/Startup.cs b/Startup.cs index 3e5b0c6..bea6aae 100644 --- a/Startup.cs +++ b/Startup.cs @@ -43,6 +43,7 @@ namespace EPAServeur context.SaveChanges(); context.AjoutInformationsDeBase(); context.AjoutChamps(); + context.AjouterNotes(); } //faire using, check si kekchoz exkist puis appeler les méthodes de création si il n'y a rien From e38ef1b7ce069c9c6df25fe11e75885a5e995a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yana=C3=ABl=20GRETTE?= Date: Fri, 4 Sep 2020 17:59:14 +0200 Subject: [PATCH 2/5] Ajout du squelette du service des notes --- IServices/INoteService.cs | 8 +++++ Services/NoteService.cs | 65 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 Services/NoteService.cs diff --git a/IServices/INoteService.cs b/IServices/INoteService.cs index e3f855f..d623438 100644 --- a/IServices/INoteService.cs +++ b/IServices/INoteService.cs @@ -1,4 +1,5 @@ using IO.Swagger.DTO; +using Org.BouncyCastle.Bcpg.OpenPgp; using System; using System.Collections.Generic; using System.Linq; @@ -8,5 +9,12 @@ namespace EPAServeur.IServices { interface INoteService { + public List GetNotes(); + public List GetNotesByAuteur(Guid? idAuteur); + public DetailsNoteDTO GetNoteById(int? idNote); + public List GetNotByCollaborateur(Guid? idAuteur, Guid? idCollaborateur); + public bool AjouterNote(DetailsNoteDTO nouvelleNote); + public bool SupprimerNote(int? idNote); + public void UpdateNote(int? idNote, DetailsNoteDTO note); } } diff --git a/Services/NoteService.cs b/Services/NoteService.cs new file mode 100644 index 0000000..e818d76 --- /dev/null +++ b/Services/NoteService.cs @@ -0,0 +1,65 @@ +using EPAServeur.IServices; +using EPAServeur.Models.Notes; +using IO.Swagger.DTO; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Services +{ + public class NoteService : INoteService + { + public bool AjouterNote(DetailsNoteDTO nouvelleNote) + { + throw new NotImplementedException(); + } + + public List GetNotByCollaborateur(Guid? idAuteur, Guid? idCollaborateur) + { + throw new NotImplementedException(); + } + + public DetailsNoteDTO GetNoteById(int? idNote) + { + throw new NotImplementedException(); + } + + public List GetNotes() + { + throw new NotImplementedException(); + } + + public List GetNotesByAuteur(Guid? idAuteur) + { + throw new NotImplementedException(); + } + + public bool SupprimerNote(int? idNote) + { + throw new NotImplementedException(); + } + + public void UpdateNote(int? idNote, DetailsNoteDTO note) + { + throw new NotImplementedException(); + } + + //Object to DTO + public AffichageNoteDTO NoteToAffichageDTO(Note note) + { + return null; + } + + public DetailsNoteDTO NoteToDetailSDTO(Note note) + { + return null; + } + + //DTO to Object + public Note DetailsNoteDTOToNote(DetailsNoteDTO detailsNoteDTO) + { + return null; + } + } +} From f01fa3099f3733da7a978db8ed0dd1117a122759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yana=C3=ABl=20GRETTE?= Date: Mon, 7 Sep 2020 10:15:31 +0200 Subject: [PATCH 3/5] =?UTF-8?q?Changement=20du=20type=20des=20IdAuteur=20e?= =?UTF-8?q?t=20Collaborateur=20et=20impl=C3=A9mentation=20des=20m=C3=A9tho?= =?UTF-8?q?des=20NoteToDTO?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Models/Notes/Note.cs | 4 ++-- Services/NoteService.cs | 51 +++++++++++++++++++++++++++++++++++------ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/Models/Notes/Note.cs b/Models/Notes/Note.cs index 3de824a..c47fd50 100644 --- a/Models/Notes/Note.cs +++ b/Models/Notes/Note.cs @@ -10,8 +10,8 @@ namespace EPAServeur.Models.Notes public int Id { get; set; } public string Titre { get; set; } public string Texte { get; set; } - public string IdAuteur { get; set; } - public string IdCollaborateur { get; set; } + public Guid IdAuteur { get; set; } + public Guid IdCollaborateur { get; set; } public DateTime DateCreation { get; set; } public DateTime DateUpdate { get; set; } } diff --git a/Services/NoteService.cs b/Services/NoteService.cs index e818d76..880b3a4 100644 --- a/Services/NoteService.cs +++ b/Services/NoteService.cs @@ -1,6 +1,8 @@ -using EPAServeur.IServices; +using EPAServeur.Context; +using EPAServeur.IServices; using EPAServeur.Models.Notes; using IO.Swagger.DTO; +using IO.Swagger.ModelCollaborateur; using System; using System.Collections.Generic; using System.Linq; @@ -10,6 +12,15 @@ namespace EPAServeur.Services { public class NoteService : INoteService { + private readonly CollaborateurService collaborateurService; + private readonly EpContext context; + + public NoteService(CollaborateurService _collaborateurService, EpContext _context) + { + collaborateurService = _collaborateurService; + context = _context; + } + public bool AjouterNote(DetailsNoteDTO nouvelleNote) { throw new NotImplementedException(); @@ -46,20 +57,46 @@ namespace EPAServeur.Services } //Object to DTO - public AffichageNoteDTO NoteToAffichageDTO(Note note) + private AffichageNoteDTO NoteToAffichageDTO(Note note) { - return null; + CollaborateurDTO collaborateur = collaborateurService.GetCollaborateurById(note.IdCollaborateur); + AffichageNoteDTO affichage = new AffichageNoteDTO() + { + Id = note.Id, + Collaborateur = collaborateur.Prenom + collaborateur.Nom, + Titre = note.Titre, + DateMiseAjour = note.DateUpdate + }; + return affichage; } - public DetailsNoteDTO NoteToDetailSDTO(Note note) + private DetailsNoteDTO NoteToDetailSDTO(Note note) { - return null; + DetailsNoteDTO details = new DetailsNoteDTO() + { + Id = note.Id, + DateCreation = note.DateCreation, + DateMiseAjour = note.DateUpdate, + Titre = note.Titre, + Texte = note.Texte, + IdAuteur = note.IdAuteur, + Collaborateur = collaborateurService.GetCollaborateurById(note.IdCollaborateur) + }; + return details; } //DTO to Object - public Note DetailsNoteDTOToNote(DetailsNoteDTO detailsNoteDTO) + private Note DetailsNoteDTOToNouvelleNote(DetailsNoteDTO detailsNoteDTO) { - return null; + Note note = new Note() + { + IdAuteur = detailsNoteDTO.IdAuteur.Value, + IdCollaborateur = detailsNoteDTO.Collaborateur.Id.Value, + Texte = detailsNoteDTO.Texte, + Titre = detailsNoteDTO.Titre, + DateCreation = DateTime.Now + }; + return note; } } } From 4e78c28d10fd63ce3934e205853fc1237dbc93d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yana=C3=ABl=20GRETTE?= Date: Mon, 7 Sep 2020 16:04:30 +0200 Subject: [PATCH 4/5] =?UTF-8?q?R=C3=A9cup=C3=A9ration=20des=20notes=20d'un?= =?UTF-8?q?=20auteur,=20par=20collaborateur=20et=20d'une=20note=20par=20id?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Context/EpContext.cs | 40 +++++------ Controllers/NotesApi.cs | 128 +++++++++++++++++++-------------- IServices/INoteService.cs | 9 +-- Properties/launchSettings.json | 2 +- Services/NoteService.cs | 59 ++++++++++++--- Startup.cs | 3 + 6 files changed, 152 insertions(+), 89 deletions(-) diff --git a/Context/EpContext.cs b/Context/EpContext.cs index 998a10b..9221c12 100644 --- a/Context/EpContext.cs +++ b/Context/EpContext.cs @@ -829,8 +829,8 @@ namespace EPAServeur.Context Id = 1, Titre = "Titre1", Texte = "Texte1", - IdAuteur = "ecf528c3-e509-402f-87bb-c8821467e350", - IdCollaborateur = "779bf1cf-4d38-48fb-8550-3d583384523b", + IdAuteur = new Guid("ecf528c3-e509-402f-87bb-c8821467e350"), + IdCollaborateur = new Guid("779bf1cf-4d38-48fb-8550-3d583384523b"), DateCreation = DateTime.Now, DateUpdate = DateTime.Now @@ -841,8 +841,8 @@ namespace EPAServeur.Context Id = 2, Titre = "Titre2", Texte = "Texte2", - IdAuteur = "ecf528c3-e509-402f-87bb-c8821467e350", - IdCollaborateur = "779bf1cf-4d38-48fb-8550-3d583384523b", + IdAuteur = new Guid("ecf528c3-e509-402f-87bb-c8821467e350"), + IdCollaborateur = new Guid("779bf1cf-4d38-48fb-8550-3d583384523b"), DateCreation = DateTime.Now, DateUpdate = DateTime.Now @@ -853,8 +853,8 @@ namespace EPAServeur.Context Id = 3, Titre = "Titre3", Texte = "Texte3", - IdAuteur = "ecf528c3-e509-402f-87bb-c8821467e350", - IdCollaborateur = "779bf1cf-4d38-48fb-8550-3d583384523b", + IdAuteur = new Guid("ecf528c3-e509-402f-87bb-c8821467e350"), + IdCollaborateur = new Guid("779bf1cf-4d38-48fb-8550-3d583384523b"), DateCreation = DateTime.Now, DateUpdate = DateTime.Now @@ -865,8 +865,8 @@ namespace EPAServeur.Context Id = 4, Titre = "Titre4", Texte = "Texte4", - IdAuteur = "ecf528c3-e509-402f-87bb-c8821467e350", - IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + IdAuteur = new Guid("ecf528c3-e509-402f-87bb-c8821467e350"), + IdCollaborateur = new Guid("006226f6-51b2-4a02-a302-7447f7fccc04"), DateCreation = DateTime.Now, DateUpdate = DateTime.Now @@ -877,8 +877,8 @@ namespace EPAServeur.Context Id = 5, Titre = "Titre5", Texte = "Texte5", - IdAuteur = "ecf528c3-e509-402f-87bb-c8821467e350", - IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + IdAuteur = new Guid("ecf528c3-e509-402f-87bb-c8821467e350"), + IdCollaborateur = new Guid("006226f6-51b2-4a02-a302-7447f7fccc04"), DateCreation = DateTime.Now, DateUpdate = DateTime.Now @@ -889,8 +889,8 @@ namespace EPAServeur.Context Id = 6, Titre = "Titre6", Texte = "Texte6", - IdAuteur = "6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb", - IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + IdAuteur = new Guid("6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb"), + IdCollaborateur = new Guid("006226f6-51b2-4a02-a302-7447f7fccc04"), DateCreation = DateTime.Now, DateUpdate = DateTime.Now @@ -901,8 +901,8 @@ namespace EPAServeur.Context Id = 7, Titre = "Titre7", Texte = "Texte7", - IdAuteur = "6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb", - IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + IdAuteur = new Guid("6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb"), + IdCollaborateur = new Guid("006226f6-51b2-4a02-a302-7447f7fccc04"), DateCreation = DateTime.Now, DateUpdate = DateTime.Now @@ -914,8 +914,8 @@ namespace EPAServeur.Context Id = 8, Titre = "Titre8", Texte = "Texte8", - IdAuteur = "571463f3-b286-4a21-9eab-0707dc506dec", - IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + IdAuteur = new Guid("571463f3-b286-4a21-9eab-0707dc506dec"), + IdCollaborateur = new Guid("006226f6-51b2-4a02-a302-7447f7fccc04"), DateCreation = DateTime.Now, DateUpdate = DateTime.Now @@ -926,8 +926,8 @@ namespace EPAServeur.Context Id = 9, Titre = "Titre9", Texte = "Texte9", - IdAuteur = "571463f3-b286-4a21-9eab-0707dc506dec", - IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + IdAuteur = new Guid("571463f3-b286-4a21-9eab-0707dc506dec"), + IdCollaborateur = new Guid("006226f6-51b2-4a02-a302-7447f7fccc04"), DateCreation = DateTime.Now, DateUpdate = DateTime.Now @@ -938,8 +938,8 @@ namespace EPAServeur.Context Id = 10, Titre = "Titre10", Texte = "Texte10", - IdAuteur = "6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb", - IdCollaborateur = "006226f6-51b2-4a02-a302-7447f7fccc04", + IdAuteur = new Guid("6aa62dcb-f7c9-4c0c-af40-e934a4d6a7eb"), + IdCollaborateur = new Guid("006226f6-51b2-4a02-a302-7447f7fccc04"), DateCreation = DateTime.Now, DateUpdate = DateTime.Now diff --git a/Controllers/NotesApi.cs b/Controllers/NotesApi.cs index 2ad7e8e..da89306 100644 --- a/Controllers/NotesApi.cs +++ b/Controllers/NotesApi.cs @@ -3,7 +3,7 @@ * * API qui sra utilisée afin de faire communiquer le client et le serveur ainsi que le serveur et la boîte noire. * - * OpenAPI spec version: 1.3.2 + * OpenAPI spec version: 1.3.3 * * Generated by: https://github.com/swagger-api/swagger-codegen.git */ @@ -18,6 +18,8 @@ using IO.Swagger.Attributes; using IO.Swagger.Security; using Microsoft.AspNetCore.Authorization; using IO.Swagger.DTO; +using EPAServeur.Services; +using EPAServeur.IServices; namespace IO.Swagger.Controllers { @@ -26,7 +28,14 @@ namespace IO.Swagger.Controllers /// [ApiController] public class NotesApiController : ControllerBase - { + { + private readonly INoteService noteService; + + public NotesApiController(INoteService _noteService) + { + noteService = _noteService; + } + /// /// /// @@ -37,7 +46,7 @@ namespace IO.Swagger.Controllers /// Ressource n'a pas été trouvée [HttpDelete] [Route("/api/notes/{idNote}/supprimer")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("DeleteNote")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] @@ -66,35 +75,38 @@ namespace IO.Swagger.Controllers /// Ressource n'a pas été trouvée [HttpGet] [Route("/api/notes/{idNote}")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetNoteById")] [SwaggerResponse(statusCode: 200, type: typeof(DetailsNoteDTO), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] public virtual IActionResult GetNoteById([FromRoute][Required]int? idNote) - { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(200, default(DetailsNoteDTO)); - + { //TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ... // return StatusCode(403, default(ErreurDTO)); - - //TODO: Uncomment the next line to return response 404 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(404, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "{\n \"idAuteur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"dateCreation\" : \"2000-01-23T04:56:07.000+00:00\",\n \"dateMiseAjour\" : \"2000-01-23T04:56:07.000+00:00\",\n \"titre\" : \"titre\",\n \"texte\" : \"texte\",\n \"id\" : 0,\n \"collaborateur\" : {\n \"businessUnit\" : {\n \"agence\" : {\n \"bu\" : [ null, null ],\n \"id\" : 6.027456183070403,\n \"nom\" : \"nom\"\n },\n \"id\" : 0.8008281904610115,\n \"nom\" : \"nom\"\n },\n \"referent\" : {\n \"mailApside\" : \"\",\n \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"nom\" : \"nom\",\n \"prenom\" : \"prenom\",\n \"collaborateurs\" : [ null, null ]\n },\n \"mailApside\" : \"\",\n \"dateArrivee\" : \"2000-01-23T04:56:07.000+00:00\",\n \"id\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"dateDepart\" : \"2000-01-23T04:56:07.000+00:00\",\n \"nom\" : \"nom\",\n \"prenom\" : \"prenom\"\n }\n}"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject(exampleJson) - : default(DetailsNoteDTO); //TODO: Change the data returned - return new ObjectResult(example); + DetailsNoteDTO note = noteService.GetNoteById(idNote); + if(note == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucune note n'a été trouvée" + }; + return NotFound(erreur); + } + return Ok(note); } /// /// /// /// Récupérer toutes les notes + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// Texte permettant d'identifier l'objet rechercher + /// Colonne du tableau sur lequel le tri s'effectue /// OK /// Acces interdit [HttpGet] @@ -104,7 +116,7 @@ namespace IO.Swagger.Controllers [SwaggerOperation("GetNotes")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] - public virtual IActionResult GetNotes() + public virtual IActionResult GetNotes([FromQuery][Required()]bool? asc, [FromQuery][Required()]int? numPage, [FromQuery][Required()]int? parPAge, [FromQuery]string texte, [FromQuery]string tri) { //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... // return StatusCode(200, default(List)); @@ -124,71 +136,77 @@ namespace IO.Swagger.Controllers /// /// /// Récupérer les notes d'une personne a écrite + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) /// id referent + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// Texte permettant d'identifier l'objet rechercher + /// Colonne du tableau sur lequel le tri s'effectue /// OK /// Acces interdit /// Ressource n'a pas été trouvée [HttpGet] [Route("/api/notes/auteur/{idReferent}")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetNotesByAuteur")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] - public virtual IActionResult GetNotesByAuteur([FromRoute][Required]Guid? idReferent) - { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(200, default(List)); - + public virtual IActionResult GetNotesByAuteur([FromQuery][Required()]bool? asc, [FromRoute][Required]Guid? idReferent, [FromQuery][Required()]int? numPage, [FromQuery][Required()]int? parPAge, [FromQuery]string texte, [FromQuery]string tri) + { //TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(403, default(ErreurDTO)); - - //TODO: Uncomment the next line to return response 404 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(404, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "[ {\n \"idCollaborateur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"dateMiseAjour\" : \"2000-01-23T04:56:07.000+00:00\",\n \"titre\" : \"titre\",\n \"id\" : 0,\n \"collaborateur\" : \"collaborateur\"\n}, {\n \"idCollaborateur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"dateMiseAjour\" : \"2000-01-23T04:56:07.000+00:00\",\n \"titre\" : \"titre\",\n \"id\" : 0,\n \"collaborateur\" : \"collaborateur\"\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + // return StatusCode(403, default(ErreurDTO)); + IEnumerable notes = noteService.GetNotesByAuteur(idReferent, asc, numPage, parPAge, texte, tri); + if(notes == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucun id ne correspond au référent" + }; + return NotFound(erreur); + } + return Ok(notes); } /// /// /// /// Récupérer une note par son id + /// Préciser si les données sont dans l'ordre (true) ou dans l'ordre inverse (false) /// id collaborateur /// id referent + /// Numéro de la page du tableau qui affiche les données + /// Nombre d'éléments affiché sur chaque page du tableau + /// Texte permettant d'identifier l'objet rechercher + /// Colonne du tableau sur lequel le tri s'effectue /// OK /// Acces interdit /// Ressource n'a pas été trouvée [HttpGet] [Route("/api/notes/auteur/{idReferent}/collaborateur/{idCollaborateur}")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("GetNotesByCollaborateur")] [SwaggerResponse(statusCode: 200, type: typeof(List), description: "OK")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] - public virtual IActionResult GetNotesByCollaborateur([FromRoute][Required]Guid? idCollaborateur, [FromRoute][Required]Guid? idReferent) - { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(200, default(List)); - + public virtual IActionResult GetNotesByCollaborateur([FromQuery][Required()]bool? asc, [FromRoute][Required]Guid? idCollaborateur, [FromRoute][Required]Guid? idReferent, [FromQuery][Required()]int? numPage, [FromQuery][Required()]int? parPAge, [FromQuery]string texte, [FromQuery]string tri) + { //TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(403, default(ErreurDTO)); - - //TODO: Uncomment the next line to return response 404 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(404, default(ErreurDTO)); - string exampleJson = null; - exampleJson = "[ {\n \"idCollaborateur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"dateMiseAjour\" : \"2000-01-23T04:56:07.000+00:00\",\n \"titre\" : \"titre\",\n \"id\" : 0,\n \"collaborateur\" : \"collaborateur\"\n}, {\n \"idCollaborateur\" : \"046b6c7f-0b8a-43b9-b35d-6489e6daee91\",\n \"dateMiseAjour\" : \"2000-01-23T04:56:07.000+00:00\",\n \"titre\" : \"titre\",\n \"id\" : 0,\n \"collaborateur\" : \"collaborateur\"\n} ]"; - - var example = exampleJson != null - ? JsonConvert.DeserializeObject>(exampleJson) - : default(List); //TODO: Change the data returned - return new ObjectResult(example); + // return StatusCode(403, default(ErreurDTO)); + IEnumerable notes = noteService.GetNotesByCollaborateur(idReferent, idCollaborateur, asc, numPage, parPAge, texte, tri); + if (notes == null) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucun id ne correspond au référent et/ou au collaborateur" + }; + return NotFound(erreur); + } + return Ok(notes); } /// @@ -200,7 +218,7 @@ namespace IO.Swagger.Controllers /// Acces interdit [HttpPost] [Route("/api/notes/nouvellenote")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("NouvelleNote")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] @@ -226,7 +244,7 @@ namespace IO.Swagger.Controllers /// Acces interdit [HttpPut] [Route("/api/notes/{idNote}/updateNote")] - [Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] + //[Authorize(AuthenticationSchemes = BearerAuthenticationHandler.SchemeName)] [ValidateModelState] [SwaggerOperation("UpdateNote")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] diff --git a/IServices/INoteService.cs b/IServices/INoteService.cs index d623438..5650f08 100644 --- a/IServices/INoteService.cs +++ b/IServices/INoteService.cs @@ -7,12 +7,13 @@ using System.Threading.Tasks; namespace EPAServeur.IServices { - interface INoteService + public interface INoteService { - public List GetNotes(); - public List GetNotesByAuteur(Guid? idAuteur); + //bool? asc, int? numPage, int? parPAge, string texte, string tri) + public IEnumerable GetNotes(bool? asc, int? numPage, int? parPAge, string texte, string tri); + public IEnumerable GetNotesByAuteur(Guid? idAuteur, bool? asc, int? numPage, int? parPAge, string texte, string tri); public DetailsNoteDTO GetNoteById(int? idNote); - public List GetNotByCollaborateur(Guid? idAuteur, Guid? idCollaborateur); + public IEnumerable GetNotesByCollaborateur(Guid? idAuteur, Guid? idCollaborateur, bool? asc, int? numPage, int? parPAge, string texte, string tri); public bool AjouterNote(DetailsNoteDTO nouvelleNote); public bool SupprimerNote(int? idNote); public void UpdateNote(int? idNote, DetailsNoteDTO note); diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index 132903c..9790dad 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -12,7 +12,7 @@ "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, - "launchUrl": "api/collaborateurs/59a8becb-bc0a-4d3d-adb1-8a8bd13c48c9", + "launchUrl": "api/notes/1", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/Services/NoteService.cs b/Services/NoteService.cs index 880b3a4..358bc49 100644 --- a/Services/NoteService.cs +++ b/Services/NoteService.cs @@ -5,6 +5,7 @@ using IO.Swagger.DTO; using IO.Swagger.ModelCollaborateur; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Threading.Tasks; @@ -12,10 +13,10 @@ namespace EPAServeur.Services { public class NoteService : INoteService { - private readonly CollaborateurService collaborateurService; + private readonly ICollaborateurService collaborateurService; private readonly EpContext context; - public NoteService(CollaborateurService _collaborateurService, EpContext _context) + public NoteService(ICollaborateurService _collaborateurService, EpContext _context) { collaborateurService = _collaborateurService; context = _context; @@ -26,24 +27,63 @@ namespace EPAServeur.Services throw new NotImplementedException(); } - public List GetNotByCollaborateur(Guid? idAuteur, Guid? idCollaborateur) + public IEnumerable GetNotesByCollaborateur(Guid? idAuteur, Guid? idCollaborateur, bool? asc, int? numPage, int? parPage, string texte, string tri) { - throw new NotImplementedException(); + if (collaborateurService.GetProfilById(idAuteur) == null || collaborateurService.GetProfilById(idCollaborateur) == null) + return null; + if (texte == null) + texte = ""; + else + texte = texte.ToLower(); + int skip = (numPage.Value - 1) * parPage.Value; + int take = parPage.Value; + IEnumerable AffichageNoteDTO = (from n in context.Note + where n.IdAuteur == idAuteur && n.IdCollaborateur == idCollaborateur + select NoteToAffichageDTO(n, collaborateurService)); + AffichageNoteDTO = (from a in AffichageNoteDTO + where a.Collaborateur.ToLower().Contains(texte) || a.Titre.ToLower().Contains(texte) + select a).Skip(skip).Take(take); + return AffichageNoteDTO; } public DetailsNoteDTO GetNoteById(int? idNote) { - throw new NotImplementedException(); + DetailsNoteDTO details = NoteToDetailSDTO(context.Note.Where(c => c.Id == idNote).FirstOrDefault()); + return details; + /*return (from n in context.Note + where n.Id == idNote + select NoteToDetailSDTO(n)).ToList().FirstOrDefault();*/ } - public List GetNotes() + public IEnumerable GetNotes(bool? asc, int? numPage, int? parPage, string texte, string tri) { throw new NotImplementedException(); } - public List GetNotesByAuteur(Guid? idAuteur) + public IEnumerable GetNotesByAuteur(Guid? idAuteur, bool? asc, int? numPage, int? parPage, string texte, string tri) { - throw new NotImplementedException(); + if (collaborateurService.GetCollaborateurById(idAuteur) == null) + return null; + if (texte == null) + texte = ""; + else + texte = texte.ToLower(); + int skip = (numPage.Value - 1) * parPage.Value; + int take = parPage.Value; + //Stopwatch stopwatch = new Stopwatch(); + //stopwatch.Start(); + IEnumerable AffichageNoteDTO = (from n in context.Note + where n.IdAuteur == idAuteur + select NoteToAffichageDTO(n, collaborateurService)); + //stopwatch.Stop(); + //Console.WriteLine("Durée d'exécution numéro 1: {0}", stopwatch.Elapsed.TotalSeconds); + //stopwatch.Restart(); + AffichageNoteDTO = (from a in AffichageNoteDTO + where a.Collaborateur.ToLower().Contains(texte) || a.Titre.ToLower().Contains(texte) + select a).Skip(skip).Take(take); + //stopwatch.Stop(); + //Console.WriteLine("Durée d'exécution numéro 2: {0}", stopwatch.Elapsed.TotalSeconds); + return AffichageNoteDTO; } public bool SupprimerNote(int? idNote) @@ -57,12 +97,13 @@ namespace EPAServeur.Services } //Object to DTO - private AffichageNoteDTO NoteToAffichageDTO(Note note) + private static AffichageNoteDTO NoteToAffichageDTO(Note note, ICollaborateurService collaborateurService) { CollaborateurDTO collaborateur = collaborateurService.GetCollaborateurById(note.IdCollaborateur); AffichageNoteDTO affichage = new AffichageNoteDTO() { Id = note.Id, + IdCollaborateur = note.IdCollaborateur, Collaborateur = collaborateur.Prenom + collaborateur.Nom, Titre = note.Titre, DateMiseAjour = note.DateUpdate diff --git a/Startup.cs b/Startup.cs index bea6aae..b0bd898 100644 --- a/Startup.cs +++ b/Startup.cs @@ -52,8 +52,11 @@ namespace EPAServeur services.AddScoped(); services.AddScoped(); + + //Services services.AddScoped(); + services.AddScoped(); } From a07a78af876d00707a0f0bfed336d7949b2c7e01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yana=C3=ABl=20GRETTE?= Date: Mon, 7 Sep 2020 18:32:16 +0200 Subject: [PATCH 5/5] =?UTF-8?q?Impl=C3=A9mentation=20de=20l'ajout,=20la=20?= =?UTF-8?q?modification=20et=20la=20suppression=20d'une=20note?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/NotesApi.cs | 43 +++++++++++++++++++------------------- IServices/INoteService.cs | 7 ++++--- Services/NoteService.cs | 44 ++++++++++++++++++++++++++++++++------- 3 files changed, 61 insertions(+), 33 deletions(-) diff --git a/Controllers/NotesApi.cs b/Controllers/NotesApi.cs index da89306..b8e526d 100644 --- a/Controllers/NotesApi.cs +++ b/Controllers/NotesApi.cs @@ -20,6 +20,7 @@ using Microsoft.AspNetCore.Authorization; using IO.Swagger.DTO; using EPAServeur.Services; using EPAServeur.IServices; +using EPAServeur.Models.Notes; namespace IO.Swagger.Controllers { @@ -53,16 +54,18 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] public virtual IActionResult DeleteNote([FromRoute][Required]int? idNote) { - //TODO: Uncomment the next line to return response 204 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(204); - //TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ... // return StatusCode(403, default(ErreurDTO)); - - //TODO: Uncomment the next line to return response 404 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(404, default(ErreurDTO)); - - throw new NotImplementedException(); + if(!noteService.SupprimerNote(idNote)) + { + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucune note trouvé" + }; + return NotFound(erreur); + }; + return NoContent(); } /// @@ -223,14 +226,11 @@ namespace IO.Swagger.Controllers [SwaggerOperation("NouvelleNote")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual IActionResult NouvelleNote([FromBody]DetailsNoteDTO body) - { - //TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(201); - + { //TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ... // return StatusCode(403, default(ErreurDTO)); - - throw new NotImplementedException(); + Note nouvelleNote = noteService.AjouterNote(body); + return Created("",nouvelleNote); } /// @@ -249,17 +249,16 @@ namespace IO.Swagger.Controllers [SwaggerOperation("UpdateNote")] [SwaggerResponse(statusCode: 403, type: typeof(ErreurDTO), description: "Acces interdit")] public virtual IActionResult UpdateNote([FromBody]DetailsNoteDTO body, [FromRoute][Required]int? idNote) - { - //TODO: Uncomment the next line to return response 200 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(200); - - //TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ... - // return StatusCode(201); - + { //TODO: Uncomment the next line to return response 403 or use other options such as return this.NotFound(), return this.BadRequest(..), ... // return StatusCode(403, default(ErreurDTO)); - throw new NotImplementedException(); + Note note = noteService.UpdateNote(idNote, body); + if (note == null) + note = noteService.AjouterNote(body); + return Ok(note); + //TODO: Uncomment the next line to return response 201 or use other options such as return this.NotFound(), return this.BadRequest(..), ... + // return StatusCode(201); } } } diff --git a/IServices/INoteService.cs b/IServices/INoteService.cs index 5650f08..7b2d0ae 100644 --- a/IServices/INoteService.cs +++ b/IServices/INoteService.cs @@ -1,4 +1,5 @@ -using IO.Swagger.DTO; +using EPAServeur.Models.Notes; +using IO.Swagger.DTO; using Org.BouncyCastle.Bcpg.OpenPgp; using System; using System.Collections.Generic; @@ -14,8 +15,8 @@ namespace EPAServeur.IServices public IEnumerable GetNotesByAuteur(Guid? idAuteur, bool? asc, int? numPage, int? parPAge, string texte, string tri); public DetailsNoteDTO GetNoteById(int? idNote); public IEnumerable GetNotesByCollaborateur(Guid? idAuteur, Guid? idCollaborateur, bool? asc, int? numPage, int? parPAge, string texte, string tri); - public bool AjouterNote(DetailsNoteDTO nouvelleNote); + public Note AjouterNote(DetailsNoteDTO nouvelleNote); public bool SupprimerNote(int? idNote); - public void UpdateNote(int? idNote, DetailsNoteDTO note); + public Note UpdateNote(int? idNote, DetailsNoteDTO note); } } diff --git a/Services/NoteService.cs b/Services/NoteService.cs index 358bc49..a900a52 100644 --- a/Services/NoteService.cs +++ b/Services/NoteService.cs @@ -22,9 +22,14 @@ namespace EPAServeur.Services context = _context; } - public bool AjouterNote(DetailsNoteDTO nouvelleNote) + public Note AjouterNote(DetailsNoteDTO nouvelleNote) { - throw new NotImplementedException(); + if (!IsDetailsNoteValide(nouvelleNote)) + return null; + Note note = DetailsNoteDTOToNouvelleNote(nouvelleNote); + context.Note.Add(note); + context.SaveChanges(); + return note; } public IEnumerable GetNotesByCollaborateur(Guid? idAuteur, Guid? idCollaborateur, bool? asc, int? numPage, int? parPage, string texte, string tri) @@ -48,8 +53,10 @@ namespace EPAServeur.Services public DetailsNoteDTO GetNoteById(int? idNote) { - DetailsNoteDTO details = NoteToDetailSDTO(context.Note.Where(c => c.Id == idNote).FirstOrDefault()); - return details; + Note note = context.Note.Find(idNote); + if (note == null) + return null; + return NoteToDetailSDTO(note); /*return (from n in context.Note where n.Id == idNote select NoteToDetailSDTO(n)).ToList().FirstOrDefault();*/ @@ -88,12 +95,32 @@ namespace EPAServeur.Services public bool SupprimerNote(int? idNote) { - throw new NotImplementedException(); + Note note = context.Note.Find(idNote); + if (note == null) + return false; + context.Remove(note); + context.SaveChanges(); + return true; } - public void UpdateNote(int? idNote, DetailsNoteDTO note) + public Note UpdateNote(int? idNote, DetailsNoteDTO note) { - throw new NotImplementedException(); + if (!IsDetailsNoteValide(note)) + return null; + Note noteToUpdate = context.Note.Find(idNote); + if (noteToUpdate == null) + return AjouterNote(note); + noteToUpdate.Titre = note.Titre; + noteToUpdate.Texte = note.Texte; + noteToUpdate.DateUpdate = DateTime.Now; + context.SaveChanges(); + return noteToUpdate; + } + + + private bool IsDetailsNoteValide(DetailsNoteDTO note) + { + return !(note == null || note.IdAuteur == null || note.Collaborateur == null || note.Collaborateur.Id == null || note.Titre == null || note.Texte == null) ; } //Object to DTO @@ -135,7 +162,8 @@ namespace EPAServeur.Services IdCollaborateur = detailsNoteDTO.Collaborateur.Id.Value, Texte = detailsNoteDTO.Texte, Titre = detailsNoteDTO.Titre, - DateCreation = DateTime.Now + DateCreation = DateTime.Now, + DateUpdate = DateTime.Now }; return note; }