private void OK_Click(object sender, RoutedEventArgs e)
 {
     if (int.Parse(SerialNumber.Text) < 1 || int.Parse(SerialNumber.Text) > CurPr.Events.Count + 1)
     {
         MessageBox.Show(string.Format("Порядковий номер івента повинен бути менше {0} і більше 0.",
             CurPr.Events.Count + 2));
     }
     else if (Name.Text == string.Empty)
         MessageBox.Show("Необхідно вказати заголовок");
     else 
     {
         Event ev = new Event(CurPr.ID, int.Parse(SerialNumber.Text), Name.Text, 
             DeadlineDate.SelectedDate == null ? DateTime.Now : DeadlineDate.SelectedDate.Value, Description.Text);
         try
         {
             Configuration config = (App.Current as App).config;
             using (TcpClient eClient = new TcpClient(config.IP.ToString(), config.Port))
             {
                 using (NetworkStream writerStream = eClient.GetStream())
                 {
                     MSG message = new MSG();
                     if(curWindow == CurrentWindow.AddEvent)
                         message.stat = STATUS.ADD_EVENT;
                     else if (curWindow == CurrentWindow.EditEvent)
                     {
                         ev.ID = CurPr.Events[EventNumber].ID;
                         message.stat = STATUS.UPDATE_EVENT;
                     }
                     BinaryFormatter formatter = new BinaryFormatter();
                     formatter.Serialize(writerStream, message);
                     formatter.Serialize(writerStream, ev);
                     if (!(bool)formatter.Deserialize(writerStream))
                         MessageBox.Show("Помилка обробки івенту");
                     else
                     {
                         this.Close();
                     }
                 }
             }
         }
         catch (Exception)
         {
             MessageBox.Show("Помилка обробки івенту");
         }   
     }
 }
Пример #2
0
        /// <summary>Обновляє подію по переданому об'єкту події</summary>
        /// <param name="event">Подія дані якої необхідно обновити в базі</param>
        /// <exception cref="ArgumentException">Якщо поле дедлайн знаходиться за межами типу SqlDateTime</exception>
        public static void UpdateEvent(Event @event)
        {
            if (@event.DeadLine < (DateTime)SqlDateTime.MinValue || @event.DeadLine > (DateTime)SqlDateTime.MaxValue)
                throw new ArgumentException(string.Format("Невірний формат дати для поля дедлайн, значення повинно бути в межах: від {0} до {1}", SqlDateTime.MinValue, SqlDateTime.MaxValue), "event");
            if (@event.ID < 1)
                throw new ArgumentException("невірний id події", "event");

            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
                using (
                    SqlCommand query =
                        new SqlCommand(
                            "UPDATE Event SET ProjectId = @ProjectId, SerialNumber = @SerialNumber, Title = @Title, Description = @Description, DeadLine = @DeadLine, AcceptDate = @AcceptDate, EventStatus = @EventStatus, Mark = @Mark, Penalty = @Penalty WHERE EventId = @Id",
                            connection, transaction))
                using (SqlCommand getSerialNumber = new SqlCommand("SELECT SerialNumber FROM [Event] WHERE EventId = @Id", connection, transaction))
                {
                    try
                    {
                        getSerialNumber.Parameters.Add("@Id", SqlDbType.Int).Value = @event.ID;
                        int oldSerialNumber = (int)(getSerialNumber.ExecuteScalar() ?? int.MinValue);
                        if (oldSerialNumber == int.MinValue)
                            throw new Exception("Event not found");

                        query.Parameters.Add("@ProjectId", SqlDbType.Int).Value = @event.ProjectId;
                        query.Parameters.Add("@SerialNumber", SqlDbType.Int).Value = @event.SerialNumber;
                        query.Parameters.Add("@Title", SqlDbType.VarChar, 50).Value = @event.Title;
                        query.Parameters.Add("@Description", SqlDbType.Text).Value = @event.Description;
                        query.Parameters.Add("@DeadLine", SqlDbType.DateTime).Value = @event.DeadLine;
                        query.Parameters.Add("@AcceptDate", SqlDbType.DateTime).Value = @event.AcceptDate < (DateTime)SqlDateTime.MinValue ? DBNull.Value : (object)@event.AcceptDate;
                        query.Parameters.Add("@EventStatus", SqlDbType.Int).Value = @event.EStatus;
                        query.Parameters.Add("@Mark", SqlDbType.Int).Value = @event.Mark;
                        query.Parameters.Add("@Penalty", SqlDbType.Real).Value = @event.Penalty;
                        query.Parameters.Add("@Id", SqlDbType.Int).Value = @event.ID;
                        query.ExecuteNonQuery();
                        using (SqlCommand childrenUpdate = new SqlCommand("UPDATE Event SET ProjectId = @ProjectId, SerialNumber = @SerialNumber, Title = @Title, Description = @Description, DeadLine = @DeadLine, AcceptDate = @AcceptDate, EventStatus = @EventStatus, Mark = @Mark, Penalty = @Penalty WHERE ProjectId = @ProjectId AND SerialNumber = @OldSerialNumber", connection, transaction))
                        {
                            childrenUpdate.Parameters.Add("@ProjectId", SqlDbType.Int);
                            childrenUpdate.Parameters.Add("@SerialNumber", SqlDbType.Int).Value = @event.SerialNumber;
                            childrenUpdate.Parameters.Add("@OldSerialNumber", SqlDbType.Int).Value = oldSerialNumber;
                            childrenUpdate.Parameters.Add("@Title", SqlDbType.VarChar, 50).Value = @event.Title;
                            childrenUpdate.Parameters.Add("@Description", SqlDbType.Text).Value = @event.Description;
                            childrenUpdate.Parameters.Add("@DeadLine", SqlDbType.DateTime).Value = @event.DeadLine;
                            childrenUpdate.Parameters.Add("@AcceptDate", SqlDbType.DateTime).Value = @event.AcceptDate < (DateTime)SqlDateTime.MinValue ? DBNull.Value : (object)@event.AcceptDate;
                            childrenUpdate.Parameters.Add("@EventStatus", SqlDbType.Int).Value = @event.EStatus;
                            childrenUpdate.Parameters.Add("@Mark", SqlDbType.Int).Value = @event.EStatus;
                            childrenUpdate.Parameters.Add("@Penalty", SqlDbType.Real).Value = @event.Penalty;
                            foreach (var project in GetProjectsByParentId(@event.ProjectId, false))
                            {
                                childrenUpdate.Parameters["@ProjectId"].Value = project.ID;
                                childrenUpdate.ExecuteNonQuery();
                            }
                        }
                        foreach (var penalty in @event.PenaltyCollection)
                            UpdatePenalty(penalty);
                        
                        transaction.Commit();
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }
Пример #3
0
        /// <summary>Додає event в базу</summary>
        /// <param name="event">Об'єкт дані якого заносяться в базу</param>
        /// <exception cref="ArgumentException">Якщо поле дедлайн знаходиться за межами типу SqlDateTime</exception>
        public static bool AddEvent(Event @event)
        {
            if (@event.DeadLine < (DateTime)SqlDateTime.MinValue || @event.DeadLine > (DateTime)SqlDateTime.MaxValue)
                throw new ArgumentException(
                    string.Format(
                        "Невірний формат дати для поля дедлайн, значення повинно бути в межах: від {0} до {1}",
                        SqlDateTime.MinValue, SqlDateTime.MaxValue), "event");


            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                connection.Open();
                using (SqlCommand count = new SqlCommand("SELECT COUNT(*) FROM Event WHERE ProjectId = @ProjectId AND SerialNumber = @SerialNumber", connection))
                {
                    count.Parameters.Add("@ProjectId", SqlDbType.Int).Value = @event.ProjectId;
                    count.Parameters.Add("@SerialNumber", SqlDbType.Int).Value = @event.SerialNumber;
                    if ((int)count.ExecuteScalar() > 0)
                        return false;
                }
                using (SqlTransaction transaction = connection.BeginTransaction())
                using (SqlCommand query = new SqlCommand(
                    "INSERT INTO Event(SerialNumber, Title, Description, DeadLine, AcceptDate, Mark, Penalty, ProjectId, EventStatus, DateOfCreation) Values(@SerialNumber, @Title, @Description, @DeadLine, @AcceptDate, @Mark, @Penalty, @ProjectId, @EventStatus, @DateOfCreation); SELECT CAST(scope_identity() AS int)",
                    connection, transaction))
                {
                    try
                    {
                        query.Parameters.Add("@SerialNumber", SqlDbType.Int).Value = @event.SerialNumber;
                        query.Parameters.Add("@Title", SqlDbType.VarChar, 50).Value = @event.Title;
                        query.Parameters.Add("@Description", SqlDbType.Text).Value = @event.Description;
                        query.Parameters.Add("@DeadLine", SqlDbType.DateTime).Value = @event.DeadLine;
                        query.Parameters.Add("@AcceptDate", SqlDbType.DateTime).Value = @event.AcceptDate < (DateTime)SqlDateTime.MinValue ? DBNull.Value : (object)@event.AcceptDate;
                        query.Parameters.Add("@Mark", SqlDbType.Decimal).Value = @event.Mark;
                        query.Parameters.Add("@Penalty", SqlDbType.Real).Value = @event.Penalty;
                        query.Parameters.Add("@ProjectId", SqlDbType.Int).Value = @event.ProjectId;
                        query.Parameters.Add("@EventStatus", SqlDbType.Int).Value = @event.EStatus;
                        query.Parameters.Add("@DateOfCreation", SqlDbType.DateTime).Value = @event.DateOfCreation <
                                                                                            (DateTime)
                                                                                                SqlDateTime.MinValue
                            ? DateTime.Now
                            : (object) @event.DateOfCreation; 
                        @event.ID = (int)query.ExecuteScalar();
                        foreach (var project in GetProjectsByParentId(@event.ProjectId, false))
                        {
                            query.Parameters["@ProjectId"].Value = project.ID;
                            query.ExecuteScalar();
                        }
                        transaction.Commit();
                        return true;
                    }
                    catch (Exception)
                    {
                        transaction.Rollback();
                        throw;
                    }
                }
            }
        }