diff --git a/Controllers/NotesApi.cs b/Controllers/NotesApi.cs index c6d5379..75d4f17 100644 --- a/Controllers/NotesApi.cs +++ b/Controllers/NotesApi.cs @@ -18,10 +18,13 @@ using IO.Swagger.Attributes; using IO.Swagger.Security; using Microsoft.AspNetCore.Authorization; using IO.Swagger.DTO; -using EPAServeur.Services; using EPAServeur.IServices; using EPAServeur.Models.Notes; using Microsoft.Extensions.Logging; +using Microsoft.EntityFrameworkCore; +using EPAServeur.Exceptions; +using Microsoft.OpenApi.Expressions; +using IO.Swagger.ClientCollaborateur; namespace IO.Swagger.Controllers { @@ -57,10 +60,15 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] public virtual IActionResult DeleteNote([FromRoute][Required]long? idNote) { - //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)); - logger.LogInformation("Suppresion de la note {idNote}", idNote); - if (!noteService.SupprimerNote(idNote)) + //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)); + try + { + logger.LogInformation("Suppresion de la note {idNote}", idNote); + noteService.SupprimerNote(idNote); + + } + catch(NoteNotFoundException) { logger.LogError("Impossible de supprimer la note d'id {idNote} car elle n'existe pas", idNote); ErreurDTO erreur = new ErreurDTO() @@ -69,7 +77,18 @@ namespace IO.Swagger.Controllers Message = "Aucune note trouvé" }; return NotFound(erreur); - }; + } + catch(DbUpdateConcurrencyException) { + logger.LogWarning("La note {idNote} n'a pas pu être supprimé car elle était prise par une autre ressource.", idNote); + } + catch (DbUpdateException) + { + logger.LogError("Une erreur a eu lieu, la note {idNote} n'a pas pu être supprimée.", idNote); + } + catch(Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la suppression de la note {idNote}",idNote); + } logger.LogInformation("Note {idNote} supprimée avec sucès", idNote); return NoContent(); } @@ -95,17 +114,34 @@ namespace IO.Swagger.Controllers //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)); logger.LogInformation("Récupération de la note {idNote}", idNote); - DetailsNoteDTO note = noteService.GetNoteById(idNote); - if(note == null) + DetailsNoteDTO note = null; + try + { + note = noteService.GetNoteById(idNote); + } + catch(NoteNotFoundException) { - logger.LogError("Aucune note ne correspond à l'id {idNote} cherché", idNote); + logger.LogError("Aucune note ne correspond à l'id {idNote} recherchée", idNote); ErreurDTO erreur = new ErreurDTO() { Code = "404", Message = "Aucune note n'a été trouvée" }; return NotFound(erreur); + } + catch (DbUpdateConcurrencyException) + { + logger.LogWarning("La note {idNote} n'a pas pu être récupérée car elle était prise par une autre ressource.", idNote); + } + catch (DbUpdateException) + { + logger.LogError("Une erreur a eu lieu, la note {idNote} n'a pas pu être récupérée.", idNote); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération de la note {idNote}", idNote); + } logger.LogInformation("Note d'id {idNote} trouvée",idNote); return Ok(note); } @@ -161,11 +197,19 @@ namespace IO.Swagger.Controllers [SwaggerResponse(statusCode: 404, type: typeof(ErreurDTO), description: "Ressource n'a pas été trouvée")] 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)); - logger.LogInformation("Récupération des notes de l'auteur d'id {idReferent}", idReferent); - IEnumerable notes = noteService.GetNotesByAuteur(idReferent, asc, numPage, parPAge, texte, tri); - if(notes == null) + //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)); + logger.LogInformation("Récupération des notes de l'auteur d'id {idReferent}", idReferent); + IEnumerable notes = null; + try + { + notes = noteService.GetNotesByAuteur(idReferent, asc, numPage, parPAge, texte, tri); + } + catch (ApiException) + { + logger.LogError("Une erreur est survenue lors de la discussion avec le service Collaborateur"); + } + catch (ReferentNotFoundException) { logger.LogError("Le référent {idReferent} n'a pas été trouvé", idReferent); ErreurDTO erreur = new ErreurDTO() @@ -174,8 +218,20 @@ namespace IO.Swagger.Controllers Message = "Aucun id ne correspond au référent" }; return NotFound(erreur); - } - logger.LogInformation("Liste des notes de l'auteur {idReferent} trouvée", idReferent); + } + catch (DbUpdateConcurrencyException) + { + logger.LogWarning("Les notes du référents {idReferent} n'ont pas pu être récupérées car une ou plusieurs notes sont prises par une autre ressource.", idReferent); + } + catch (DbUpdateException) + { + logger.LogError("Une erreur dans la base de données est survenue lors de la récupération des notes du référent {idRefrent}", idReferent); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue est survenue lors de la récupération des notes du référebt {idReferent}", idReferent); + } + logger.LogInformation("Liste des notes de l'auteur {idReferent} récupérée", idReferent); return Ok(notes); } @@ -206,17 +262,48 @@ namespace IO.Swagger.Controllers //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)); logger.LogInformation("Récupération des notes de l'auteur {idReferent} sur un collaborateur {idCollaborateur}", idReferent, idCollaborateur); - IEnumerable notes = noteService.GetNotesByCollaborateur(idReferent, idCollaborateur, asc, numPage, parPAge, texte, tri); - if (notes == null) + IEnumerable notes = null; + try + { + notes = noteService.GetNotesByCollaborateur(idReferent, idCollaborateur, asc, numPage, parPAge, texte, tri); + + } + catch (ApiException) { - logger.LogInformation("auteur {idReferent} ou collaborateur {idCollaborateur} introuvable", idReferent, idCollaborateur); + logger.LogError("Une erreur est survenue lors de la discussion avec le service Collaborateur"); + } + catch (CollaborateurNotFoundException) + { + logger.LogError("Le référent {idReferent} n'a pas été trouvé", idReferent); + ErreurDTO erreur = new ErreurDTO() + { + Code = "404", + Message = "Aucun id ne correspond au référent" + }; + return NotFound(erreur); + } + catch(ReferentNotFoundException) + { + logger.LogError("Le collaborateur {idCollaborateur} n'a pas été trouvé", idCollaborateur); ErreurDTO erreur = new ErreurDTO() { Code = "404", - Message = "Aucun id ne correspond au référent et/ou au collaborateur" + Message = "Aucun id ne correspond au collaborateur" }; return NotFound(erreur); } + catch (DbUpdateConcurrencyException) + { + logger.LogWarning("Les notes du référents {idReferent} n'ont pas pu être récupérées pour le collaborateur {idCollaborateur} car une ou plusieurs notes sont prises par une autre ressource", idReferent, idCollaborateur); + } + catch (DbUpdateException) + { + logger.LogError("Une erreur a eu lieu dans la base de données lors de la récupération des notes du référent {idReferent} pour le collaborateur {idCollaborateur}.", idReferent, idCollaborateur); + } + catch (Exception) + { + logger.LogError("Une erreur inconnue a eu lieu lors de la récupération des notes du référent { idReferent} pour le collaborateur { idCollaborateur}.", idReferent, idCollaborateur); + } logger.LogInformation("Liste des notes de l'auteur {idReferent} sur un collaborateur {idCollaborateur} trouvée", idReferent, idCollaborateur); return Ok(notes); } @@ -239,7 +326,14 @@ namespace IO.Swagger.Controllers //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)); logger.LogInformation("Ajout d'une nouvelle note"); - Note nouvelleNote = noteService.AjouterNote(body); + DetailsNoteDTO nouvelleNote = null; + try + { + nouvelleNote = noteService.AjouterNote(body); + } + catch (NoteInvalideException) { logger.LogWarning("Des données sont manquants, la nouvelle note ne peut pas être ajoutée"); } + catch (DbUpdateException) { logger.LogError("Une erreur est survenue dans la base de données durant l'ajout de la nouvelle note"); } + catch(Exception) { logger.LogError("Une erreur inconnue est survenue lors de l'ajout de la nouvelle note"); } logger.LogInformation("Nouvelle note ajoutée"); return Created("",nouvelleNote); } @@ -264,17 +358,42 @@ namespace IO.Swagger.Controllers //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)); logger.LogInformation("Mise à jour de la note d'id {idNote}", idNote); - Note note = noteService.UpdateNote(idNote, body); - if (note == null) { - logger.LogInformation(""); - note = noteService.AjouterNote(body); - logger.LogInformation("Ajout effectuer avec succès"); + DetailsNoteDTO note = null; + try + { + note = noteService.UpdateNote(idNote, body); + } + catch(ApiException) + { + logger.LogError("Une erreur est survenue lors de la discussion avec le service Collaborateur"); + } + catch(NoteIdImcompatibleException) + { + logger.LogError("L'id de la note à mettre à jour {body.Id} et l'id de la note avec les nouvelels information {idNote} sont différents", body.Id, idNote); + } + catch(NoteNotFoundException) + { + logger.LogError("La note {idNote} n'a pas pu être mise à jour car elle n'existe pas", idNote); + } + catch (NoteInvalideException) + { + logger.LogWarning("Des données sont manquants, la note {idNote} ne peut pas être mise à jour", idNote); + } + catch (DbUpdateConcurrencyException) + { + logger.LogError("La note {idNote} n'a pas pu être mise à jour car elle été prise par une autre ressource", idNote); + } + catch(DbUpdateException) + { + logger.LogError("Une erreur est survenue dans la base de données lors de la mise à jour de la note {idNote}", idNote); + } + catch(Exception) { + logger.LogError("Une erreur inconnue est survenue lors de la mise à jour de la note {idNote}", idNote); } - else - logger.LogInformation("Update effectué avec succès"); + logger.LogInformation("Update effectué avec succès"); 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); } } -} +} \ No newline at end of file diff --git a/Exceptions/CollaborateurNotFoundException.cs b/Exceptions/CollaborateurNotFoundException.cs new file mode 100644 index 0000000..a753e13 --- /dev/null +++ b/Exceptions/CollaborateurNotFoundException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter quand le collaborateur n'est pas trouvé + /// + public class CollaborateurNotFoundException : Exception + { + } +} diff --git a/Exceptions/NoteIdImcompatibleException.cs b/Exceptions/NoteIdImcompatibleException.cs new file mode 100644 index 0000000..8a3e5f3 --- /dev/null +++ b/Exceptions/NoteIdImcompatibleException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter lorsque l'id de la note avec les données à mettre à jour et l'id de la note à mettre sont différents + /// + public class NoteIdImcompatibleException : Exception + { + } +} diff --git a/Exceptions/NoteInvalideException.cs b/Exceptions/NoteInvalideException.cs new file mode 100644 index 0000000..e85526e --- /dev/null +++ b/Exceptions/NoteInvalideException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter quand l'objet détails note contient des valeurs null + /// + public class NoteInvalideException : Exception + { + } +} diff --git a/Exceptions/NoteNotFoundException.cs b/Exceptions/NoteNotFoundException.cs new file mode 100644 index 0000000..4244e57 --- /dev/null +++ b/Exceptions/NoteNotFoundException.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter quand la note n'a pas été trouvée + /// + public class NoteNotFoundException : Exception + { + + } +} diff --git a/Exceptions/ReferentNotFoundException.cs b/Exceptions/ReferentNotFoundException.cs new file mode 100644 index 0000000..3559ada --- /dev/null +++ b/Exceptions/ReferentNotFoundException.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EPAServeur.Exceptions +{ + /// + /// Exception à jeter quand le référent n'a pas été trouvée + /// + public class ReferentNotFoundException : Exception + { + } +} diff --git a/IServices/INoteService.cs b/IServices/INoteService.cs index ee1c5ce..6b69c12 100644 --- a/IServices/INoteService.cs +++ b/IServices/INoteService.cs @@ -15,8 +15,8 @@ namespace EPAServeur.IServices public IEnumerable GetNotesByAuteur(Guid? idAuteur, bool? asc, int? numPage, int? parPAge, string texte, string tri); public DetailsNoteDTO GetNoteById(long? idNote); public IEnumerable GetNotesByCollaborateur(Guid? idAuteur, Guid? idCollaborateur, bool? asc, int? numPage, int? parPAge, string texte, string tri); - public Note AjouterNote(DetailsNoteDTO nouvelleNote); - public bool SupprimerNote(long? idNote); - public Note UpdateNote(long? idNote, DetailsNoteDTO note); + public DetailsNoteDTO AjouterNote(DetailsNoteDTO nouvelleNote); + public void SupprimerNote(long? idNote); + public DetailsNoteDTO UpdateNote(long? idNote, DetailsNoteDTO note); } } diff --git a/Program.cs b/Program.cs index 6ec487d..65abc7a 100644 --- a/Program.cs +++ b/Program.cs @@ -23,8 +23,8 @@ namespace EPAServeur Host.CreateDefaultBuilder(args) .ConfigureLogging(logging => { - logging.ClearProviders(); - }) + //logging.ClearProviders(); + }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); diff --git a/Services/CollaborateurService.cs b/Services/CollaborateurService.cs index c7bccce..bb7ab43 100644 --- a/Services/CollaborateurService.cs +++ b/Services/CollaborateurService.cs @@ -6,6 +6,7 @@ using IO.Swagger.ModelCollaborateur; using Microsoft.EntityFrameworkCore.ChangeTracking; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices.ComTypes; using System.Threading.Tasks; @@ -113,6 +114,15 @@ namespace EPAServeur.Services /// Renvoyer les collaborateurs du référent dont l'id est passé en paramètre public IEnumerable GetCollaborateursByReferent(Guid? idReferent, bool? asc, int? numPage, int? parPage, string texte, string tri) { + /* + Stopwatch stopwatch = new Stopwatch(); + stopwatch.Start(); + stopwatch.Stop(); + Console.WriteLine("Durée d'exécution numéro 1: {0}", stopwatch.Elapsed.TotalSeconds); + stopwatch.Restart(); + stopwatch.Stop(); + Console.WriteLine("Durée d'exécution numéro 1: {0}", stopwatch.Elapsed.TotalSeconds); + */ Collaborateur referent = collaborateurApi.ChercherCollabId(idReferent); if (referent == null) return null; @@ -150,7 +160,15 @@ namespace EPAServeur.Services /// Renvoie le profil correspondant à l'id passé en paramètre public ProfilDTO GetProfilById(Guid? idCollaborateur) { + //Stopwatch stopwatch = new Stopwatch(); + //stopwatch.Start(); Collaborateur collaborateur = collaborateurApi.ChercherCollabId(idCollaborateur); + //stopwatch.Stop(); + //Console.WriteLine("Durée d'exécution GetProfil: {0}", stopwatch.Elapsed.TotalSeconds); + //stopwatch.Restart(); + //stopwatch.Stop(); + //Console.WriteLine("Durée d'exécution numéro 1: {0}", stopwatch.Elapsed.TotalSeconds); + if (collaborateur == null) return null; return GetProfilDTO(collaborateur); diff --git a/Services/NoteService.cs b/Services/NoteService.cs index 2e4783a..64a59f7 100644 --- a/Services/NoteService.cs +++ b/Services/NoteService.cs @@ -1,4 +1,5 @@ using EPAServeur.Context; +using EPAServeur.Exceptions; using EPAServeur.IServices; using EPAServeur.Models.Notes; using IO.Swagger.DTO; @@ -39,14 +40,14 @@ namespace EPAServeur.Services /// /// La nouvelle note a ajouté en base /// - public Note AjouterNote(DetailsNoteDTO nouvelleNote) + public DetailsNoteDTO AjouterNote(DetailsNoteDTO nouvelleNote) { if (!IsDetailsNoteValide(nouvelleNote)) - return null; + throw new NoteInvalideException(); Note note = DetailsNoteDTOToNouvelleNote(nouvelleNote); context.Note.Add(note); context.SaveChanges(); - return note; + return NoteToDetailSDTO(note); } #region Services @@ -63,8 +64,18 @@ namespace EPAServeur.Services /// Retour la liste des notes à afficher public IEnumerable GetNotesByCollaborateur(Guid? idAuteur, Guid? idCollaborateur, bool? asc, int? numPage, int? parPage, string texte, string tri) { + //Stopwatch stopwatch = new Stopwatch(); + //stopwatch.Start(); + if (collaborateurService.GetProfilById(idAuteur) == null) + throw new ReferentNotFoundException(); + //stopwatch.Stop(); + //Console.WriteLine("Durée d'exécution GetProfil 1: {0}", stopwatch.Elapsed.TotalSeconds); + //stopwatch.Restart(); if (collaborateurService.GetProfilById(idAuteur) == null || collaborateurService.GetProfilById(idCollaborateur) == null) - return null; + throw new CollaborateurNotFoundException(); + //stopwatch.Stop(); + //Console.WriteLine("Durée d'exécution GetProfil 2: {0}", stopwatch.Elapsed.TotalSeconds); + if (texte == null) texte = ""; else @@ -89,11 +100,8 @@ namespace EPAServeur.Services { Note note = context.Note.Find(idNote); if (note == null) - return null; + throw new NoteNotFoundException(); return NoteToDetailSDTO(note); - /*return (from n in context.Note - where n.Id == idNote - select NoteToDetailSDTO(n)).ToList().FirstOrDefault();*/ } public IEnumerable GetNotes(bool? asc, int? numPage, int? parPage, string texte, string tri) @@ -114,7 +122,7 @@ namespace EPAServeur.Services public IEnumerable GetNotesByAuteur(Guid? idAuteur, bool? asc, int? numPage, int? parPage, string texte, string tri) { if (collaborateurService.GetCollaborateurById(idAuteur) == null) - return null; + throw new CollaborateurNotFoundException(); if (texte == null) texte = ""; else @@ -142,14 +150,13 @@ namespace EPAServeur.Services /// /// Id de la note à supprimer /// Si oui ou non la notea bien été supprimé - public bool SupprimerNote(long? idNote) + public void SupprimerNote(long? idNote) { Note note = context.Note.Find(idNote); if (note == null) - return false; + throw new NoteNotFoundException(); context.Remove(note); context.SaveChanges(); - return true; } /// @@ -158,18 +165,18 @@ namespace EPAServeur.Services /// Id de la note à modifier /// /// - public Note UpdateNote(long? idNote, DetailsNoteDTO note) + public DetailsNoteDTO UpdateNote(long? idNote, DetailsNoteDTO note) { + if (idNote != note.Id) + throw new NoteIdImcompatibleException(); if (!IsDetailsNoteValide(note)) - return null; + throw new NoteInvalideException(); Note noteToUpdate = context.Note.Find(idNote); - if (noteToUpdate == null || note.Id != noteToUpdate.Id ) - return AjouterNote(note); noteToUpdate.Titre = note.Titre; noteToUpdate.Texte = note.Texte; noteToUpdate.DateUpdate = DateTime.Now; context.SaveChanges(); - return noteToUpdate; + return NoteToDetailSDTO(noteToUpdate); } ///