public HttpResponseMessage PostRegister(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(
                () =>
                {
                    using (var context = new PictureTogetherContext())
                    {
                        this.ValidateUsername(model.Username);
                        this.ValidateAuthCode(model.AuthCode);

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

                        if (user != null)
                        {
                            throw new InvalidOperationException("User already exists.");
                        }

                        user = new User
                        {
                            Username = model.Username,
                            AuthCode = model.AuthCode
                        };

                        context.Users.Add(user);
                        context.SaveChanges();

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

                        var loggedModel = new LoggedUserModel
                        {
                            Username = user.Username,
                            SessionKey = user.SessionKey
                        };

                        var response =
                            this.Request.CreateResponse(HttpStatusCode.Created,
                                loggedModel);
                        return response;
                    }
                });

            return responseMsg;
        }
        public HttpResponseMessage PostLogin(UserModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(
                () =>
                {
                    using (var context = new PictureTogetherContext())
                    {
                        this.ValidateUsername(model.Username);
                        this.ValidateAuthCode(model.AuthCode);

                        var usernameToLower = model.Username.ToLower();
                        var user = context.Users.FirstOrDefault(
                            usr => usr.Username == usernameToLower &&
                                   usr.AuthCode == model.AuthCode);

                        if (user == null)
                        {
                            throw new InvalidOperationException("Wrong username or password.");
                        }

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

                        var loggedModel = new LoggedUserModel
                        {
                            Username = user.Username,
                            SessionKey = user.SessionKey,
                            Albums = user.Albums.Select(a => new AlbumModel
                            {
                                Id = a.Id,
                                Name = a.Name
                            }).ToList()
                        };

                        var response =
                            this.Request.CreateResponse(HttpStatusCode.Created,
                                loggedModel);
                        return response;
                    }
                });

            return responseMsg;
        }