Exemplo n.º 1
0
        public Person DeletePerson(Person personToDelete)
        {
            using (EventSchedulerContext dbContext = new EventSchedulerContext())
            {
                bool found = dbContext.People.Any(p => p.JMBG.Equals(personToDelete.JMBG));
                if (found)
                {
                    Person       foundPerson   = dbContext.People.Include(p => p.ScheduledEvents).SingleOrDefault(p => p.JMBG.Equals(personToDelete.JMBG));
                    List <Event> updatedEvents = new List <Event>();
                    foreach (Event e in foundPerson.ScheduledEvents)
                    {
                        updatedEvents.Add(dbContext.Events.First(ev => ev.EventId.Equals(e.EventId)));
                    }

                    Person deletedPerson = dbContext.People.Remove(foundPerson);
                    try
                    {
                        dbContext.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        logger.Error($"Error in DbMenager. Message: {e.Message}");
                    }
                    logger.Info($"DeletePerson successful");

                    IObserverPattern foundObserver = _eventNotifier.Observers.FirstOrDefault(p => ((Person)p).JMBG.Equals(personToDelete.JMBG));
                    _eventNotifier.UnregisterObserver(foundObserver);

                    deletedPerson.ScheduledEvents = new List <Event>(updatedEvents);
                    return(deletedPerson);
                }

                return(null);
            }
        }
Exemplo n.º 2
0
        public Person ModifyPerson(Person personToModify)
        {
            using (EventSchedulerContext dbContext = new EventSchedulerContext())
            {
                bool found = dbContext.People.Any(p => p.JMBG.Equals(personToModify.JMBG));
                if (found)
                {
                    Person foundPerson = dbContext.People.Include(p => p.ScheduledEvents).SingleOrDefault(p => p.JMBG.Equals(personToModify.JMBG));
                    dbContext.People.Attach(foundPerson);

                    foundPerson.JMBG              = personToModify.JMBG;
                    foundPerson.FirstName         = personToModify.FirstName;
                    foundPerson.LastName          = personToModify.LastName;
                    foundPerson.LastEditTimeStamp = DateTime.Now;

                    foreach (Event e in personToModify.ScheduledEvents)
                    {
                        Event foundEvent = dbContext.Events.FirstOrDefault(ev => ev.EventId.Equals(e.EventId));
                        if (foundEvent != null)
                        {
                            dbContext.Events.Attach(foundEvent);

                            if (foundPerson.ScheduledEvents.Contains(foundEvent, new EventComparer()))
                            {
                                Event eventInPerson = foundPerson.ScheduledEvents.FirstOrDefault(eve => eve.EventId.Equals(foundEvent.EventId));
                                foundPerson.ScheduledEvents.Remove(eventInPerson);

                                foundPerson.ScheduledEvents.Add(foundEvent);
                            }
                            else
                            {
                                foundPerson.ScheduledEvents.Add(foundEvent);
                            }
                        }
                        else
                        {
                            foundPerson.ScheduledEvents.Add(e);
                        }
                    }

                    try
                    {
                        dbContext.SaveChanges();
                    }
                    catch (Exception e)
                    {
                        logger.Error($"Error in DbMenager. Message: {e.Message}");
                    }
                    logger.Info($"ModifyPerson successful");
                    IObserverPattern personObserver = _eventNotifier.Observers.FirstOrDefault(p => ((Person)p).JMBG.Equals(personToModify.JMBG));
                    _eventNotifier.UnregisterObserver(personObserver);
                    _eventNotifier.RegisterObserver(new PersonObserver(foundPerson));
                    return(foundPerson);
                }

                return(null);
            }
        }
Exemplo n.º 3
0
 public virtual void UnregisterObserver(IObserverPattern observer)
 {
     Observers.Remove(observer);
 }
Exemplo n.º 4
0
 public virtual void RegisterObserver(IObserverPattern observer)
 {
     Observers.Add(observer);
 }