RestaurationTypeFactory.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 RestaurationTypeFactory : Internal.BaseFactory<Entity.RestaurationType>
  10. {
  11. public override IQueryable<Entity.RestaurationType> getAll()
  12. {
  13. return base.getAll("Ordre");
  14. }
  15. public IQueryable<Entity.RestaurationType> getAllValid()
  16. {
  17. return base.getAll("Ordre").Where(i => i.Valid == true);
  18. }
  19. public void Up(ref Entity.RestaurationType item)
  20. {
  21. int ordre = item.Ordre;
  22. //Cas de sortie immédiate
  23. if (item.Ordre == 1) return;
  24. Entity.RestaurationType itemToSubstitute = dbContext.RestaurationTypes.Where(i => i.Ordre == ordre - 1).First();
  25. itemToSubstitute.Ordre += 1;
  26. item.Ordre -= 1;
  27. dbContext.SaveChanges();
  28. }
  29. public void Down(ref Entity.RestaurationType item)
  30. {
  31. int ordre = item.Ordre;
  32. //Cas de sortie immédiate
  33. if (item.Ordre == dbContext.RestaurationTypes.Max(i => i.Ordre)) return;
  34. Entity.RestaurationType itemToSubstitute = dbContext.RestaurationTypes.Where(i => i.Ordre == ordre + 1).First();
  35. itemToSubstitute.Ordre -= 1;
  36. item.Ordre += 1;
  37. dbContext.SaveChanges();
  38. }
  39. private void Sort()
  40. {
  41. int ordre = 1;
  42. foreach (Entity.RestaurationType statut in dbContext.RestaurationTypes.OrderBy(i => i.Ordre))
  43. {
  44. statut.Ordre = ordre++;
  45. }
  46. dbContext.SaveChanges();
  47. }
  48. public override void add(ref Entity.RestaurationType entity)
  49. {
  50. //Initialisation de l'ordre
  51. if (dbContext.RestaurationTypes.Count() == 0) entity.Ordre = 1;
  52. else entity.Ordre = dbContext.RestaurationTypes.Max(i => i.Ordre) + 1;
  53. base.add(ref entity);
  54. }
  55. public override void delete(ref Entity.RestaurationType entity)
  56. {
  57. base.delete(ref entity);
  58. Sort();
  59. }
  60. }
  61. }