Добавлен интерфейс логики, а так же реализована работа с объявлениями

This commit is contained in:
Алина Батылкина 2023-03-29 18:07:58 +04:00
parent e2cb1bd139
commit 4f5adc07dc
15 changed files with 617 additions and 146 deletions

View File

@ -1,39 +0,0 @@
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebsiteForPlacingAds
{
public abstract class AbstractDatabase
{
private NpgsqlConnection connection;
public AbstractDatabase(string connectionString)
{
connection = new NpgsqlConnection(connectionString);
}
protected DataTable ExecuteQuery(string query)
{
connection.Open();
NpgsqlCommand command = new NpgsqlCommand(query, connection);
NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(command);
DataTable result = new DataTable();
adapter.Fill(result);
connection.Close();
return result;
}
protected void ExecuteNonQuery(string query)
{
connection.Open();
NpgsqlCommand command = new NpgsqlCommand(query, connection);
command.ExecuteNonQuery();
connection.Close();
}
}
}

View File

@ -1,23 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebsiteForPlacingAds
{
public class ConnectionSingleton
{
private static PostgreSqlDatabase postgreSqlDatabase;
private ConnectionSingleton()
{ }
public static PostgreSqlDatabase getPostgreSqlDatabase(string connectionString)
{
if (postgreSqlDatabase == null)
postgreSqlDatabase = new PostgreSqlDatabase(connectionString);
return postgreSqlDatabase;
}
}
}

View File

@ -28,7 +28,7 @@
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.buttonCreateAd = new System.Windows.Forms.Button();
this.labelHeading = new System.Windows.Forms.Label();
this.textBoxHeading = new System.Windows.Forms.TextBox();
this.textBoxPrice = new System.Windows.Forms.TextBox();
@ -45,15 +45,15 @@
this.comboBoxClient = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// button1
// buttonCreateAd
//
this.button1.Location = new System.Drawing.Point(49, 456);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(125, 67);
this.button1.TabIndex = 0;
this.button1.Text = "Разместить объявление";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.buttonCreateAd.Location = new System.Drawing.Point(49, 456);
this.buttonCreateAd.Name = "buttonCreateAd";
this.buttonCreateAd.Size = new System.Drawing.Size(273, 39);
this.buttonCreateAd.TabIndex = 0;
this.buttonCreateAd.Text = "Разместить объявление";
this.buttonCreateAd.UseVisualStyleBackColor = true;
this.buttonCreateAd.Click += new System.EventHandler(this.buttonCreateAd_Click);
//
// labelHeading
//
@ -68,14 +68,14 @@
//
this.textBoxHeading.Location = new System.Drawing.Point(49, 34);
this.textBoxHeading.Name = "textBoxHeading";
this.textBoxHeading.Size = new System.Drawing.Size(125, 27);
this.textBoxHeading.Size = new System.Drawing.Size(273, 27);
this.textBoxHeading.TabIndex = 2;
//
// textBoxPrice
//
this.textBoxPrice.Location = new System.Drawing.Point(49, 96);
this.textBoxPrice.Name = "textBoxPrice";
this.textBoxPrice.Size = new System.Drawing.Size(125, 27);
this.textBoxPrice.Size = new System.Drawing.Size(273, 27);
this.textBoxPrice.TabIndex = 4;
//
// labelPrice
@ -91,7 +91,7 @@
//
this.textBoxDate.Location = new System.Drawing.Point(49, 158);
this.textBoxDate.Name = "textBoxDate";
this.textBoxDate.Size = new System.Drawing.Size(125, 27);
this.textBoxDate.Size = new System.Drawing.Size(273, 27);
this.textBoxDate.TabIndex = 6;
//
// labelDate
@ -125,7 +125,7 @@
//
this.textBoxStatus.Location = new System.Drawing.Point(49, 221);
this.textBoxStatus.Name = "textBoxStatus";
this.textBoxStatus.Size = new System.Drawing.Size(125, 27);
this.textBoxStatus.Size = new System.Drawing.Size(273, 27);
this.textBoxStatus.TabIndex = 8;
//
// labelStatus
@ -151,7 +151,7 @@
this.comboBoxSubcategory.FormattingEnabled = true;
this.comboBoxSubcategory.Location = new System.Drawing.Point(49, 413);
this.comboBoxSubcategory.Name = "comboBoxSubcategory";
this.comboBoxSubcategory.Size = new System.Drawing.Size(125, 28);
this.comboBoxSubcategory.Size = new System.Drawing.Size(273, 28);
this.comboBoxSubcategory.TabIndex = 15;
//
// comboBoxCategory
@ -159,7 +159,7 @@
this.comboBoxCategory.FormattingEnabled = true;
this.comboBoxCategory.Location = new System.Drawing.Point(49, 351);
this.comboBoxCategory.Name = "comboBoxCategory";
this.comboBoxCategory.Size = new System.Drawing.Size(125, 28);
this.comboBoxCategory.Size = new System.Drawing.Size(273, 28);
this.comboBoxCategory.TabIndex = 16;
//
// comboBoxClient
@ -167,14 +167,14 @@
this.comboBoxClient.FormattingEnabled = true;
this.comboBoxClient.Location = new System.Drawing.Point(49, 284);
this.comboBoxClient.Name = "comboBoxClient";
this.comboBoxClient.Size = new System.Drawing.Size(125, 28);
this.comboBoxClient.Size = new System.Drawing.Size(273, 28);
this.comboBoxClient.TabIndex = 17;
//
// CreateAdsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(228, 558);
this.ClientSize = new System.Drawing.Size(370, 530);
this.Controls.Add(this.comboBoxClient);
this.Controls.Add(this.comboBoxCategory);
this.Controls.Add(this.comboBoxSubcategory);
@ -189,7 +189,7 @@
this.Controls.Add(this.labelPrice);
this.Controls.Add(this.textBoxHeading);
this.Controls.Add(this.labelHeading);
this.Controls.Add(this.button1);
this.Controls.Add(this.buttonCreateAd);
this.Name = "CreateAdsForm";
this.Text = "Объявления";
this.ResumeLayout(false);
@ -199,7 +199,7 @@
#endregion
private Button button1;
private Button buttonCreateAd;
private Label labelHeading;
private TextBox textBoxHeading;
private TextBox textBoxPrice;

View File

@ -9,39 +9,61 @@ namespace WebsiteForPlacingAds
{
InitializeComponent();
this.psd = psd;
FillComboBox();
LoadData();
}
private void button1_Click(object sender, EventArgs e)
private void buttonCreateAd_Click(object sender, EventArgs e)
{
string[] client = comboBoxClient.SelectedItem.ToString().Split(" ");
string[] category = comboBoxCategory.SelectedItem.ToString().Split(" ");
string[] subcategory = comboBoxSubcategory.SelectedItem.ToString().Split(" ");
psd.addAd(textBoxHeading.Text, int.Parse(textBoxPrice.Text), textBoxDate.Text, textBoxStatus.Text, int.Parse(client[0]), int.Parse(category[0]), int.Parse(subcategory[0]));
string heading = textBoxHeading.Text;
string price = textBoxPrice.Text;
string date = textBoxDate.Text;
string status = textBoxStatus.Text;
string client_id = client[0];
string category_id = category[0];
string subcategory_id = subcategory[0];
string query = $"INSERT INTO ads VALUES (nextval('seq_ads'), '{heading}', {price}, '{date}', '{status}', {client_id}, {category_id}, {subcategory_id})";
try
{
psd.ExecuteNonQuery(query);
MessageBox.Show("Äîáàâëåíî!");
LoadData();
}
catch (Exception ex)
{
MessageBox.Show("Íå óäàëîñü äîáàâèòü");
}
}
private void FillComboBox()
private void LoadData()
{
DataTable result = psd.getAllCategories();
comboBoxCategory.Items.Clear();
comboBoxSubcategory.Items.Clear();
comboBoxClient.Items.Clear();
DataTable result = psd.ExecuteQuery("SELECT * FROM CATEGORIES");
string[] id_names = new string[result.Rows.Count];
for (int i = 0; i < result.Rows.Count; i++)
{
id_names[i] = result.Rows[i]["category_id"].ToString() + " " + (string)result.Rows[i]["name"];
id_names[i] = result.Rows[i]["categories_id"].ToString() + ". " + (string)result.Rows[i]["name"];
comboBoxCategory.Items.Add(id_names[i]);
}
result = psd.getAllSubcategories();
result = psd.ExecuteQuery("SELECT * FROM SUBCATEGORIES");
id_names = new string[result.Rows.Count];
for (int i = 0; i < result.Rows.Count; i++)
{
id_names[i] = result.Rows[i]["subcategory_id"].ToString() + " " + (string)result.Rows[i]["name"];
id_names[i] = result.Rows[i]["subcategories_id"].ToString() + ". " + (string)result.Rows[i]["name"];
comboBoxSubcategory.Items.Add(id_names[i]);
}
result = psd.getAllClients();
result = psd.ExecuteQuery("SELECT * FROM CLIENTS");
string[] id_names_surnames = new string[result.Rows.Count];
for (int i = 0; i < result.Rows.Count; i++)
{
id_names_surnames[i] = result.Rows[i]["client_id"].ToString() + " " +(string)result.Rows[i]["name"] + " " + (string)result.Rows[i]["surname"];
id_names_surnames[i] = result.Rows[i]["clients_id"].ToString().Trim() + ". " + ((string)result.Rows[i]["name"]).Trim() + " " + ((string)result.Rows[i]["surname"]).Trim();
comboBoxClient.Items.Add(id_names_surnames[i]);
}
}

View File

@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebsiteForPlacingAds
{
public class DatabaseLogic : IDatabaseLogic
{
private IDatabase database;
public DatabaseLogic(IDatabase database)
{
this.database = database;
}
public Object getRowTableById(string tableName, string columnName, int id)
{
return database.ExecuteQuery($"SELECT {columnName} FROM {tableName} WHERE {tableName}_id = {id}").Rows[0][$"{columnName}"];
}
public DataTable getEntryById(string tableName, int id)
{
return database.ExecuteQuery($"SELECT * FROM {tableName} WHERE {tableName}_id = {id}");
}
public void ExecuteNonQuery(string query)
{
database.ExecuteNonQuery(query);
}
public DataTable ExecuteQuery(string query)
{
return database.ExecuteQuery(query);
}
public DataTable getAllEntry(string tableName)
{
return database.ExecuteQuery($"SELECT * FROM {tableName}");
}
}
}

View File

@ -29,46 +29,47 @@
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.buttonDeleteAd = new System.Windows.Forms.Button();
this.comboBoxDeleteAds = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(40, 23);
this.label1.Location = new System.Drawing.Point(80, 22);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(338, 20);
this.label1.TabIndex = 0;
this.label1.Text = "Выберите объявление, которое хотите удалить";
//
// button1
// buttonDeleteAd
//
this.button1.Location = new System.Drawing.Point(136, 117);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(132, 55);
this.button1.TabIndex = 1;
this.button1.Text = "Удалить";
this.button1.UseVisualStyleBackColor = true;
this.buttonDeleteAd.Location = new System.Drawing.Point(151, 113);
this.buttonDeleteAd.Name = "buttonDeleteAd";
this.buttonDeleteAd.Size = new System.Drawing.Size(186, 37);
this.buttonDeleteAd.TabIndex = 1;
this.buttonDeleteAd.Text = "Удалить";
this.buttonDeleteAd.UseVisualStyleBackColor = true;
this.buttonDeleteAd.Click += new System.EventHandler(this.buttonDeleteAd_Click);
//
// comboBox1
// comboBoxDeleteAds
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(40, 64);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(338, 28);
this.comboBox1.TabIndex = 2;
this.comboBoxDeleteAds.FormattingEnabled = true;
this.comboBoxDeleteAds.Location = new System.Drawing.Point(40, 64);
this.comboBoxDeleteAds.Name = "comboBoxDeleteAds";
this.comboBoxDeleteAds.Size = new System.Drawing.Size(419, 28);
this.comboBoxDeleteAds.TabIndex = 2;
//
// UpdateAdsForm
// DeleteAdsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(415, 216);
this.Controls.Add(this.comboBox1);
this.Controls.Add(this.button1);
this.ClientSize = new System.Drawing.Size(505, 175);
this.Controls.Add(this.comboBoxDeleteAds);
this.Controls.Add(this.buttonDeleteAd);
this.Controls.Add(this.label1);
this.Name = "UpdateAdsForm";
this.Text = "UpdateAdsForm";
this.Name = "DeleteAdsForm";
this.Text = "Удалить объявление";
this.ResumeLayout(false);
this.PerformLayout();
@ -77,7 +78,7 @@
#endregion
private Label label1;
private Button button1;
private ComboBox comboBox1;
private Button buttonDeleteAd;
private ComboBox comboBoxDeleteAds;
}
}

View File

@ -12,9 +12,52 @@ namespace WebsiteForPlacingAds
{
public partial class DeleteAdsForm : Form
{
public DeleteAdsForm()
PostgreSqlDatabase psd;
private int setval_seq;
public DeleteAdsForm(PostgreSqlDatabase psd)
{
InitializeComponent();
this.psd = psd;
LoadData();
}
private void buttonDeleteAd_Click(object sender, EventArgs e)
{
string[] ads = comboBoxDeleteAds.SelectedItem.ToString().Split("|");
string ad_id = ads[0].Trim();
string query = $"DELETE FROM ADS WHERE ads_id = {ad_id}";
string query_seq_reduction = $"SELECT setval('seq_ads', {setval_seq - 1});";
try
{
psd.ExecuteNonQuery(query);
psd.ExecuteNonQuery(query_seq_reduction);
MessageBox.Show("Удалено!");
LoadData();
}
catch (Exception ex)
{
MessageBox.Show("Не удалось удалить");
}
}
public void LoadData()
{
comboBoxDeleteAds.Items.Clear();
DataTable result = psd.ExecuteQuery("SELECT * FROM ADS");
setval_seq = result.Rows.Count;
string Ads = "";
for (int i = 0; i < result.Rows.Count; i++)
{
Ads = result.Rows[i]["ads_id"].ToString() + " | " +
(string)result.Rows[i]["heading"] + " | " +
result.Rows[i]["price"].ToString() + " | " +
((DateTime)result.Rows[i]["date_of_placement"]).ToString("d") + " | " +
(string)result.Rows[i]["status"] + " | " +
result.Rows[i]["id_client"].ToString() + " | " +
result.Rows[i]["id_category"].ToString() + " | " +
result.Rows[i]["id_subcategory"].ToString() + "\n";
comboBoxDeleteAds.Items.Add(Ads);
}
}
}
}

View File

@ -0,0 +1,18 @@
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebsiteForPlacingAds
{
public interface IDatabase
{
public DataTable ExecuteQuery(string query);
public void ExecuteNonQuery(string query);
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebsiteForPlacingAds
{
public interface IDatabaseLogic
{
public DataTable getEntryById(string tableName, int id);
public Object getRowTableById(string tableName, string columnName, int id);
public DataTable ExecuteQuery(string query);
public void ExecuteNonQuery(string query);
DataTable getAllEntry(string tableName);
}
}

View File

@ -29,33 +29,35 @@
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.labelListAds = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.SystemColors.Control;
this.label1.Font = new System.Drawing.Font("Segoe UI", 11F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(149, 20);
this.label1.Size = new System.Drawing.Size(186, 25);
this.label1.TabIndex = 0;
this.label1.Text = "Список объявлений";
//
// label2
// labelListAds
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(12, 47);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(0, 20);
this.label2.TabIndex = 1;
this.label2.Click += new System.EventHandler(this.label2_Click);
this.labelListAds.AutoSize = true;
this.labelListAds.Font = new System.Drawing.Font("Segoe UI", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.labelListAds.Location = new System.Drawing.Point(12, 47);
this.labelListAds.Name = "labelListAds";
this.labelListAds.Size = new System.Drawing.Size(0, 23);
this.labelListAds.TabIndex = 1;
//
// ListAdsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.label2);
this.Controls.Add(this.labelListAds);
this.Controls.Add(this.label1);
this.Name = "ListAdsForm";
this.Text = "ListAdsForm";
@ -67,6 +69,6 @@
#endregion
private Label label1;
private Label label2;
private Label labelListAds;
}
}

View File

@ -7,20 +7,36 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace WebsiteForPlacingAds
{
public partial class ListAdsForm : Form
{
public ListAdsForm()
PostgreSqlDatabase psd;
public ListAdsForm(PostgreSqlDatabase psd)
{
InitializeComponent();
this.psd = psd;
LoadData();
}
private void label2_Click(object sender, EventArgs e)
private void LoadData()
{
string s = "Первое объявление\nВторое объявление";
label2.Text = s;
DataTable result = psd.ExecuteQuery("SELECT * FROM ADS");
string listAds = "";
for (int i = 0; i < result.Rows.Count; i++)
{
listAds += result.Rows[i]["ads_id"].ToString() + " | " +
(string)result.Rows[i]["heading"] + " | " +
result.Rows[i]["price"].ToString() + " | " +
((DateTime)result.Rows[i]["date_of_placement"]).ToString("d") + " | " +
(string)result.Rows[i]["status"] + " | " +
result.Rows[i]["id_client"].ToString() + " | " +
result.Rows[i]["id_category"].ToString() + " | " +
result.Rows[i]["id_subcategory"].ToString() + "\n";
}
labelListAds.Text = listAds;
}
}
}

View File

@ -12,11 +12,14 @@ namespace WebsiteForPlacingAds
{
public partial class MainForm : Form
{
PostgreSqlDatabase psd;
IDatabase psd;
IDatabaseLogic databaseLogic;
public MainForm()
{
InitializeComponent();
psd = new PostgreSqlDatabase("Server=localhost;Port=5432;User Id=postgres;Password=aswdqq;Database=alina_refactor;");
psd = new PostgreSqlDatabase("Server=localhost;Port=5432;User Id=postgres;Password=12345678;Database=AlinaDatabase1;");
databaseLogic = new DatabaseLogic(psd);
}
private void списокКлиентвToolStripMenuItem_Click(object sender, EventArgs e)
@ -36,25 +39,25 @@ namespace WebsiteForPlacingAds
private void создатьОбъявлениеToolStripMenuItem_Click(object sender, EventArgs e)
{
CreateAdsForm caf = new CreateAdsForm(psd);
CreateAdsForm caf = new CreateAdsForm((PostgreSqlDatabase)psd);
caf.Show();
}
private void удалитьОбъявлениеToolStripMenuItem_Click(object sender, EventArgs e)
{
UpdateAdsForm daf = new UpdateAdsForm();
daf.Show();
DeleteAdsForm uaf = new DeleteAdsForm((PostgreSqlDatabase)psd);
uaf.Show();
}
private void изменитьОбъявлениеэToolStripMenuItem_Click(object sender, EventArgs e)
{
DeleteAdsForm uaf = new DeleteAdsForm();
uaf.Show();
UpdateAdsForm daf = new UpdateAdsForm(databaseLogic);
daf.Show();
}
private void списокОбъявленийToolStripMenuItem_Click(object sender, EventArgs e)
{
ListAdsForm laf = new ListAdsForm();
ListAdsForm laf = new ListAdsForm((PostgreSqlDatabase)psd);
laf.Show();
}
}

View File

@ -1,16 +1,40 @@
using System;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WebsiteForPlacingAds
{
public class PostgreSqlDatabase : AbstractDatabase
public class PostgreSqlDatabase : IDatabase
{
public PostgreSqlDatabase(string connectionString) : base(connectionString)
protected NpgsqlConnection connection;
public PostgreSqlDatabase(string connectionString)
{
connection = new NpgsqlConnection(connectionString);
}
public DataTable ExecuteQuery(string query)
{
connection.Open();
NpgsqlCommand command = new NpgsqlCommand(query, connection);
NpgsqlDataAdapter adapter = new NpgsqlDataAdapter(command);
DataTable result = new DataTable();
adapter.Fill(result);
connection.Close();
return result;
}
public void ExecuteNonQuery(string query)
{
connection.Open();
NpgsqlCommand command = new NpgsqlCommand(query, connection);
command.ExecuteNonQuery();
connection.Close();
}
public void addAd(string heading, int price, string date, string status, int id_client, int id_category, int id_subcategory)
@ -58,5 +82,24 @@ namespace WebsiteForPlacingAds
string query = $"SELECT * FROM clients where id = {id_client}";
return ExecuteQuery(query);
}
public DataTable getFullList()
{
throw new NotImplementedException();
}
public void Create(string query)
{
ExecuteNonQuery(query);
}
public void Update()
{
throw new NotImplementedException();
}
public void Delete()
{
throw new NotImplementedException();
}
}
}

View File

@ -28,12 +28,215 @@
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.comboBoxClient = new System.Windows.Forms.ComboBox();
this.comboBoxCategory = new System.Windows.Forms.ComboBox();
this.comboBoxSubcategory = new System.Windows.Forms.ComboBox();
this.labelClient = new System.Windows.Forms.Label();
this.labelSubcategory = new System.Windows.Forms.Label();
this.labelCategory = new System.Windows.Forms.Label();
this.textBoxStatus = new System.Windows.Forms.TextBox();
this.labelStatus = new System.Windows.Forms.Label();
this.textBoxDate = new System.Windows.Forms.TextBox();
this.labelDate = new System.Windows.Forms.Label();
this.textBoxPrice = new System.Windows.Forms.TextBox();
this.labelPrice = new System.Windows.Forms.Label();
this.textBoxHeading = new System.Windows.Forms.TextBox();
this.labelHeading = new System.Windows.Forms.Label();
this.buttonUpdateAd = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.comboBoxUpdateAd = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBoxClient
//
this.comboBoxClient.FormattingEnabled = true;
this.comboBoxClient.Location = new System.Drawing.Point(93, 371);
this.comboBoxClient.Name = "comboBoxClient";
this.comboBoxClient.Size = new System.Drawing.Size(273, 28);
this.comboBoxClient.TabIndex = 32;
//
// comboBoxCategory
//
this.comboBoxCategory.FormattingEnabled = true;
this.comboBoxCategory.Location = new System.Drawing.Point(93, 438);
this.comboBoxCategory.Name = "comboBoxCategory";
this.comboBoxCategory.Size = new System.Drawing.Size(273, 28);
this.comboBoxCategory.TabIndex = 31;
//
// comboBoxSubcategory
//
this.comboBoxSubcategory.FormattingEnabled = true;
this.comboBoxSubcategory.Location = new System.Drawing.Point(93, 500);
this.comboBoxSubcategory.Name = "comboBoxSubcategory";
this.comboBoxSubcategory.Size = new System.Drawing.Size(273, 28);
this.comboBoxSubcategory.TabIndex = 30;
//
// labelClient
//
this.labelClient.AutoSize = true;
this.labelClient.Location = new System.Drawing.Point(93, 348);
this.labelClient.Name = "labelClient";
this.labelClient.Size = new System.Drawing.Size(58, 20);
this.labelClient.TabIndex = 29;
this.labelClient.Text = "Клиент";
//
// labelSubcategory
//
this.labelSubcategory.AutoSize = true;
this.labelSubcategory.Location = new System.Drawing.Point(93, 477);
this.labelSubcategory.Name = "labelSubcategory";
this.labelSubcategory.Size = new System.Drawing.Size(107, 20);
this.labelSubcategory.TabIndex = 28;
this.labelSubcategory.Text = "Подкатегория";
//
// labelCategory
//
this.labelCategory.AutoSize = true;
this.labelCategory.Location = new System.Drawing.Point(93, 415);
this.labelCategory.Name = "labelCategory";
this.labelCategory.Size = new System.Drawing.Size(81, 20);
this.labelCategory.TabIndex = 27;
this.labelCategory.Text = "Категория";
//
// textBoxStatus
//
this.textBoxStatus.Location = new System.Drawing.Point(93, 308);
this.textBoxStatus.Name = "textBoxStatus";
this.textBoxStatus.Size = new System.Drawing.Size(273, 27);
this.textBoxStatus.TabIndex = 26;
//
// labelStatus
//
this.labelStatus.AutoSize = true;
this.labelStatus.Location = new System.Drawing.Point(93, 285);
this.labelStatus.Name = "labelStatus";
this.labelStatus.Size = new System.Drawing.Size(52, 20);
this.labelStatus.TabIndex = 25;
this.labelStatus.Text = "Статус";
//
// textBoxDate
//
this.textBoxDate.Location = new System.Drawing.Point(93, 245);
this.textBoxDate.Name = "textBoxDate";
this.textBoxDate.Size = new System.Drawing.Size(273, 27);
this.textBoxDate.TabIndex = 24;
//
// labelDate
//
this.labelDate.AutoSize = true;
this.labelDate.Location = new System.Drawing.Point(93, 222);
this.labelDate.Name = "labelDate";
this.labelDate.Size = new System.Drawing.Size(134, 20);
this.labelDate.TabIndex = 23;
this.labelDate.Text = "Дата размещения";
//
// textBoxPrice
//
this.textBoxPrice.Location = new System.Drawing.Point(93, 183);
this.textBoxPrice.Name = "textBoxPrice";
this.textBoxPrice.Size = new System.Drawing.Size(273, 27);
this.textBoxPrice.TabIndex = 22;
//
// labelPrice
//
this.labelPrice.AutoSize = true;
this.labelPrice.Location = new System.Drawing.Point(93, 160);
this.labelPrice.Name = "labelPrice";
this.labelPrice.Size = new System.Drawing.Size(45, 20);
this.labelPrice.TabIndex = 21;
this.labelPrice.Text = "Цена";
//
// textBoxHeading
//
this.textBoxHeading.Location = new System.Drawing.Point(93, 121);
this.textBoxHeading.Name = "textBoxHeading";
this.textBoxHeading.Size = new System.Drawing.Size(273, 27);
this.textBoxHeading.TabIndex = 20;
//
// labelHeading
//
this.labelHeading.AutoSize = true;
this.labelHeading.Location = new System.Drawing.Point(93, 98);
this.labelHeading.Name = "labelHeading";
this.labelHeading.Size = new System.Drawing.Size(81, 20);
this.labelHeading.TabIndex = 19;
this.labelHeading.Text = "Заголовок";
//
// buttonUpdateAd
//
this.buttonUpdateAd.Location = new System.Drawing.Point(93, 543);
this.buttonUpdateAd.Name = "buttonUpdateAd";
this.buttonUpdateAd.Size = new System.Drawing.Size(273, 39);
this.buttonUpdateAd.TabIndex = 18;
this.buttonUpdateAd.Text = "Изменить объявление";
this.buttonUpdateAd.UseVisualStyleBackColor = true;
this.buttonUpdateAd.Click += new System.EventHandler(this.buttonUpdateAd_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(58, 20);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(338, 20);
this.label1.TabIndex = 33;
this.label1.Text = "Выберите объявление, которое хотите удалить";
//
// comboBoxUpdateAd
//
this.comboBoxUpdateAd.FormattingEnabled = true;
this.comboBoxUpdateAd.Location = new System.Drawing.Point(12, 52);
this.comboBoxUpdateAd.Name = "comboBoxUpdateAd";
this.comboBoxUpdateAd.Size = new System.Drawing.Size(430, 28);
this.comboBoxUpdateAd.TabIndex = 34;
this.comboBoxUpdateAd.SelectedIndexChanged += new System.EventHandler(this.comboBoxUpdateAd_SelectedIndexChanged);
//
// UpdateAdsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "DeleteAdsForm";
this.ClientSize = new System.Drawing.Size(454, 618);
this.Controls.Add(this.comboBoxUpdateAd);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBoxClient);
this.Controls.Add(this.comboBoxCategory);
this.Controls.Add(this.comboBoxSubcategory);
this.Controls.Add(this.labelClient);
this.Controls.Add(this.labelSubcategory);
this.Controls.Add(this.labelCategory);
this.Controls.Add(this.textBoxStatus);
this.Controls.Add(this.labelStatus);
this.Controls.Add(this.textBoxDate);
this.Controls.Add(this.labelDate);
this.Controls.Add(this.textBoxPrice);
this.Controls.Add(this.labelPrice);
this.Controls.Add(this.textBoxHeading);
this.Controls.Add(this.labelHeading);
this.Controls.Add(this.buttonUpdateAd);
this.Name = "UpdateAdsForm";
this.Text = "Обновить объявление";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private ComboBox comboBoxClient;
private ComboBox comboBoxCategory;
private ComboBox comboBoxSubcategory;
private Label labelClient;
private Label labelSubcategory;
private Label labelCategory;
private TextBox textBoxStatus;
private Label labelStatus;
private TextBox textBoxDate;
private Label labelDate;
private TextBox textBoxPrice;
private Label labelPrice;
private TextBox textBoxHeading;
private Label labelHeading;
private Button buttonUpdateAd;
private Label label1;
private ComboBox comboBoxUpdateAd;
}
}

View File

@ -12,9 +12,132 @@ namespace WebsiteForPlacingAds
{
public partial class UpdateAdsForm : Form
{
public UpdateAdsForm()
//PostgreSqlDatabase psd;
IDatabaseLogic databaseLogic;
public UpdateAdsForm(IDatabaseLogic databaseLogic)
{
InitializeComponent();
this.databaseLogic = databaseLogic;
LoadData();
}
private void buttonUpdateAd_Click(object sender, EventArgs e)
{
string[] selectedAds = comboBoxUpdateAd.SelectedItem.ToString().Split("|");
int ads_id = int.Parse(selectedAds[0].Trim());
string query = $"UPDATE ads SET ";
int len = query.Length;
string oldHeading = databaseLogic.getRowTableById("ads", "heading", ads_id).ToString();
string oldPrice = databaseLogic.getRowTableById("ads", "heading", ads_id).ToString();
string oldDate = ((DateTime)databaseLogic.getRowTableById("ads", "date_of_placement", ads_id)).ToString("dd.MM.yyyy");
string oldStatus = databaseLogic.getRowTableById("ads", "status", ads_id).ToString();
string[] oldClient = databaseLogic.getRowTableById("ads", "id_client", ads_id).ToString().Split(" ");
string[] oldCategory = databaseLogic.getRowTableById("ads", "id_category", ads_id).ToString().Split(" ");
string[] oldSubcategory = databaseLogic.getRowTableById("ads", "id_subcategory", ads_id).ToString().Split(" ");
string newHeading = textBoxHeading.Text;
string newPrice = textBoxPrice.Text;
string newDate = textBoxDate.Text;
string newStatus = textBoxStatus.Text;
string[] newClient = comboBoxClient.SelectedItem.ToString().Split(" ");
string[] newCategory = comboBoxCategory.SelectedItem.ToString().Split(" ");
string[] newSubcategory = comboBoxSubcategory.SelectedItem.ToString().Split(" ");
query = oldHeading == newHeading ? query : query + $"heading = '{newHeading}', ";
query = oldPrice == newPrice ? query : query + $"price = {newPrice}, ";
query = oldDate == newDate ? query : query + $"date_of_placement = '{newDate}', ";
query = oldStatus == newStatus ? query : query + $"status = '{newStatus}', ";
query = oldClient[0].Trim() == newClient[0].Trim() ? query : query + $"id_client = '{newClient[0].Substring(0, newClient[0].IndexOf("."))}', ";
query = oldCategory[0].Trim() == newCategory[0].Trim() ? query : query + $"id_category = '{newCategory[0].Substring(0, newCategory[0].IndexOf("."))}', ";
query = oldSubcategory[0].Trim() == newSubcategory[0].Trim() ? query : query + $"id_subcategory = '{newSubcategory[0].Substring(0, newSubcategory[0].IndexOf("."))}', ";
if (len != query.Length)
{
query = query.TrimEnd(' ');
query = query.TrimEnd(',');
query += $" WHERE ADS_ID = {ads_id};";
}
try
{
databaseLogic.ExecuteNonQuery(query);
MessageBox.Show("Данные успешно обновлены!");
}
catch (Exception)
{
MessageBox.Show("Данные не обновились");
}
}
public void LoadData()
{
comboBoxUpdateAd.Items.Clear();
DataTable result = databaseLogic.getAllEntry("Ads");
string Ads = "";
for (int i = 0; i < result.Rows.Count; i++)
{
Ads = result.Rows[i]["ads_id"].ToString() + " | " +
(string)result.Rows[i]["heading"] + " | " +
result.Rows[i]["price"].ToString() + " | " +
((DateTime)result.Rows[i]["date_of_placement"]).ToString("dd.MM.yyyy") + " | " +
(string)result.Rows[i]["status"] + " | " +
result.Rows[i]["id_client"].ToString() + " | " +
result.Rows[i]["id_category"].ToString() + " | " +
result.Rows[i]["id_subcategory"].ToString() + "\n";
comboBoxUpdateAd.Items.Add(Ads);
}
comboBoxCategory.Items.Clear();
comboBoxSubcategory.Items.Clear();
comboBoxClient.Items.Clear();
result = databaseLogic.getAllEntry("Categories");
string[] id_names = new string[result.Rows.Count];
for (int i = 0; i < result.Rows.Count; i++)
{
id_names[i] = result.Rows[i]["categories_id"].ToString() + ". " + (string)result.Rows[i]["name"];
comboBoxCategory.Items.Add(id_names[i]);
}
result = databaseLogic.getAllEntry("Subcategories");
id_names = new string[result.Rows.Count];
for (int i = 0; i < result.Rows.Count; i++)
{
id_names[i] = result.Rows[i]["subcategories_id"].ToString() + ". " + (string)result.Rows[i]["name"];
comboBoxSubcategory.Items.Add(id_names[i]);
}
result = databaseLogic.getAllEntry("Clients");
string[] id_names_surnames = new string[result.Rows.Count];
for (int i = 0; i < result.Rows.Count; i++)
{
id_names_surnames[i] = result.Rows[i]["clients_id"].ToString().Trim() + ". " + ((string)result.Rows[i]["name"]).Trim() + " " + ((string)result.Rows[i]["surname"]).Trim();
comboBoxClient.Items.Add(id_names_surnames[i]);
}
}
public void LoadDataSelectedItem(int ads_id)
{
DataTable result = databaseLogic.getEntryById("ads", ads_id);
textBoxHeading.Text = (string)result.Rows[0]["heading"];
textBoxPrice.Text = result.Rows[0]["price"].ToString();
textBoxDate.Text = ((DateTime)result.Rows[0]["date_of_placement"]).ToString("dd.MM.yyyy");
textBoxStatus.Text = (string)result.Rows[0]["status"];
comboBoxCategory.SelectedItem = result.Rows[0]["id_category"].ToString() + ". " + databaseLogic.getRowTableById("categories", "name", Convert.ToInt32(result.Rows[0]["id_category"])).ToString();
comboBoxSubcategory.SelectedItem = result.Rows[0]["id_subcategory"].ToString() + ". " + databaseLogic.getRowTableById("subcategories", "name", Convert.ToInt32(result.Rows[0]["id_subcategory"])).ToString();
comboBoxClient.SelectedItem = result.Rows[0]["id_client"].ToString().Trim() + ". " +
databaseLogic.getRowTableById("clients", "name", Convert.ToInt32(result.Rows[0]["id_client"])).ToString().Trim() + " " +
databaseLogic.getRowTableById("clients", "surname", Convert.ToInt32(result.Rows[0]["id_client"])).ToString().Trim();
}
private void comboBoxUpdateAd_SelectedIndexChanged(object sender, EventArgs e)
{
string[] selectedItems = comboBoxUpdateAd.SelectedItem.ToString().Split("|");
int ads_id = int.Parse(selectedItems[0].Trim());
LoadDataSelectedItem(ads_id);
}
}
}