public bool DeleteAllContributionsForAllOpenEventsFromPerson(Person person)
 {
     using (SynchronicWorldContext context = new SynchronicWorldContext())
     {
         var PersonInDb = context.Persons.Where(p=>p.Nickname.Equals(person.Nickname)).FirstOrDefault();
         if(PersonInDb==null)
         {
             return false;
         }
         List<Event> openEvents = context.Events.Include(e=>e.Contributions.Select(c=>c.Person)).Where(e=>e.Status.ToString().Equals(EventStatus.Open.ToString())).ToList();
         var contr = context.Contributions.Include(c => c.Person).Where(c => c.Person.Nickname.Equals(PersonInDb.Nickname));
         foreach (var openEvent in openEvents)
         {
             foreach (var cont in contr)
             {
                 if (openEvent.Contributions.Contains(cont))
                 {
                     context.Contributions.Remove(cont);
                 }
             }
         }
         context.SaveChanges();
         return true;
     }
 }
 public Person CreatePerson(Person person)
 {
     using (SynchronicWorldContext context = new SynchronicWorldContext())
     {
         //can't contain space, or nickname already used, or Firstname AND Lastname couple is already used
         if (context.Persons.Where(c => c.Firstname.ToLower().Equals(person.Firstname) && c.Lastname.ToLower().Equals(person.Lastname.ToLower())).Count() != 0
             || context.Persons.Where(c => c.Nickname.ToLower().Equals(person.Nickname.ToLower())).Count() != 0
             || person.Nickname.Contains(' ') == true)
         {
             return null;
         }
         Person newPerson = context.Persons.Add(person);
         context.SaveChanges();
         return newPerson;
     }
 }
 public bool AddPersonToOpenEvent(Event eventTarget, Person person)
 {
     using (SynchronicWorldContext context = new SynchronicWorldContext())
     {
         Person PersonInDb = context.Persons.Where(p => p.Nickname.ToLower().Equals(person.Nickname.ToLower())).FirstOrDefault();
         if (PersonInDb == null)
         {
             return false;
         }
         if (context.Events.Include(e=>e.Persons).Where(e => e.Name.ToLower().Equals(eventTarget.Name.ToLower())).FirstOrDefault().Persons.Contains(PersonInDb)
             || !context.Events.Where(e=>e.Name.ToLower().Equals(eventTarget.Name.ToLower())).FirstOrDefault().Status.ToString().Equals(EventStatus.Open.ToString()))
         {
             return false;
         }
         context.Events.Where(e => e.Name.Equals(eventTarget.Name)).FirstOrDefault().Persons.Add(PersonInDb);
         context.SaveChanges();
         return true;
     }
 }
        public Person UpdatePerson(Person person)
        {
            using (SynchronicWorldContext context = new SynchronicWorldContext())
            {

                //can't contain space, or nickname already used, or Firstname AND Lastname couple is already used
                if (context.Persons.Where(p => p.Firstname.ToLower().Equals(person.Firstname) && p.Lastname.ToLower().Equals(person.Lastname.ToLower()) && !p.Id.Equals(person.Id)).Count() != 0
                   || context.Persons.Where(p => p.Nickname.ToLower().Equals(person.Nickname.ToLower()) && !p.Id.Equals(person.Id)).Count() != 0
                   || person.Nickname.Contains(' ') == true)
                {
                    return null;
                }
                Person personInDb = context.Persons.Where(p => p.Id.Equals(person.Id)).FirstOrDefault();
                if (personInDb == null)
                {
                    return null;
                }
                personInDb.Firstname = person.Firstname;
                personInDb.Lastname = person.Lastname;
                personInDb.Nickname = person.Nickname;
                context.SaveChanges();
                return personInDb;
            }
        }
        public bool DeletePerson(Person person)
        {
            using (SynchronicWorldContext context = new SynchronicWorldContext())
            {
                Person PersonInDb = context.Persons.Where(p => p.Id.Equals(person.Id)).FirstOrDefault();
                if (PersonInDb == null)
                {
                    return false;
                }
                var contribsToRemove = context.Contributions.Include(c => c.Person).Where(p => p.Person.Id.Equals(person.Id));
                var events = context.Events.Include(e => e.Contributions).Include(e => e.Persons).ToList();
                foreach (var event1 in events)
                {
                    event1.Persons.Remove(PersonInDb);
                    foreach(var contr in contribsToRemove)
                    {
                        event1.Contributions.Remove(contr);
                    }
                }

                // A CONTRIBUTION CAN HAVE NO PERSON
                /*foreach (var contr in contribs)
                {
                    context.Persons.Remove(contr.Person);
                }*/

                // BUT I CHOOSE A CONTRIBUTION MUST HAVE A PERSON, SO I DELETE EVERY CONTRIBUTIONS WHICH HAVE NOBODY NOW
                foreach (var contr in contribsToRemove)
                {
                    context.Contributions.Remove(contr);
                }
                context.Persons.Remove(PersonInDb);
                context.SaveChanges();
                return true;
            }
        }
 public Contribution(string name, string quantity, ContributionType type, Person person)
 {
     this.Name = name;
     this.Quantity = quantity;
     this.Type = type;
 }
 public List<Contribution> GetAllContributionsFromPerson(Person person)
 {
     using (SynchronicWorldContext context = new SynchronicWorldContext())
     {
         if (context.Persons.Where(c => c.Nickname.ToLower().Equals(person.Nickname.ToLower().FirstOrDefault())) == null)
         {
             return null;
         }
         return context.Contributions.Include(c => c.Person).Where(c => c.Person.Nickname.ToLower().ToString().Equals(person.Nickname.ToLower().ToString())).ToList();
     }
 }