You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Digitalisation_EPA_Serveur/Services/NoteService.cs

171 lines
5.3 KiB

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.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace EPAServeur.Services
{
public class NoteService : INoteService
{
private readonly ICollaborateurService collaborateurService;
private readonly EpContext context;
public NoteService(ICollaborateurService _collaborateurService, EpContext _context)
{
collaborateurService = _collaborateurService;
context = _context;
}
public Note AjouterNote(DetailsNoteDTO nouvelleNote)
{
if (!IsDetailsNoteValide(nouvelleNote))
return null;
Note note = DetailsNoteDTOToNouvelleNote(nouvelleNote);
context.Note.Add(note);
context.SaveChanges();
return note;
}
public IEnumerable<AffichageNoteDTO> GetNotesByCollaborateur(Guid? idAuteur, Guid? idCollaborateur, bool? asc, int? numPage, int? parPage, string texte, string tri)
{
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> 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(long? idNote)
{
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();*/
}
public IEnumerable<AffichageNoteDTO> GetNotes(bool? asc, int? numPage, int? parPage, string texte, string tri)
{
throw new NotImplementedException();
}
public IEnumerable<AffichageNoteDTO> GetNotesByAuteur(Guid? idAuteur, bool? asc, int? numPage, int? parPage, string texte, string tri)
{
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> 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(long? idNote)
{
Note note = context.Note.Find(idNote);
if (note == null)
return false;
context.Remove(note);
context.SaveChanges();
return true;
}
public Note UpdateNote(long? idNote, DetailsNoteDTO note)
{
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
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
};
return affichage;
}
private DetailsNoteDTO NoteToDetailSDTO(Note note)
{
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
private Note DetailsNoteDTOToNouvelleNote(DetailsNoteDTO detailsNoteDTO)
{
Note note = new Note()
{
IdAuteur = detailsNoteDTO.IdAuteur.Value,
IdCollaborateur = detailsNoteDTO.Collaborateur.Id.Value,
Texte = detailsNoteDTO.Texte,
Titre = detailsNoteDTO.Titre,
DateCreation = DateTime.Now,
DateUpdate = DateTime.Now
};
return note;
}
}
}