Материал: Кузьмич КУРСОВА ТРПЗ

Внимание! Если размещение файла нарушает Ваши авторские права, то обязательно сообщите нам

SqlParameter idParam = new SqlParameter("@Id",id);

comm.Parameters.Add(idParam);

int num = comm.ExecuteNonQuery();

comm.Dispose();

if (conn != null)

conn.Close();

}

}

private List<Item> GetItemsFromQuery(string sql)

{

var result = new List<Item>();

try

{

itemsTable = new DataTable();

SqlConnection connection = new SqlConnection(connectionString);

SqlCommand command = new SqlCommand(sql, connection);

adapter = new SqlDataAdapter(command);

connection.Open();

adapter.Fill(itemsTable);

foreach (DataRow row in itemsTable.Rows)

{

var el = new Item

{

Company = row["Company"].ToString(),

ImagePath = row["ImagePath"].ToString(),

Title = row["Title"].ToString(),

Price = Convert.ToInt32(row["Price"]),

Id = Convert.ToInt32(row["Id"])

};

result.Add(el);

}

if (connection != null)

connection.Close();

if (itemsTable != null)

itemsTable.Clear();

}

catch (Exception ex)

{

}

return result;

}

public IEnumerable<Item> GetAllItems()

{

string sql = "Select * from Items";

return GetItemsFromQuery(sql);

}

public async Task<IEnumerable<Item>> GetAllItemsAsync()

{

string sql = "Select * from Items";

itemsTable = new DataTable();

using (SqlConnection connection = new SqlConnection(connectionString))

{

SqlCommand command = new SqlCommand(sql, connection);

adapter = new SqlDataAdapter(command);

await connection.OpenAsync();

adapter.Fill(itemsTable);

var result = new List<Item>();

foreach (DataRow row in itemsTable.Rows)

{

var el = new Item

{

Company = row["Company"].ToString(),

ImagePath = row["ImagePath"].ToString(),

Title = row["Title"].ToString(),

Price = Convert.ToInt32(row["Price"]),

Id = Convert.ToInt32(row["Id"])

};

result.Add(el);

}

if (connection != null)

connection.Close();

return result;

}

}

public Item GetItem(int id)

{

string sql = "Select * from Items " +

"where id = @Id";

var res = new Item();

itemsTable = new DataTable();

try

{

using (SqlConnection connection = new SqlConnection(connectionString))

{

SqlCommand command = new SqlCommand(sql, connection);

SqlParameter idParam = new SqlParameter("@Id", id);

command.Parameters.Add(idParam);

adapter = new SqlDataAdapter(command);

connection.Open();

adapter.Fill(itemsTable);

foreach (DataRow row in itemsTable.Rows)

{

var el = new Item

{

Company = row["Company"].ToString(),

ImagePath = row["ImagePath"].ToString(),

Title = row["Title"].ToString(),

Price = Convert.ToInt32(row["Price"]),

Id = Convert.ToInt32(row["Id"])

};

res = el;

}

connection.Close();

if (itemsTable != null)

itemsTable.Clear();

}

}

catch (Exception ex)

{

}

return res;

}

public void Save()

{

//string query = "Begin Transaction commit";

//ExecuteQuery(query);

}

public void Update(Item item)

{

string sqlQueryParam =

"Update Items " +

"Set " +

"Title = @Title," +

"ImagePath = @ImagePath," +

"Price = @Price," +

"Company = @Company " +

"Where Id = @Id";

using (SqlConnection conn = new SqlConnection(connectionString))

{

conn.Open();

SqlCommand comm = new SqlCommand(sqlQueryParam, conn);

SqlParameter param = new SqlParameter("@Title", SqlDbType.NVarChar, -1);

param.Value = item.Title;

comm.Parameters.Add(param);

SqlParameter ImagePathParam = new SqlParameter("@ImagePath", SqlDbType.NVarChar, -1);

ImagePathParam.Value = item.ImagePath;

comm.Parameters.Add(ImagePathParam);

SqlParameter PriceParam = new SqlParameter("@Price",SqlDbType.Int);

PriceParam.Value = item.Price;

comm.Parameters.Add(PriceParam);

SqlParameter CompanyParam = new SqlParameter("@Company", SqlDbType.NVarChar, -1);

CompanyParam.Value = item.Company;

comm.Parameters.Add(CompanyParam);

SqlParameter idParam = new SqlParameter("@Id", SqlDbType.Int);

idParam.Value = item.Id;

comm.Parameters.Add(idParam);

try

{

int num = comm.ExecuteNonQuery();

}

catch(Exception ex)

{

}

comm.Dispose();

if (conn != null)

conn.Close();

}

}

public void SendToServer<T>(T elem)

{

ClientTcpAction client = new ClientTcpAction();

client.SendData(elem);

}

}

Лістинг А.3 – Клас «ApplicationViewModel» (файл ApplicationViewModel.cs)

public class ApplicationViewModel: INotifyPropertyChanged

{

public ObservableCollection<ItemModel> Computers{ get; set; }

public ObservableCollection<ItemModel> OrderedComputers { get; set; }

private RelayCommand addComputerCommand;

public RelayCommand AddComputerCommand

{

get

{

if (addComputerCommand != null)

return addComputerCommand;

else

return (addComputerCommand = new RelayCommand(ob =>

{

StackPanel result = ob as StackPanel;

try

{

ItemModel computer = new ItemModel()

{

Title = result.Children.OfType<TextBox>().FirstOrDefault(n => n.Name == "TitleTxt").Text.ToString(),

Company = result.Children.OfType<TextBox>().FirstOrDefault(n => n.Name == "CompanyTxt").Text.ToString(),

ImagePath = result.Children.OfType<TextBox>().FirstOrDefault(n => n.Name == "ImageTxt").Text.ToString(),

Price = Convert.ToInt32(result.Children.OfType<TextBox>().FirstOrDefault(n => n.Name == "PriceTxt").Text.ToString())

};

//Item item = new Item();

Computers.Insert(0, computer);

SelectedComputer = computer;

}catch(Exception ex)

{

}

}));

}

}

private RelayCommand addComputerToDatabaseCommand;

public RelayCommand AddComputerToDatabaseCommand

{

get

{

if (addComputerToDatabaseCommand != null)

return addComputerToDatabaseCommand;

else

return (addComputerToDatabaseCommand = new RelayCommand(ob =>

{

ItemModel el = ob as ItemModel;

//Mapper.Initialize(config => config.CreateMap<ItemModel, Item>());

var mapperConf = new MapperConfiguration(config => config.CreateMap<ItemModel, Item>());

var mapper = mapperConf.CreateMapper();

Item item = mapper.Map<ItemModel, Item>(el);

if (ob != null)

{

try

{

try

{

serviceAdo.Items.Create(item);

}

catch (Exception ex)

{

}

try

{

service.Items.Create(item);

}

catch (Exception ex)

{

}

client.ItemAdded(item.ToString());

}

catch (Exception ex)

{

}

}

}));

}

}

private RelayCommand deleteComputerCommand;

public RelayCommand DeleteComputerCommand

{

get

{

if (deleteComputerCommand != null)

return deleteComputerCommand;

else

return (deleteComputerCommand = new RelayCommand(ob =>

{

ItemModel item = ob as ItemModel;

if (ob != null)

{

Computers.Remove(item);

try

{

try

{

serviceAdo.Items.Delete(item.Id);

}

catch (Exception ex)

{

}

try

{

service.Items.Delete(item.Id);

}

catch (Exception ex)

{

}

client.ItemDeleted(item.Id);

}

catch (Exception ex)

{

}

//service.Items.Delete(item.Id);

}

},

(ob)=>Computers.Count>0

));

}

}

private RelayCommand saveComputerCommand;

public RelayCommand SaveComputerCommand

{

get

{

if (saveComputerCommand != null)

return saveComputerCommand;

else

return (saveComputerCommand = new RelayCommand(ob =>

{

try

{

service.Items.Save();

}

catch (Exception ex)

{

}

}));

}

}

private RelayCommand updateComputerCommand;

public RelayCommand UpdateComputerCommand

{

get

{

if (updateComputerCommand != null)

return updateComputerCommand;

else

return (updateComputerCommand = new RelayCommand(ob =>

{

ItemModel el = ob as ItemModel;

var mapperConf = new MapperConfiguration(config => config.CreateMap<ItemModel, Item>());

var mapper = mapperConf.CreateMapper();

Item item = mapper.Map<ItemModel, Item>(el);

if (ob != null)

{

try

{

try

{

serviceAdo.Items.Update(item);

}

catch (Exception ex)

{

}

try

{

service.Items.Update(item);

}

catch (Exception ex )

{

}

client.ItemUpdated(item.Id, item.Title);

}catch(Exception ex)

{

}

//service.Items.Update(item);

}

},

(ob) => Computers.Count > 0

));

}

}

private RelayCommand moveToOrderList;

public RelayCommand MoveToOrderList

{

get

{

if (moveToOrderList != null)

return moveToOrderList;

else

return (moveToOrderList = new RelayCommand(ob =>

{

ItemModel item = ob as ItemModel;

if (ob != null)

{

OrderedComputers.Add(item);

}

},

(ob) => Computers.Count > 0

));

}

}

private ItemModel selectedComputer;

public ItemModel SelectedComputer

{

get

{

return selectedComputer;

}

set

{

selectedComputer = value;

OnPropertyRaised("SelectedComputer");

}

}

private ServiceModule service;

private ServiceModuleAdo serviceAdo;

ItemServiceClient client;

public ApplicationViewModel()

{

service = new ServiceModule("mvvmApp.Dal.Abstract.Entities.ApplicationContext");

serviceAdo = new ServiceModuleAdo();

List<Item> computersIEnum = serviceAdo.Items.GetAllItems().ToList();//service.Items.GetAllItems().ToList();//client.GetAllItems().ToList(); //

try

{

client = new ItemServiceClient();

if (client != null)

{

string ItemData = "";

foreach (var item in computersIEnum)

{

ItemData += item.ToString() + "\n";

}

client.ItemsFromDatabase(ItemData);

}

}

catch (Exception ex)

{

}

//Mapper.Initialize(config => config.CreateMap<Item, ItemModel>());

var mapperConf = new MapperConfiguration(conf => conf.CreateMap<Item, ItemModel>());

var mapper = mapperConf.CreateMapper();

Computers = new ObservableCollection<ItemModel>

(mapper.Map<List<Item>, ObservableCollection<ItemModel>>(computersIEnum));

//serviceAdo = new ServiceModuleAdo();

//var computersIEnum = serviceAdo.Items.GetAllItems().ToList();

//var orders = serviceAdo.Orders.GetAllItems();

//Computers = serviceAdo.Computers;

}

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyRaised(string propertyname)

{

if (PropertyChanged != null)

{

PropertyChanged(this, new PropertyChangedEventArgs(propertyname));

}

}

}

ДОДАТОК Б (ДІАГРАМА КЛАСІВ)

ДОДАТОК В (ДІАГРАМА Use Case)

Источник: https://studfile.net/preview/14690636/