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] =?UTF-8?q?Impl=C3=A9mentation=20de=20l'ajout,=20la=20modi?= =?UTF-8?q?fication=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; }