public void Delete(int?id)
        {
            if (!id.HasValue)
            {
                throw new AlarmException("You must select an Alarm to delete.");
            }

            AlarmRepository alarmRepository = new AlarmRepository();

            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                Entities.EntityAlarm toRemove = context.EntityAlarms.FirstOrDefault(a => a.Id == id);

                if (toRemove == null)
                {
                    throw new AlarmException("Alarm not found.");
                }

                var idAlarm = toRemove.Alarm.Id;
                context.EntityAlarms.Remove(toRemove);

                context.SaveChanges();

                alarmRepository.Delete(idAlarm);
            }
        }
Beispiel #2
0
        public void Modify(int?id, AuthorAlarm alarm)
        {
            if (!id.HasValue)
            {
                throw new AlarmException("You must select an Alarm to modify.");
            }

            AuthorAlarm     oldAlarm        = Get(alarm.Id);
            AlarmRepository alarmRepository = new AlarmRepository();

            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                var toUpdate = context.AuthorAlarms.FirstOrDefault(a => a.Id == id);

                if (id != alarm.Id || oldAlarm.Type != alarm.Type || oldAlarm.TimeMeasure != alarm.TimeMeasure ||
                    !oldAlarm.PostQuantity.Equals(alarm.PostQuantity) || !oldAlarm.Time.Equals(alarm.Time))
                {
                    Exists(alarm);
                }

                alarm.ReAnalyePhrases(Helper.Instance.GetPhrases(context.Phrases));

                toUpdate.Alarm.PostQuantity = Int32.Parse(alarm.PostQuantity);
                toUpdate.Alarm.PostCount    = alarm.PostCount;
                toUpdate.Alarm.Time         = Int32.Parse(alarm.Time);
                toUpdate.Alarm.Type         = alarm.Type;
                toUpdate.TimeMeasure        = alarm.TimeMeasure;

                alarmRepository.Modify(toUpdate.Alarm.Id, toUpdate.Alarm);

                context.AuthorAlarms.AddOrUpdate(toUpdate);

                context.SaveChanges();
            }
        }
        public void Modify(int?id, EntityAlarm alarm)
        {
            if (!id.HasValue)
            {
                throw new AlarmException("You must select an Alarm to modify.");
            }

            EntityAlarm     oldAlarm        = Get(alarm.Id);
            AlarmRepository alarmRepository = new AlarmRepository();

            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                var toUpdate = context.EntityAlarms.FirstOrDefault(a => a.Id == id);

                if (id != alarm.Id &&
                    (oldAlarm.Type != alarm.Type || oldAlarm.Entity.Id != alarm.Entity.Id || !oldAlarm.PostQuantity.Equals(alarm.PostQuantity) || !oldAlarm.Time.Equals(alarm.Time)))
                {
                    Exists(alarm);
                }

                alarm.ReAnalyePhrases(Helper.Instance.GetPhrases(context.Phrases));

                toUpdate.Alarm.PostQuantity = Int32.Parse(alarm.PostQuantity);
                toUpdate.Alarm.PostCount    = alarm.PostCount;
                toUpdate.Alarm.Time         = Int32.Parse(alarm.Time);
                toUpdate.Alarm.Type         = alarm.Type;
                toUpdate.Entity             = alarm.Entity != null?context.Entities.AsNoTracking().ToList().First(e => e.Id == alarm.Entity.Id) : null;

                alarmRepository.Modify(toUpdate.Alarm.Id, toUpdate.Alarm);

                context.EntityAlarms.AddOrUpdate(toUpdate);

                ObjectStateEntry entityEntry;
                ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.TryGetObjectStateEntry(toUpdate.Entity, out entityEntry);

                if (entityEntry != null)
                {
                    ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(toUpdate.Entity, toUpdate.Entity.Id != alarm.Entity.Id ? EntityState.Modified : EntityState.Unchanged);
                }

                context.SaveChanges();
            }
        }