public static void Update(User newUser)
        {
            try
            {
                using (var db = new MyDbContext())
                {
                    User dbUser = db.User.Single(i => i.Id == newUser.Id);
                    dbUser.Area = newUser.Area;
                    dbUser.BirthDate = newUser.BirthDate;
                    dbUser.FirstName = newUser.FirstName;
                    dbUser.Gender = newUser.Gender;
                    dbUser.LastName = newUser.LastName;
                    dbUser.PhoneNumber = newUser.PhoneNumber;
                    dbUser.State = newUser.State;
                    dbUser.StreetAddress = newUser.StreetAddress;
                    dbUser.Password = newUser.Password;
                    db.SaveChanges();

                }
            }
            catch (Exception e)
            {
                throw new DomainException("Could not update user", e);
            }
        }
        public static Sport GetOrCreate(string name)
        {
            try
            {
                validateName(name);

                Sport sport;
                using (var db = new MyDbContext())
                {
                    sport = db.Sport.Where(p => p.Name == name).FirstOrDefault();

                    if (sport == null)
                    {
                        sport = new Sport();
                        sport.Name = name;
                        db.Sport.Add(sport);
                        db.SaveChanges();
                    }

                    return sport;
                }
            }
            catch (Exception e)
            {
                throw new DomainException("Could not find or create sport", e);
            }
        }
 public static List<Sport> GetAll()
 {
     try
     {
         using (var db = new MyDbContext())
         {
             return (from s in db.Sport select s).ToList();
         }
     }
     catch (Exception e)
     {
         throw new DomainException("Could not get all sports", e);
     }
 }
 public static List<Location> getAll()
 {
     try
     {
         using (var db = new MyDbContext())
         {
             return db.Location.ToList();
         }
     }
     catch (Exception e)
     {
         throw new DomainException("Could not get all locations", e);
     }
 }
 public static void Add(JoinedUser joinedUser)
 {
     try
     {
         using (var db = new MyDbContext())
         {
             db.JoinedUser.Add(joinedUser);
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new DomainException("Could not add joined user", e);
     }
 }
 public static void Register(User user)
 {
     try
     {
         using (var db = new MyDbContext())
         {
             db.User.Add(user);
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new DomainException("Could not create user", e);
     }
 }
 public static void Create(Suggestion suggestion)
 {
     try
     {
         using (var db = new MyDbContext())
         {
             db.JoinedUser.Add(suggestion.JoinedUsers[0]);
             db.Suggestion.Add(suggestion);
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new DomainException("Could not create suggestion", e);
     }
 }
        public static List<JoinedUser> Get(int suggestionId)
        {
            if (suggestionId < 0)
                throw new DomainException("Invalid suggestion id");

            try
            {
                using (var db = new MyDbContext())
                {
                    return (from j in db.JoinedUser.Include("User") where j.SuggestionId == suggestionId select j).ToList();
                }
            }
            catch (Exception e)
            {
                throw new DomainException("Could not get joined users for suggestion", e);
            }
        }
        public static User Get(int userId)
        {
            try
            {
                if (userId < 0)
                    throw new DomainException("Invalid user id");

                using (var db = new MyDbContext())
                {
                    return db.User.Find(userId);
                }
            }
            catch (Exception e)
            {
                throw new DomainException("Could not fetch user", e);
            }
        }
 public static List<Suggestion> GetAll()
 {
     try
     {
         using (var db = new MyDbContext())
         {
             return (from s in db.Suggestion
                     .Include("Sport")
                     .Include("JoinedUsers")
                     .Include("Creator")
                     .Include("Location")
                     where s.IsClosed == false
                     select s).ToList();
         }
     }
     catch (Exception e)
     {
         throw new DomainException("Could not get all suggestions", e);
     }
 }
        public static User Get(string userEmail)
        {
            try
            {
                if (userEmail == null || userEmail.Equals(""))
                    throw new DomainException("Invalid email address");

                var user = new User();
                using (var db = new MyDbContext())
                {
                    var dbUser = db.User.Where(u => u.Email == userEmail).First();
                    user = (User)dbUser;
                }

                return user;
            }
            catch (Exception e)
            {
                throw new DomainException("Could not fetch user", e);
            }
        }
        public static List<Suggestion> GetByCreator(int creatorId)
        {
            try
            {
                validateId(creatorId);

                using (var db = new MyDbContext())
                {
                    return (from s in db.Suggestion
                            .Include("Sport")
                            .Include("JoinedUsers")
                            .Include("Creator")
                            .Include("Location")
                            where s.CreatorId == creatorId
                            select s).ToList();
                }
            }
            catch (Exception e)
            {
                throw new DomainException("Could not get suggestions by creator", e);
            }
        }
        public static Suggestion Get(int id)
        {
            try
            {
                validateId(id);

                using (var db = new MyDbContext())
                {
                    return (from s in db.Suggestion
                                .Include("Sport")
                                .Include("JoinedUsers")
                                .Include("Creator")
                                .Include("Location")
                                where s.Id == id
                                select s).First();
                }
            }
            catch (Exception e)
            {
                throw new DomainException("Could not fetch suggestion", e);
            }
        }
        public static List<Suggestion> GetByJoinedUser(int userId)
        {
            try
            {
                validateId(userId);

                using (var db = new MyDbContext())
                {
                    var query = from s in db.Suggestion
                                from ju in db.JoinedUser
                                where ju.UserId == userId
                                && s.Id == ju.SuggestionId
                                select new
                                {
                                    s,
                                    sport = s.Sport,
                                    ju = s.JoinedUsers
                                };

                    var suggestions = query.AsEnumerable().Select(m => m.s);

                    return suggestions.ToList();
                }
            }
            catch (Exception e)
            {
                throw new DomainException("Could not get suggestions by joined user", e);
            }
        }
 public static void Update(Suggestion updatedSuggestion)
 {
     try
     {
         using (var db = new MyDbContext())
         {
             var suggestion = db.Suggestion.Single((i => i.Id == updatedSuggestion.Id));
             updatedSuggestion.CreatorId = suggestion.CreatorId;
             db.Entry(suggestion).CurrentValues.SetValues(updatedSuggestion);
             db.SaveChanges();
         }
     }
     catch (Exception e)
     {
         throw new DomainException("Could not update suggestion", e);
     }
 }