AnneeFactory.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using CD67.FicheCollege.Entity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Data.Entity;
  7. namespace CD67.FicheCollege.Factory
  8. {
  9. public partial class AnneeFactory : Internal.BaseFactory<Entity.Annee>
  10. {
  11. //public AnneeFactory(Entities dbContext) : base(dbContext) { }
  12. public Annee getByLibelle(params object[] keyValues)
  13. {
  14. string libelle = keyValues[0].ToString();
  15. Annee annee = base.getAll().Where(a=>a.Libelle == libelle).First();
  16. //si l'élément n'existe pas, on le créé
  17. if (annee == null)
  18. {
  19. annee = new Annee()
  20. {
  21. Libelle = libelle
  22. };
  23. this.add(ref annee);
  24. dbContext.Entry(annee).Reference(i => i.Colleges).Load();
  25. }
  26. return annee;
  27. }
  28. // Retourne l'id de l'année courante
  29. public int get_current_year_id()
  30. {
  31. string annee_lib;
  32. if (DateTime.Now.Month <= 7)
  33. {
  34. annee_lib = String.Format("{0}-{1}", DateTime.Now.Year - 1, DateTime.Now.Year);
  35. }
  36. else
  37. {
  38. annee_lib = String.Format("{0}-{1}", DateTime.Now.Year, DateTime.Now.Year + 1);
  39. }
  40. return dbContext.Annees.Where(a => a.Libelle == annee_lib).First().Id;
  41. }
  42. }
  43. }