public static string CheckIn(int userId, int placeId)
        {
            var context = new FoursquareContext();
            var user = context.Users.FirstOrDefault(u => u.Id == userId);
            var place = context.Places.FirstOrDefault(p => p.Id == placeId);
            if (user == null || place == null)
            {
                throw new ServerErrorException("User or place does not exist.", "INV_NICK_LEN");
            }

            user.Latitude = place.Latitude;
            user.Longitude = place.Longitude;
            context.SaveChanges();

            PubnubAPI pubnub = new PubnubAPI(
            "pub-c-b428e4fd-a82e-4cc7-a0d2-d4f832fd1d2b",               // PUBLISH_KEY
            "sub-c-20f993fa-04b4-11e3-a005-02ee2ddab7fe",               // SUBSCRIBE_KEY
            "sec-c-ZGEzODJjZDEtOWE1ZC00YTE4LTg2NTctNzdiYTc4NjBlODVh",   // SECRET_KEY
            true                                                        // SSL_ON?
            );
            string channel = place.Id + "" + place.Name;
            List<object> publishResult = pubnub.Publish(channel, string.Format("User {0} checked at {1}.", user.Username,
                place.Name));
            return channel;
        }
        public static void CreateUser(string username, string authCode)
        {
            ValidateUsername(username);
            ValidateAuthCode(authCode);
            using (FoursquareContext context = new FoursquareContext())
            {
                var usernameToLower = username.ToLower();

                var dbUser = context.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower);

                if (dbUser != null)
                {
                    if (dbUser.Username.ToLower() == usernameToLower)
                    {
                        throw new ServerErrorException("Username already exists", "ERR_DUP_USR");
                    }
                }

                dbUser = new User()
                {
                    Username = usernameToLower,
                    AuthCode = authCode
                };
                context.Users.Add(dbUser);
                context.SaveChanges();
            }
        }
        public static void AddPlace(PlaceModel place)
        {
            ValidatePlaceModel(place);

            var context = new FoursquareContext();
            Place newPlace = new Place();
            newPlace.Name = place.Name;
            newPlace.Latitude = place.Latitude;
            newPlace.Longitude = place.Longitude;
            context.Places.Add(newPlace);
            context.SaveChanges();
        }
        public static string LoginUser(string username, string authCode, out string nameToReturn, out int id)
        {
            ValidateUsername(username);
            ValidateAuthCode(authCode);
            var context = new FoursquareContext();
            using (context)
            {
                var usernameToLower = username.ToLower();
                var user = context.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower && u.AuthCode == authCode);
                if (user == null)
                {
                    throw new ServerErrorException("Invalid user authentication", "INV_USR_AUTH");
                }

                var sessionKey = GenerateSessionKey((int)user.Id);
                user.SessionKey = sessionKey;
                context.SaveChanges();
                nameToReturn = user.Username;
                id = (int)user.Id;
                return sessionKey;
            }
        }
        public static void AddComment(int userId, int placeId, CommentModel comment)
        {
            ValidateComment(comment);
            FoursquareContext context = new FoursquareContext();
            var place = context.Places.FirstOrDefault(p => p.Id == placeId);

            if (place == null)
            {
                throw new ServerErrorException("Place does not exist anymore.", "INV_NICK_LEN");
            }

            Comment newComment = new Comment();
            newComment.Text = comment.Text;
            var user = context.Users.FirstOrDefault(u => u.Id == userId);

            if (user == null)
            {
                throw new ServerErrorException("User does not exist anymore.", "INV_NICK_LEN");
            }

            newComment.User = user;
            place.Comments.Add(newComment);
            context.SaveChanges();
        }
 public static void LogoutUser(string sessionKey)
 {
     ValidateSessionKey(sessionKey);
     var context = new FoursquareContext();
     using (context)
     {
         var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
         if (user == null)
         {
             throw new ServerErrorException("Invalid user authentication", "INV_USR_AUTH");
         }
         user.SessionKey = null;
         context.SaveChanges();
     }
 }