Файл: Кузьмич КУРСОВА ТРПЗ.docx

ВУЗ: Не указан

Категория: Не указан

Дисциплина: Не указана

Добавлен: 08.04.2024

Просмотров: 44

Скачиваний: 0

ВНИМАНИЕ! Если данный файл нарушает Ваши авторские права, то обязательно сообщите нам.

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)