0

I am developing app, that extract data from emails. I am trying to update DataGrid in WPF programatically.

Here is the scenario: If newly arrived alert with same element and same problem does exist in database, update him

else just save him.

It seems to me like easy step, but It don't save nothing. Every CRUD method works fine alone, so I am little bit frustrated why this not does not work.

SQL Scheme AMUser_ID and Problem_ID are FK

CREATE TABLE [dbo].[Alert](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [message_ID] [varchar](100) NOT NULL,
    [date] [datetime] NOT NULL,
    [element] [varchar](50) NOT NULL,
    [AMUser_ID] [int] NOT NULL,
    [Problem_ID] [int] NOT NULL,
    [clearTime] [int] NULL);

 CREATE TABLE [dbo].[Problem](
        [id] [int] IDENTITY(1,1) NOT NULL,
        [nameOfAlert] [varchar](50) NOT NULL,
        [Value_ID] [int] NOT NULL,
        [Result_ID] [int] NOT NULL,
        [message_ID] [varchar](100) NOT NULL);

Main method

for (int i = 0; i < inbox.Count; i++)
                        {
                            var message = inbox.GetMessage(i, cancel.Token);
                            GetBodyText = message.TextBody;
                            Problem problem = new Problem(message.MessageId);
                            //Here I am saving only unique problems, this prevents to save duplicates
                            if (!GetAll().Any(x => x.Message_Id.Equals(problem.Message_Id)))
                            {
                                Save(problem);
//Creates new Alert(1 is ID of Bot, 5 is the interval that indicates how long it takes to update the alert )

                                    Alert alert = new Alert(message.MessageId, message.Date.DateTime, message.Subject, 1, problem.Id, 5);

//Again here I am preventing to save only unique Alert by Id which is string
                                    if (!dAOAlert.GetAll().Any(x => x.Id_MimeMessage.Equals(alert.Id_MimeMessage)))
                                    {
                                        foreach (var item in GetAll())
                                        {
                                            FindUpdate(alert, item);
                                        }
                                    }
                                    else
                                    {
                                       
                                        MessageBox.Show("Duplicate");
                                    }
    
                                }
                        }

returns interval

 public double GetInterval(DateTime a, DateTime b)
        {
            return a.Subtract(b).TotalMinutes;
        }

Method that looks for Alerts, that should be Updated. GetAll() method is IEnumerable of all objects in DB

  public void FindUpdate(Alert newAlert, Alert matchAlert)
        {

        //If new Alerts arrives with same element and with same problem till five 5 minutes Update him, if not just Save him
        if (GetAll().Any(x => x.Element.Equals(newAlert.Element) && x.Problem.NameOfAlert.Equals(newAlert.Problem.NameOfAlert))
            &&
            GetInterval(newAlert.Date, matchAlert.Date) > 0 && GetInterval(newAlert.Date, matchAlert.Date) <= newAlert.ClearTime)
        {
            //Find alert, that should be updated
            var item = GetAll().FirstOrDefault(x => x.Element.Equals(newAlert.Element) && x.Problem.NameOfAlert.Equals(newAlert.Problem.NameOfAlert));
            Update(item, newAlert);
            LoadAlertGrid();
        }
        else
        {
            Save(newAlert);
            LoadAlertGrid();
        }
    }

EDIT: It update it, but it saves both alerts(new and old one) and it seems to me like performance killer

if (dAOAlert.GetAll().Any(x => x.Element.Equals(newAlert.Element) && x.Problem.NameOfAlert.Equals(newAlert.Problem.NameOfAlert))
                &&
                GetInterval(newAlert.Date, matchAlert.Date) > 0 && GetInterval(newAlert.Date, matchAlert.Date) <= 5)
            {                                       


matchAlert = dAOAlert.GetAll().FirstOrDefault(x => x.Element.Equals(newAlert.Element) && x.Problem.NameOfAlert.Equals(newAlert.Problem.NameOfAlert)) ;
                dAOAlert.Update(matchAlert, newAlert);
                dAOAlert.Delete(newAlert);
                dAOProblem.Delete(problem);
                LoadAlertGrid();
            }

Then main method looks like this

 if (!dAOAlert.GetAll().Any(x => x.Id_MimeMessage.Equals(alert.Id_MimeMessage)))
                                {
                                    dAOAlert.Save(alert);
                                    var alertOld = dAOAlert.GetAll().FirstOrDefault(x => x.Element.Equals(alert.Element) && x.Problem.NameOfAlert.Equals(alert.Problem.NameOfAlert));
                                    FindUpdate(alert, alertOld, problem);
                                }

But this is a bad solution in my opinion, especially in terms of application performance and also it does not Delete new Alert(even it should by Debug)

Petr
  • 103
  • 6

0 Answers0