DBRequests.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using WpfAppUI.Data;
  6. namespace WpfAppUI.Model
  7. {
  8. /// <summary>
  9. /// Класс реализует функционал запросов для бд
  10. /// </summary>
  11. public static class DBRequests
  12. {
  13. //Все рабочие
  14. public static ICollection<Emploee> AllDataRequest()
  15. {
  16. ApplicationContextUI db = new ApplicationContextUI();
  17. return db.Emploees.ToList();
  18. }
  19. //Рабочий по ID
  20. public static Emploee EmploeeById(int id)
  21. {
  22. using (ApplicationContextUI db = new ApplicationContextUI())
  23. {
  24. return db.Emploees.Where(i => i.id == id).FirstOrDefault();
  25. }
  26. }
  27. //Все должности
  28. public static ICollection<Post> PostRequest()
  29. {
  30. using (ApplicationContextUI db = new ApplicationContextUI())
  31. {
  32. return db.Posts.ToList();
  33. }
  34. }
  35. //Удалить сотрудника
  36. public static bool DeleteEmploeeById(int id)
  37. {
  38. using (ApplicationContextUI db = new ApplicationContextUI())
  39. {
  40. var emploee = db.Emploees.Where(i => i.id == id).FirstOrDefault();
  41. if (emploee != null)
  42. {
  43. db.Remove(emploee);
  44. db.SaveChanges();
  45. return true;
  46. }
  47. return false;
  48. }
  49. }
  50. //Изменить данные о сотруднике
  51. public static void ChangeEmploeeData(int id, string surname, string name, Post post, DateTime birthday, string phone, string mail, string patronymic = null)
  52. {
  53. ApplicationContextUI db = new ApplicationContextUI();
  54. var emploee = db.Emploees.Where(i => i.id == id).FirstOrDefault();
  55. emploee.Surname = surname;
  56. emploee.Name = name;
  57. emploee.Post = post;
  58. emploee.Birthday = birthday;
  59. emploee.Phone = phone;
  60. emploee.Mail = mail;
  61. emploee.Patronymic = patronymic;
  62. db.SaveChanges();
  63. db.Dispose();
  64. }
  65. //Новый сотрудник
  66. public static bool AddNewEmploee(string surname, string name, Post post, DateTime birthday, string phone, string mail, string patronymic = null)
  67. {
  68. ApplicationContextUI db = new ApplicationContextUI();
  69. bool chek = false;
  70. if (!db.Emploees.Any(el => el.Name == name && el.Surname == surname && el.Mail == mail || el.Birthday == birthday))
  71. {
  72. Emploee emploee = new Emploee
  73. {
  74. Surname = surname,
  75. Name = name,
  76. Mail = mail,
  77. Birthday = birthday,
  78. Phone = phone,
  79. Post = post,
  80. Patronymic = patronymic,
  81. };
  82. db.Emploees.Add(emploee);
  83. db.SaveChanges();
  84. db.Dispose();
  85. chek = true;
  86. }
  87. return chek;
  88. }
  89. }
  90. }