DishEditForm.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using RestaurantApp.DAL;
  2. using RestaurantApp.Models;
  3. using System;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Windows.Forms;
  7. using System.Xml.Linq;
  8. namespace RestaurantApp
  9. {
  10. public partial class DishEditForm : Form
  11. {
  12. private DishRepository repo = new DishRepository();
  13. private Dish dish;
  14. private bool isEdit = false;
  15. public DishEditForm()
  16. {
  17. InitializeComponent();
  18. isEdit = false;
  19. dish = new Dish();
  20. this.Text = "Новое блюдо";
  21. LoadCategories();
  22. btnSave.Click += BtnSave_Click;
  23. btnCancel.Click += (s, e) => DialogResult = DialogResult.Cancel;
  24. }
  25. public DishEditForm(Dish existingDish)
  26. {
  27. InitializeComponent();
  28. isEdit = true;
  29. dish = existingDish;
  30. this.Text = "Редактирование блюда";
  31. LoadCategories();
  32. LoadData();
  33. btnSave.Click += BtnSave_Click;
  34. btnCancel.Click += (s, e) => DialogResult = DialogResult.Cancel;
  35. }
  36. private void LoadCategories()
  37. {
  38. var dtCategories = repo.GetCategories();
  39. var list = dtCategories.AsEnumerable().Select(r => new
  40. {
  41. category_id = r.Field<int>("category_id"),
  42. name = r.Field<string>("name")
  43. }).ToList();
  44. cmbCategory.DataSource = list;
  45. cmbCategory.DisplayMember = "name";
  46. cmbCategory.ValueMember = "category_id";
  47. if (!isEdit && cmbCategory.Items.Count > 0)
  48. cmbCategory.SelectedIndex = 0;
  49. }
  50. private void LoadData()
  51. {
  52. cmbCategory.SelectedValue = dish.CategoryId;
  53. txtName.Text = dish.Name;
  54. txtDescription.Text = dish.Description;
  55. numPrice.Value = dish.Price;
  56. numWeight.Value = dish.WeightG ?? 0;
  57. numCookTime.Value = dish.CookTimeMin ?? 0;
  58. chkAvailable.Checked = dish.IsAvailable;
  59. }
  60. private void SaveData()
  61. {
  62. dish.CategoryId = (int)cmbCategory.SelectedValue;
  63. dish.Name = txtName.Text.Trim();
  64. dish.Description = txtDescription.Text.Trim();
  65. dish.Price = numPrice.Value;
  66. dish.WeightG = numWeight.Value > 0 ? (int?)numWeight.Value : null;
  67. dish.CookTimeMin = numCookTime.Value > 0 ? (int?)numCookTime.Value : null;
  68. dish.IsAvailable = chkAvailable.Checked;
  69. if (string.IsNullOrWhiteSpace(dish.Name))
  70. throw new Exception("Введите название блюда");
  71. if (dish.Price <= 0)
  72. throw new Exception("Цена должна быть больше 0");
  73. if (isEdit)
  74. repo.Update(dish);
  75. else
  76. repo.Insert(dish);
  77. }
  78. private void BtnSave_Click(object sender, EventArgs e)
  79. {
  80. try
  81. {
  82. SaveData();
  83. DialogResult = DialogResult.OK;
  84. Close();
  85. }
  86. catch (Exception ex)
  87. {
  88. MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
  89. }
  90. }
  91. private void DishEditForm_Load(object sender, EventArgs e)
  92. {
  93. }
  94. }
  95. }