Exemple #1
0
 private void MenuItem_Click(object sender, RoutedEventArgs e)
 {
     if (list.SelectedIndex >= 0)
     {
         Reciepe       reciepe    = Messages[list.SelectedIndex].Reciepe;
         Model.Reciepe NewReciepe = new Model.Reciepe()
         {
             ReciepeName = reciepe.ReciepeName,
             Ingredients = reciepe.Ingredients,
             PhotoPath   = reciepe.PhotoPath,
             Instruction = reciepe.Instruction,
             CookingTime = reciepe.CookingTime,
             Calories    = reciepe.Calories,
             CuisineId   = reciepe.Cuisine.Id,
             TypeId      = reciepe.ReciepeType.Id,
             Rating      = reciepe.Raiting,
         };
         try
         {
             ReciepeBookHelper.dbReciepe.Reciepe.Add(NewReciepe);
             ReciepeBookHelper.dbReciepe.SaveChanges();
         }
         catch (DbUpdateException ex)
         {
             MessageBox.Show("У вас вже є такий рецепт", "Неможливо добавити",
                             MessageBoxButton.OK, MessageBoxImage.Error);
         }
     }
 }
Exemple #2
0
        private void btnSend_Click(object sender, RoutedEventArgs e)
        {
            if (!String.IsNullOrEmpty(tbTo.Text) && cbReciepe.SelectedIndex >= 0)
            {
                const int port   = 2020;
                TcpClient client = new TcpClient();
                try
                {
                    client.Connect("127.0.0.1", port);
                    using (NetworkStream stream = client.GetStream())
                    {
                        stream.Write(Encoding.UTF8.GetBytes("AM"), 0, 2);
                        #region AddMessage1

                        stream.Write(Encoding.UTF8.GetBytes(tbTo.Text), 0, tbTo.Text.Length);
                        stream.Flush();

                        const int BufferSize      = 6;
                        byte[]    commandResponce = new byte[BufferSize];
                        stream.Read(commandResponce, 0, BufferSize);
                        int userId = Convert.ToInt32(Encoding.UTF8.GetString(commandResponce));
                        Thread.Sleep(100);
                        if (userId >= 0)
                        {
                            Reciepe reciepe = Reciepes[cbReciepe.SelectedIndex];
                            string  text    = JsonConvert.SerializeObject(reciepe);

                            //Thread.Sleep(200);
                            //XmlSerializer xmlSerializer = new XmlSerializer(reciepe.GetType());
                            //xmlSerializer.Serialize(stream, reciepe);


                            stream.Write(Encoding.UTF8.GetBytes(text), 0, text.Length);
                            //} while (stream.DataAvailable);
                            Thread.Sleep(1000);
                            stream.Write(Encoding.UTF8.GetBytes(User.Email), 0, User.Email.Length);
                            Thread.Sleep(100);
                            //     stream.Flush();
                            stream.Write(Encoding.UTF8.GetBytes(tbText.Text), 0, tbText.Text.Length);
                        }
                        else
                        {
                            MessageBox.Show("Невірні дані користувача", "", MessageBoxButton.OK, MessageBoxImage.Error);
                        }
                        #endregion
                        #region SendMessage2
                        //stream.Write(Encoding.UTF8.GetBytes(tbTo.Text), 0, tbTo.Text.Length);
                        //Thread.Sleep(100);
                        //stream.Write(Encoding.UTF8.GetBytes(User.Email), 0, User.Email.Length);
                        //stream.Flush();
                        //Thread.Sleep(100);
                        //stream.Write(Encoding.UTF8.GetBytes(tbText.Text), 0, tbText.Text.Length);
                        //Thread.Sleep(100);

                        //Reciepe reciepe = Reciepes[cbReciepe.SelectedIndex];
                        //XmlSerializer xmlSerializer = new XmlSerializer(reciepe.GetType());
                        //xmlSerializer.Serialize(stream, reciepe);
                        #endregion
                    }
                }
                catch (SocketException ex)
                {
                    MessageBox.Show("Неможливо приєднатись до сервера", "", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                finally
                {
                    client.Close();
                }
            }
            else
            {
                MessageBox.Show("Ведіть адресу отримувача та виберіть рецепт", "",
                                MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }