public HttpResponseMessage LoginUser(UserRegisteredModel userModel)
        {
            var responseMessage = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    PlacesContext context = new PlacesContext();
                    using (context)
                    {
                        this.ValidateUsername(userModel.Username);
                        this.ValidateAuthCode(userModel.AuthCode);

                        string usernameToLower = userModel.Username.ToLower();

                        User existingUser = context.Users
                                                   .FirstOrDefault(u => u.Username.ToLower() == usernameToLower);
                        if (existingUser == null)
                        {
                            throw new ArgumentNullException("The user does not exist.");
                        }

                        if (existingUser.SessionKey == null)
                        {
                            existingUser.SessionKey = this.GenerateSessionKey(existingUser.Id);
                            context.SaveChanges();
                        }

                        UserLoggedInModel loggedUser = new UserLoggedInModel()
                        {
                            Nickname = existingUser.Nickname,
                            SessionKey = existingUser.SessionKey
                        };

                        return this.Request.CreateResponse(HttpStatusCode.Created, loggedUser);
                    }
                });

            return responseMessage;
        }
        public HttpResponseMessage RegisterUser(UserRegisteredModel userModel)
        {
            var responseMessage = this.PerformOperationAndHandleExceptions(
                () =>
                {
                    PlacesContext context = new PlacesContext();
                    using (context)
                    {
                        this.ValidateUsername(userModel.Username);
                        this.ValidateNickname(userModel.Nickname);
                        this.ValidateAuthCode(userModel.AuthCode);

                        string usernameToLower = userModel.Username.ToLower();
                        string nicknameToLower = userModel.Nickname.ToLower();

                        User user = context.Users
                                           .FirstOrDefault(u => u.Username.ToLower() == usernameToLower ||
                                                                u.Nickname == nicknameToLower);
                        if (user != null)
                        {
                            throw new InvalidOperationException("The user already exists.");
                        }

                        user = new User()
                        {
                            Username = usernameToLower,
                            Nickname = userModel.Nickname,
                            AuthCode = userModel.AuthCode,
                            Role = Role.User
                        };
                        context.Users.Add(user);
                        context.SaveChanges();

                        user.SessionKey = this.GenerateSessionKey(user.Id);
                        context.SaveChanges();

                        UserLoggedInModel loggedInUser = new UserLoggedInModel()
                        {
                            Nickname = user.Nickname,
                            SessionKey = user.SessionKey
                        };

                        return this.Request.CreateResponse(HttpStatusCode.Created, loggedInUser);
                    }
                });

            return responseMessage;
        }