Optimisation du code du service des notes et mise à jour et validation des tests unitaires

develop
Yanaël GRETTE 3 years ago
parent f13fcc25b7
commit db66711b77
  1. 51
      EPAServeur.Tests/Services/NoteServiceTests.cs
  2. 50
      EPAServeur/Services/NoteService.cs

@ -415,15 +415,16 @@ namespace EPAServeur.Tests.Services
note.Titre = "Titre à jour " + id;
note.Texte= "Texte à jour " + id;
DetailsNoteDTO noteUpdate = await noteService.UpdateNoteAsync(id, note);
Note noteUpdated = context.Note.Find(noteUpdate.Id);
Assert.IsNotNull(noteUpdate);
Assert.AreEqual(note.Id, noteUpdate.Id);
Assert.AreEqual(note.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(note.Collaborateur.Id, noteUpdate.Collaborateur.Id);
Assert.AreEqual(note.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(note.Titre, noteUpdate.Titre);
Assert.AreEqual(note.Texte, noteUpdate.Texte);
Assert.AreNotEqual(note.DateMiseAjour, noteUpdate.DateMiseAjour);
Assert.IsNotNull(noteUpdated);
Assert.AreEqual(noteUpdated.IdNote, noteUpdate.Id);
Assert.AreEqual(noteUpdated.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(noteUpdated.IdCollaborateur, noteUpdate.Collaborateur.Id);
Assert.AreEqual(noteUpdated.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(noteUpdated.Titre, noteUpdate.Titre);
Assert.AreEqual(noteUpdated.Texte, noteUpdate.Texte);
Assert.AreEqual(noteUpdated.DateMiseAJour, noteUpdated.DateMiseAJour);
}
[TestCase(1)]
@ -436,15 +437,16 @@ namespace EPAServeur.Tests.Services
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Texte = "Texte à jour " + id;
DetailsNoteDTO noteUpdate = await noteService.UpdateNoteAsync(id, note);
Note noteUpdated = context.Note.Find(noteUpdate.Id);
Assert.IsNotNull(noteUpdate);
Assert.AreEqual(note.Id, noteUpdate.Id);
Assert.AreEqual(note.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(note.Collaborateur.Id, noteUpdate.Collaborateur.Id);
Assert.AreEqual(note.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(note.Titre, noteUpdate.Titre);
Assert.AreEqual(note.Texte, noteUpdate.Texte);
Assert.AreNotEqual(note.DateMiseAjour, noteUpdate.DateMiseAjour);
Assert.IsNotNull(noteUpdated);
Assert.AreEqual(noteUpdated.IdNote, noteUpdate.Id);
Assert.AreEqual(noteUpdated.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(noteUpdated.IdCollaborateur, noteUpdate.Collaborateur.Id);
Assert.AreEqual(noteUpdated.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(noteUpdated.Titre, noteUpdate.Titre);
Assert.AreEqual(noteUpdated.Texte, noteUpdate.Texte);
Assert.AreEqual(noteUpdated.DateMiseAJour, noteUpdated.DateMiseAJour);
}
[TestCase(1)]
@ -457,15 +459,16 @@ namespace EPAServeur.Tests.Services
DetailsNoteDTO note = await noteService.GetNoteByIdAsync(id);
note.Titre = "Titre à jour " + id;
DetailsNoteDTO noteUpdate = await noteService.UpdateNoteAsync(id, note);
Note noteUpdated = context.Note.Find(noteUpdate.Id);
Assert.IsNotNull(noteUpdate);
Assert.AreEqual(note.Id, noteUpdate.Id);
Assert.AreEqual(note.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(note.Collaborateur.Id, noteUpdate.Collaborateur.Id);
Assert.AreEqual(note.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(note.Titre, noteUpdate.Titre);
Assert.AreEqual(note.Texte, noteUpdate.Texte);
Assert.AreNotEqual(note.DateMiseAjour, noteUpdate.DateMiseAjour);
Assert.IsNotNull(noteUpdated);
Assert.AreEqual(noteUpdated.IdNote, noteUpdate.Id);
Assert.AreEqual(noteUpdated.IdAuteur, noteUpdate.IdAuteur);
Assert.AreEqual(noteUpdated.IdCollaborateur, noteUpdate.Collaborateur.Id);
Assert.AreEqual(noteUpdated.DateCreation, noteUpdate.DateCreation);
Assert.AreEqual(noteUpdated.Titre, noteUpdate.Titre);
Assert.AreEqual(noteUpdated.Texte, noteUpdate.Texte);
Assert.AreEqual(noteUpdated.DateMiseAJour, noteUpdated.DateMiseAJour);
}
[TestCase(1,2)]

@ -42,7 +42,7 @@ namespace EPAServeur.Services
}
#endregion
#region Services Async
/// <summary>
/// Ajouter une nouvelle note dans la base de données de manière async
@ -53,14 +53,17 @@ namespace EPAServeur.Services
{
//vérifier qu'il n'y a aucune valeur null
CheckNoteValide(nouvelleNote);
List<Guid?> guids = new List<Guid?>();
guids.AddRange(new Guid?[]{nouvelleNote.IdAuteur, nouvelleNote.Collaborateur.Id});
IEnumerable<Collaborateur> collaborateurs = await collaborateurApi.ChercherCollabAsync(guids);
//vérifier que le référent existe
Collaborateur auteur = await collaborateurApi.ChercherCollabIdAsync((Guid?)nouvelleNote.IdAuteur);
CheckReferent(auteur);
CheckReferent(collaborateurs.Where(c => c.Id.Equals(nouvelleNote.IdAuteur)).FirstOrDefault());
//vérifier que le collaborateur existe
Collaborateur collaborateur = await collaborateurApi.ChercherCollabIdAsync(nouvelleNote.Collaborateur.Id);
CheckCollaborateur(collaborateur);
CheckCollaborateur(collaborateurs.Where(c => c.Id.Equals(nouvelleNote.Collaborateur.Id)).FirstOrDefault());
//transformer la note DTO en Note
Note note = DetailsNoteDTOToNouvelleNote(nouvelleNote);
@ -70,6 +73,7 @@ namespace EPAServeur.Services
await context.SaveChangesAsync();
nouvelleNote.Id = note.IdNote;
return nouvelleNote;
}
/// <summary>
@ -107,7 +111,7 @@ namespace EPAServeur.Services
if (auteur == null)
throw new ReferentNotFoundException("L'auteur de la note n'existe pas");
return await NoteToDetailSDTOAsync(note);
return NoteToDetailSDTO(note, await collaborateurService.GetCollaborateurByIdAsync(note.IdCollaborateur));
}
/// <summary>
@ -122,17 +126,20 @@ namespace EPAServeur.Services
/// <returns>Retour la liste des notes à afficher</returns>
public async Task<IEnumerable<AffichageNoteDTO>> GetNotesByAuteurAsync(Guid? idAuteur, bool? asc, int? numPage, int? parPage, string texte, string tri)
{
Collaborateur auteur = await collaborateurApi.ChercherCollabIdAsync(idAuteur);
if (auteur == null)
throw new ReferentNotFoundException("L'auteur de la note n'existe pas");
IEnumerable<Note> notes = context.Note.Where(n => n.IdAuteur == idAuteur);
List<Guid?> guids = notes.Select(n => (Guid?)n.IdCollaborateur).ToList();
IEnumerable<Collaborateur> collaborateurs = await collaborateurApi.ChercherCollabAsync(guids);
IEnumerable<AffichageNoteDTO> AffichageNoteDTO = notes.Select(note => NoteToAffichageDTO(note, collaborateurs));
int skip = (numPage.Value - 1) * parPage.Value;
int take = parPage.Value;
IEnumerable<Note> notes = context.Note.Where(n => n.IdAuteur == idAuteur);
var task = notes.Select(n => NoteToAffichageDTOAsync(n));
IEnumerable<AffichageNoteDTO> AffichageNoteDTO = await Task.WhenAll(task);
if (texte != null)
{
@ -148,6 +155,7 @@ namespace EPAServeur.Services
AffichageNoteDTO = AffichageNoteDTO.Reverse();
}
return AffichageNoteDTO.Skip(skip).Take(take);
}
/// <summary>
@ -161,11 +169,11 @@ namespace EPAServeur.Services
Collaborateur auteur = await collaborateurApi.ChercherCollabIdAsync(idAuteur);
if (auteur == null)
throw new ReferentNotFoundException("L'auteur de la note n'existe pas");
IEnumerable<Note> notes = context.Note.Where(n => n.IdAuteur == idAuteur);
var task = notes.Select(n => NoteToAffichageDTOAsync(n));
IEnumerable<AffichageNoteDTO> AffichageNoteDTO = await Task.WhenAll(task);
List<Guid?> guids = notes.Select(n => (Guid?)n.IdCollaborateur).ToList();
IEnumerable<Collaborateur> collaborateurs = await collaborateurApi.ChercherCollabAsync(guids);
IEnumerable<AffichageNoteDTO> AffichageNoteDTO = notes.Select(note => NoteToAffichageDTO(note, collaborateurs));
if (texte != null)
{
@ -196,9 +204,8 @@ namespace EPAServeur.Services
noteToUpdate.Titre = note.Titre;
noteToUpdate.Texte = note.Texte;
noteToUpdate.DateMiseAJour = DateTime.Now;
note.DateMiseAjour = DateTime.Now;
await context.SaveChangesAsync();
return await NoteToDetailSDTOAsync(noteToUpdate);
note.DateMiseAjour = noteToUpdate.DateMiseAJour;
return note;
}
#endregion
@ -276,12 +283,12 @@ namespace EPAServeur.Services
/// <param name="note">Note à transformer</param>
/// <param name="collaborateurService">Service collaborateur pour récupérer les informations des collaborateurs</param>
/// <returns>La note transformée pour être affichée</returns>
private async Task<AffichageNoteDTO> NoteToAffichageDTOAsync(Note note)
private AffichageNoteDTO NoteToAffichageDTO(Note note, IEnumerable<Collaborateur> collaborateurs)
{
Collaborateur collaborateur = collaborateurs.FirstOrDefault(c => c.Id.Equals(note.IdCollaborateur));
string infoCollab = "Aucun collaborateur lié";
Collaborateur collaborateur = await collaborateurApi.ChercherCollabIdAsync((Guid?)note.IdCollaborateur);
if (collaborateur != null)
infoCollab = collaborateur.Prenom + " " + collaborateur.Nom;
infoCollab = collaborateur.Nom + " " + collaborateur.Prenom;
AffichageNoteDTO affichage = new AffichageNoteDTO()
{
Id = note.IdNote,
@ -300,9 +307,8 @@ namespace EPAServeur.Services
/// </summary>
/// <param name="note">Note à transformer</param>
/// <returns>Note transformer en DetailsNoteDTO</returns>
private async Task<DetailsNoteDTO> NoteToDetailSDTOAsync(Note note)
private DetailsNoteDTO NoteToDetailSDTO(Note note, CollaborateurDTO collaborateur)
{
CollaborateurDTO collaborateur = await collaborateurService.GetCollaborateurByIdAsync(note.IdCollaborateur);
if (collaborateur == null)
throw new CollaborateurNotFoundException("Il est impossible de récupérer une note donc le collaborateur n'existe plus");
DetailsNoteDTO details = new DetailsNoteDTO()

Loading…
Cancel
Save