public static Location Calculate(Suggestion suggestion)
        {
            List<Location> locations = DataAccessLayer.LocationAccess.getAll();
            Dictionary<User,List<Element>> distanceElements = new Dictionary<User,List<Element>>();

            // generate a string of stationary locations that fit the Google Distance API
            string origins = null;
            foreach (Location location in locations)
            {
                origins += location.Latitude + "," + location.Longitude + "|";
            }

            // Get distances for each users
            foreach (JoinedUser joinedUser in suggestion.JoinedUsers)
            {
                User user = UserBusiness.Get(joinedUser.UserId);
                distanceElements.Add(user,GetDistancesFromGoogle(user, origins));
            }

            int[] distances = new int[locations.Count];

            foreach(KeyValuePair<User, List<Element>> item in distanceElements)
            {
                for (int i = 0; i < item.Value.Count; i++)
                {
                    distances[i] += item.Value[i].distance.value;
                }
            }

            // select the best location and return it
            int index = Array.IndexOf(distances, distances.Min());
            return locations[index];
        }
        public static void Create(Suggestion suggestion, string weekdays)
        {
            JoinedUser u = new JoinedUser
            {
                UserId = suggestion.CreatorId,
                Weekdays = weekdays
            };

            suggestion.JoinedUsers.Add(u);

            DataAccessLayer.SuggestionAccess.Create(suggestion);
        }
 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 SuggestionViewModel FromModel(Suggestion suggestion)
 {
     if (suggestion == null)
         return null;
     return new SuggestionViewModel
     {
         Id = suggestion.Id,
         Creator = suggestion.Creator,
         EndDate = suggestion.EndDate,
         IsClosed = suggestion.IsClosed,
         JoinedUsers = suggestion.JoinedUsers.Select(x => new JoinedUserViewModel
         {
             User = x.User,
             Weekdays = x.Weekdays
         }).ToList(),
         Location = suggestion.Location,
         Sport = suggestion.Sport,
         StartDate = suggestion.StartDate,
         Title = suggestion.Title,
         MaximumUsers = suggestion.MaximumUsers,
         Description = suggestion.Description,
         MostPopularDays = suggestion.MostPopularDays
     };
 }
 private static bool HasUserJoined(int userId, Suggestion suggestion)
 {
     return suggestion.JoinedUsers.FindAll(j => j.UserId == userId).Count == 0;
 }
 public static void Update(Suggestion suggestion, int userId)
 {
     ValidateUserAccess(suggestion.Id, userId);
     DataAccessLayer.SuggestionAccess.Update(suggestion);
 }
 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);
     }
 }
        private Suggestion GetDomainFromViewModel(CreateSuggestionViewModel model)
        {
            // Get or create sport
            var suggestionSport = BusinessLayer.SportBusiness.Get(model.Sport);

            Suggestion suggestion = new Suggestion
            {
                Title = model.Title,
                StartDate = model.StartDate,
                EndDate = model.EndDate,
                Description = model.Description,
                MinimumUsers = model.MinPeople,
                MaximumUsers = model.MaxPeople,
                SportId = suggestionSport.Name,
                JoinedUsers = new List<JoinedUser>()
                {

                }
            };

            return suggestion;
        }