1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
| public partial class MainForm : Form
{
// Первая инициализация пользователя - как гость
User user = User.GetUser();
// SQL-запрос для обовления данных в dataGridView
public static string QueryToUpdate { set; get; } = null;
public static bool flagToRefresh = false;
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
// TODO: данная строка кода позволяет загрузить данные в таблицу "dataBaseDETDataSet.Products". При необходимости она может быть перемещена или удалена.
this.productsTableAdapter.Fill(this.dataBaseDETDataSet.Products);
administrationToolStripMenuItem.Visible = false;
buttonFilters.Enabled = false;
Show();
Welcome welcome = new Welcome();
welcome.ShowDialog();
UpdateControls();
QueryToUpdate = @"SELECT * FROM Products";
UpdateDataGridView();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("Закрыть программу?", "Выход из программы",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
{
e.Cancel = true;
}
}
private async void UpdateDataGridView()
{
Cursor = Cursors.WaitCursor;
progressBar.Value = 0;
if (string.IsNullOrWhiteSpace(QueryToUpdate))
QueryToUpdate = @"SELECT * FROM Products";
string WhereInStock = @" WHERE Магазин>0";
string AndInStock = @" AND Магазин>0";
string Sort = @" ORDER BY Жанр";
progressBar.Value = 10;
if (QueryToUpdate.Contains(Sort)) QueryToUpdate = QueryToUpdate.Replace(Sort, "");
if (radioButtonStock.Checked)
{
// показать товары в наличии
if (!QueryToUpdate.Contains("WHERE"))
QueryToUpdate += WhereInStock;
else
if (!QueryToUpdate.Contains("Магазин>0"))
QueryToUpdate += AndInStock;
}
else
if (radioButtonAllProduct.Checked)
{
// показать все товары
if (QueryToUpdate.Contains(WhereInStock))
QueryToUpdate = QueryToUpdate.Replace(WhereInStock, "");
if (QueryToUpdate.Contains(AndInStock))
QueryToUpdate = QueryToUpdate.Replace(AndInStock, "");
}
// сортировка
if (!QueryToUpdate.Contains("ORDER BY")) QueryToUpdate += Sort;
progressBar.Value = 30;
SqlConnection connection = new SqlConnection(Common.StrSQLConnection);
try
{
progressBar.Value = 35;
await connection.OpenAsync();
progressBar.Value = 60;
SqlDataAdapter sqlAdapter = new SqlDataAdapter(QueryToUpdate, connection);
DataSet dataSet = new DataSet();
sqlAdapter.Fill(dataSet);
progressBar.Value = 95;
dataGridView.DataSource = dataSet.Tables[0];
dataGridView.Columns[0].Visible = false;
dataGridView.Columns[9].Visible = false;
dataGridView.Columns[10].Visible = false;
progressBar.Value = 100;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), ex.Source.ToString(),
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
if (connection != null && connection.State != ConnectionState.Closed)
connection.Close();
if (dataGridView.DataSource != null) buttonFilters.Enabled = true;
Cursor = Cursors.Default;
progressBar.Value = 0;
}
}
private void UpdateControls()
{
if (user.Status == UserStatus.Guest)
{
loginToolStripMenuItem.Visible = true;
registerToolStripMenuItem.Visible = true;
goToPersonalAreaToolStripMenuItem.Visible = false;
logoutToolStripMenuItem.Visible = false;
administrationToolStripMenuItem.Visible = false;
textBoxUser.Visible = false;
buttonCart.Enabled = true;
}
else
{
if (user.Status == UserStatus.Admin)
{
administrationToolStripMenuItem.Visible = true;
buttonCart.Enabled = false;
}
else administrationToolStripMenuItem.Visible = false;
textBoxUser.Text = $"{user.Surname} {user.Name} {user.Patronymic}";
textBoxUser.Visible = true;
loginToolStripMenuItem.Visible = false;
registerToolStripMenuItem.Visible = false;
goToPersonalAreaToolStripMenuItem.Visible = true;
logoutToolStripMenuItem.Visible = true;
}
}
private void dataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0 && e.Button == MouseButtons.Left)
{
Cursor = Cursors.WaitCursor;
Product product = new Product(dataGridView.SelectedRows[0]);
product.ShowDialog();
Cursor = Cursors.Default;
}
}
private void bRefresh_Click(object sender, EventArgs e)
{
// очистка dataGridView
dataGridView.DataSource = null;
// загрузка данных из БД
UpdateDataGridView();
}
private void buttonFilters_Click(object sender, EventArgs e)
{
QueryToUpdate = null;
Filters filters = new Filters();
filters.ShowDialog();
if (flagToRefresh && !string.IsNullOrWhiteSpace(QueryToUpdate))
UpdateDataGridView();
}
private void buttonCart_Click(object sender, EventArgs e)
{
if (Common.ProductsInCart.Count == 0)
{
MessageBox.Show("Корзина пуста.", "Сообщение",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
Cart cart = new Cart();
cart.ShowDialog();
}
}
private void aboutProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutProgram about = new AboutProgram();
about.ShowDialog();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void goToPersonalAreaToolStripMenuItem_Click(object sender, EventArgs e)
{
switch (user.Status)
{
case UserStatus.Admin:
AdminProfile adminProfile = new AdminProfile();
adminProfile.ShowDialog();
break;
case UserStatus.Customer:
Profile profile = new Profile();
profile.ShowDialog();
break;
case UserStatus.Guest:
MessageBox.Show("Сначала авторизируйтесь.", "Переход в личный кабинет",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
break;
}
}
private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
{
if (user.Status != UserStatus.Guest)
{
if (MessageBox.Show("Вы уверены, что хотите выйти из системы?", "Подтверждение действия", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
User.Logout();
Common.ProductsInCart.Clear();
UpdateControls();
}
}
}
private void loginToolStripMenuItem_Click(object sender, EventArgs e)
{
Authorization auth = new Authorization();
auth.ShowDialog();
UpdateControls();
}
private void registerToolStripMenuItem_Click(object sender, EventArgs e)
{
Registration reg = new Registration();
reg.ShowDialog();
UpdateControls();
}
private void productManageToolStripMenuItem_Click(object sender, EventArgs e)
{
if (user.Status == UserStatus.Admin)
{
ProductManagement change = new ProductManagement();
change.ShowDialog();
UpdateDataGridView();
}
else MessageBox.Show("Недостаточно полномочий для завершения этого действия.",
"Некорректное действие!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
private void registerNewUserToolStripMenuItem_Click(object sender, EventArgs e)
{
Registration reg = new Registration();
reg.ShowDialog();
UpdateControls();
}
private void orderManagerToolStripMenuItem_Click(object sender, EventArgs e)
{
if (user.Status == UserStatus.Admin)
{
Orders orders = new Orders();
orders.ShowDialog();
}
else MessageBox.Show("Недостаточно полномочий для завершения этого действия.",
"Некорректное действие!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(Common.StrSQLConnection);
if (comboBox1.Text == "[Жанр]")
{
SqlDataAdapter sda = new SqlDataAdapter("SELECT [Название книги], [Жанр], [Автор] FROM Products WHERE [Жанр] LIKE '" + textBox1.Text + "%'", con);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView.DataSource = dt;
}
}
} |