public User Authenticate(string facebookAccessToken)
 {
     GameNickEntities entities = new GameNickEntities();
     FacebookClient fbclient = new FacebookClient(facebookAccessToken);
     dynamic me = fbclient.Get("me");
     long myId = (long)Convert.ToUInt64(me.id.ToString());
     var exists = from users in entities.Users
                  where users.FacebookID == myId
                  select users;
     if (exists.Count() > 0)
     {
         exists.First().Name = me.name;
         exists.First().FacebookAccessToken = facebookAccessToken;
         exists.First().GameNickAccessToken = Guid.NewGuid();
         entities.SaveChanges();
         User _me = exists.First();
         _me.Status.Load();
         _me.GameNicks.Load();
         return _me;
     }
     // ReSharper disable RedundantIfElseBlock
     else
     // ReSharper restore RedundantIfElseBlock
     {
         User user = User.CreateUser(myId, 0);
         user.Name = me.name;
         // check that the ID is updated to a new unique value when it's added
         entities.AddToUsers(user);
         entities.SaveChanges();
         // todo: update the userID before returing it
         return entities.Users.First(u => u.FacebookID == myId);//user;
     }
 }
 public User AddGameNick(User accessToken, GameNick gameNick)
 {
     GameNickEntities entities = new GameNickEntities();
     if (IsAuthenticated(accessToken, entities))
     {
         if(     (gameNick.UserID == accessToken.ID)
             &&  (gameNick.GameID > 0)
             &&  (gameNick.Name != string.Empty))
         {
             entities.AddToGameNicks(gameNick);
             entities.SaveChanges();
             return GetMe(entities, accessToken.ID);
         }
         else
         {
             throw new InvalidDataException();
         }
     }
     throw new SecurityAccessDeniedException();
 }
        public long CreateEvent(User accessToken, GamingEvent eventInfo, List<User> inviteList)
        {
            GameNickEntities entities = new GameNickEntities();
            if (IsAuthenticated(accessToken, entities))
            {
                FacebookClient facebookClient = new FacebookClient(accessToken.FacebookAccessToken);
                Dictionary<string, object> createEventParameters = new Dictionary<string, object>();
                createEventParameters.Add("name", eventInfo.Name);
                createEventParameters.Add("start_time", eventInfo.StartTime.ToString());
                createEventParameters.Add("end_time", eventInfo.EndTime.ToString());
                createEventParameters.Add("owner", accessToken.FacebookID.ToString());
                createEventParameters.Add("privacy", "OPEN");
                createEventParameters.Add("description", eventInfo.Description);
                try
                {
                    dynamic result = facebookClient.Post("me/events", createEventParameters);

                    if (result != null)
                    {
                        long eventId = (long) Convert.ToInt64(result["id"].ToString());
                        if (inviteList != null)
                            if(inviteList.Count > 0)
                        {
                            string inviteString = "/invited?users=";
                            for (int i = 0; i < inviteList.Count(); i++)
                            {
                                inviteString += inviteList[i].FacebookID.ToString();
                                if (i < inviteList.Count() - 1)
                                    // don't add a , at the end
                                    inviteString += ",";
                            }

                            dynamic inviteResult =
                                 facebookClient.Post(eventId.ToString() + inviteString);
                        }
                        // add event to database
                        Event eEvent = Event.CreateEvent(0, eventInfo.Event.GameID, eventId);
                        entities.AddToEvents(eEvent);
                        entities.SaveChanges();
                        return eventId;
                    }
                }
                catch (Exception e)
                {

                    throw;
                }

                throw new Exception();
            }
            throw new SecurityAccessDeniedException();
        }
 public User UpdateStatus(User accessToken, int gameNickId, string status)
 {
     GameNickEntities entities = new GameNickEntities();
     if (IsAuthenticated(accessToken, entities))
     {
         try
         {
             Status newStatus = Status.CreateStatus(0, gameNickId, status, accessToken.ID);
             entities.AddToStatus(newStatus);
             entities.SaveChanges();
             return GetMe(entities, accessToken.ID);
         }
         catch (Exception exception)
         {
             throw;
         }
     }
     throw new SecurityAccessDeniedException();
 }
 public User RemoveGameNick(User accessToken, GameNick gameNick)
 {
     GameNickEntities entities = new GameNickEntities();
     if (IsAuthenticated(accessToken, entities))
     {
         entities.Attach(gameNick);
         entities.DeleteObject(gameNick);
         entities.SaveChanges();
         return GetMe(entities, accessToken.ID);
     }
     throw new SecurityAccessDeniedException();
 }