aksenov_exam_main/WindowsFormsApp2/Admin.cs
2026-05-14 15:56:24 +04:00

145 lines
5.2 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using MySql.Data.MySqlClient;
using System;
using System.Data;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Admin : Form
{
string connectionString = "Server=85.239.57.125; Database=Gavrilov_Aksenov_exam; Uid=ISP41_Gavrilov; Pwd=ISP41_Gavrilov;";
public Admin()
{
InitializeComponent();
LoadOrders();
buttonAdd.Click += ButtonAdd_Click;
buttonEdit.Click += ButtonEdit_Click;
buttonDelete.Click += ButtonDelete_Click;
textBoxSearch.TextChanged += TextBoxSearch_TextChanged;
comboBoxFilter.SelectedIndexChanged += ComboBoxFilter_SelectedIndexChanged;
comboBoxSort.SelectedIndexChanged += ComboBoxSort_SelectedIndexChanged;
}
void LoadOrders()
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
string query = "SELECT o.id, o.order_number, u.full_name, s.name as status, o.pickup_address, o.order_date, o.delivery_date FROM orders o JOIN users u ON o.user_id = u.id JOIN status s ON o.status = s.idstatus";
MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
}
}
void ButtonAdd_Click(object sender, EventArgs e)
{
OrderEditForm form = new OrderEditForm(connectionString, null);
if (form.ShowDialog() == DialogResult.OK)
{
LoadOrders();
}
}
void ButtonEdit_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["id"].Value);
OrderEditForm form = new OrderEditForm(connectionString, id);
if (form.ShowDialog() == DialogResult.OK)
{
LoadOrders();
}
}
}
void ButtonDelete_Click(object sender, EventArgs e)
{
if (dataGridView1.SelectedRows.Count > 0)
{
int id = Convert.ToInt32(dataGridView1.SelectedRows[0].Cells["id"].Value);
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
string query = "DELETE FROM orders WHERE id = @id";
MySqlCommand cmd = new MySqlCommand(query, conn);
cmd.Parameters.AddWithValue("@id", id);
cmd.ExecuteNonQuery();
}
LoadOrders();
}
}
void TextBoxSearch_TextChanged(object sender, EventArgs e)
{
FilterAndSort();
}
void ComboBoxFilter_SelectedIndexChanged(object sender, EventArgs e)
{
FilterAndSort();
}
void ComboBoxSort_SelectedIndexChanged(object sender, EventArgs e)
{
FilterAndSort();
}
void FilterAndSort()
{
string search = textBoxSearch.Text;
string filter = comboBoxFilter.Text;
string sort = comboBoxSort.Text;
string query = "SELECT o.id, o.order_number, u.full_name, s.name as status, o.pickup_address, o.order_date, o.delivery_date FROM orders o JOIN users u ON o.user_id = u.id JOIN status s ON o.status = s.idstatus WHERE 1=1";
if (!string.IsNullOrEmpty(search))
{
if (filter == "По имени")
{
query += " AND u.full_name LIKE '%" + search + "%'";
}
else if (filter == "По категории")
{
query += " AND o.order_number LIKE '%" + search + "%'";
}
else
{
query += " AND (o.order_number LIKE '%" + search + "%' OR u.full_name LIKE '%" + search + "%')";
}
}
if (sort == "По имени А-Я")
{
query += " ORDER BY u.full_name ASC";
}
else if (sort == "По имени Я-А")
{
query += " ORDER BY u.full_name DESC";
}
else if (sort == "По цене возр.")
{
query += " ORDER BY o.order_date ASC";
}
else if (sort == "По цене убыв.")
{
query += " ORDER BY o.order_date DESC";
}
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
conn.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter(query, conn);
DataTable dt = new DataTable();
adapter.Fill(dt);
dataGridView1.DataSource = dt;
}
}
private void button1_Click(object sender, EventArgs e)
{
Author form1 = new Author();
form1.Show();
this.Hide();
}
}
}