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.
ServeurCollaborateurPourEP/ServeurCollaborateur/Service/CollaborateurService.cs

90 lines
2.4 KiB

using IO.Swagger.Models;
using ServeurCollaborateur.IService;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using Newtonsoft.Json;
using System.Threading.Tasks;
namespace ServeurCollaborateur.Service
{
public class CollaborateurService : ICollaborateurService
{
private readonly string fileName = "./collaborateurs.json";
private readonly Dictionary<Guid?, Collaborateur> collaborateurs;
public CollaborateurService()
{
string jsonString = File.ReadAllText(fileName);
IEnumerable<Collaborateur> _collaborateurs = JsonConvert.DeserializeObject<IEnumerable<Collaborateur>>(jsonString);
collaborateurs = new Dictionary<Guid?, Collaborateur>();
foreach(Collaborateur c in _collaborateurs)
{
collaborateurs.Add(c.Id, c);
Console.WriteLine(c.Nom + " " + c.Prenom);
}
}
public Collaborateur GetCollaborateurById(Guid? id)
{
bool trouve = collaborateurs.TryGetValue(id, out Collaborateur collaborateur);
if (!trouve)
{
throw new Exception("Collaborateur not found");
}
return collaborateur;
}
public Collaborateur GetCollaborateurByMail(string mail)
{
Collaborateur collaborateur = collaborateurs.Values.Where(c => c.MailApside.Equals(mail)).FirstOrDefault();
if (collaborateur == null)
{
throw new Exception("Collaborateur not found");
}
return collaborateur;
}
public IEnumerable<Collaborateur> GetCollaborateurs(List<Guid?> collabsId, bool? ancienCollaborateur, List<string> roles, List<long?> buIds)
{
IEnumerable<Collaborateur> res_collaborateurs;
if (collabsId != null && collabsId.Count > 0)
{
return collaborateurs.Values.Where(c => collabsId.Contains(c.Id.Value));
}
else
{
res_collaborateurs = collaborateurs.Values;
}
if(!ancienCollaborateur.HasValue || !ancienCollaborateur.Value)
{
res_collaborateurs = res_collaborateurs.Where(c => c.DateDepart == null);
}
if (roles != null && roles.Count > 0)
{
res_collaborateurs = res_collaborateurs.Where(c => HasRole(c, roles));
}
if (buIds != null && buIds.Count > 0)
{
res_collaborateurs = res_collaborateurs.Where(c => buIds.Contains(c.BusinessUnit.Id));
}
return res_collaborateurs;
}
private bool HasRole(Collaborateur c, List<string> roles)
{
foreach (Fonction fonction in c.Fonctions)
{
if (roles.Contains(fonction.Intitule))
return true;
}
return false;
}
}
}