예제 #1
0
        public AlarmEntity Get(int id)
        {
            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                Entities.AlarmEntity alarm = context.EntityAlarms.FirstOrDefault(a => a.Id == id);

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

                return(helper.ToAlarmEntityBL(alarm));
            }
        }
예제 #2
0
        public void Delete(int?id)
        {
            if (id == null)
            {
                throw new AlarmException("You must select an Alarm to delete.");
            }

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

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

                context.EntityAlarms.Remove(toRemove);

                context.SaveChanges();
            }
        }
예제 #3
0
        public void Add(AlarmEntity alarm)
        {
            using (SentimentAnalysisContext context = new SentimentAnalysisContext())
            {
                if (context.EntityAlarms.Any(a => a.Id == alarm.Id))
                {
                    throw new AlarmException("An Alarm with the same ID already exists.");
                }

                ExistsAlarm(alarm.Entity, alarm.NumberDays, alarm.PostQuantity, alarm.Type);

                alarm.AnalyzePhrases(helper.GetPhrases(context.Phrases));
                Entities.AlarmEntity toAdd = helper.ToAlarmEntityEF(alarm);
                context.EntityAlarms.Add(toAdd);

                ((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.ChangeObjectState(toAdd.Entity, toAdd.Entity.Id != alarm.Entity.Id ? EntityState.Modified : EntityState.Unchanged);

                context.SaveChanges();
                alarm.Id = toAdd.Id;
            }
        }