// POST api/userdata
        public IHttpActionResult Post(TokenViewModel obj)
        {
            ILogger _logger = new Logger();
            _logger.Log("Post api/userdata. Object TokenViewModel initial: " + obj.TokenString, "api", 3);
            obj = Encryptor.EncryptToken(obj);
            Token token = _tokenRepo.Get(t => t.GuId == obj.GuId && t.Status == 1).FirstOrDefault();

            if (token != null)
            {
                var profile = AutoMapper.Mapper.Map<ProfileViewModel>(token.Profile);
                profile = Encryptor.DecryptProfile(profile);
                var currentTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;
                profile.Employments = profile.Employments.AsQueryable().Where(x => x.StartDateTimestamp < currentTimestamp && (x.EndDateTimestamp > currentTimestamp || x.EndDateTimestamp == 0)).ToList();

                UserInfoViewModel ui = new UserInfoViewModel();
                ui.profile = profile;
                ui.rates = AutoMapper.Mapper.Map <List<RateViewModel>> (_rateRepo.Get().Where(x=> x.isActive).ToList());

                _logger.Log("Post api/userdata. Before ok: ", "api", 3);
                return Ok(ui);
            }
            else
            {
                _logger.Log("Post api/userdata. Error: Token not found ", "api", 3);
                return new CustomErrorActionResult(Request, "Token not found", ErrorCodes.InvalidAuthorization, HttpStatusCode.Unauthorized);
            }
        }
        // POST new token from main server
        public IHttpActionResult Post(tmpCreateToken obj)
        {
            ILogger _logger = new Logger();
            _logger.Log("Post TokenController. Object tmpCreateToken initial: " + obj.Token, "api", 3);
            if (obj.Password == PasswordString)
            {
                Core.DomainModel.Profile profile = _profileRepo.Get(x => x.Id == obj.ProfileId).FirstOrDefault();

                if (profile != null)
                {
                    //Create viewmodel and encrypt it
                    TokenViewModel token = new TokenViewModel();
                    token.Status = 2;
                    token.GuId = obj.GuId;
                    token.TokenString = obj.Token;
                    token = Encryptor.EncryptToken(token);

                    bool anyToken = _tokenRepo.Get(x => x.GuId == token.GuId).Any();

                    if (!anyToken)
                    {
                        //Map to datamodel, and add profileid
                        Token mToken = AutoMapper.Mapper.Map<Token>(token);
                        mToken.ProfileId = obj.ProfileId;

                        try
                        {
                            _tokenRepo.Insert(mToken);
                            _uow.Save();
                            _logger.Log("Post TokenController. Before OK: ", "api", 3);
                            return Ok();
                        }
                        catch (Exception ex)
                        {
                            _logger.Log("Post TokenController. Save error. Exception: " + ex.Message, "api", 3);
                            return new CustomErrorActionResult(Request, "Save Error", ErrorCodes.SaveError,HttpStatusCode.BadRequest);
                        }
                    }
                    else
                    {
                        _logger.Log("Post TokenController. Error: Token already exists ", "api", 3);
                        return new CustomErrorActionResult(Request, "Token allready exists", ErrorCodes.TokenAllreadyExists, HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    _logger.Log("Post TokenController. Error: User not found", "api", 3);
                    return new CustomErrorActionResult(Request, "User not found", ErrorCodes.UserNotFound, HttpStatusCode.BadRequest);
                }
            }
            else
            {
                _logger.Log("Post TokenController. Error: Wrong password. Password: "******"api", 3);
                return new CustomErrorActionResult(Request, "Wrong Password", ErrorCodes.BadPassword, HttpStatusCode.Unauthorized);
            }
        }
        public static TokenViewModel EncryptToken(TokenViewModel token)
        {
            if (token.GuId != null)
            token.GuId = StringCipher.Encrypt(token.GuId, EncryptKey);

            if (token.TokenString != null)
            token.TokenString = StringCipher.Encrypt(token.TokenString, EncryptKey);

            return token;
        }
        // POST api/userdata
        public IHttpActionResult Post(TokenViewModel obj)
        {
            ILogger _logger = new Logger();
            _logger.Log("Post api/userdata. Object Token initial: " + obj.TokenString, "api", 3);
            obj = Encryptor.EncryptToken(obj);

            //Confirm link with token
            var tokens = _tokenRepo.Get(t => t.TokenString == obj.TokenString);

            if (tokens.Any())
            {
                //There could be multiple, loop if that is the case
                foreach (var token in tokens)
                {
                    if (token.Status == 2)
                    {
                        token.Status = 1;
                        _uow.Save();


                        //Return user info (optional)
                        var profile = AutoMapper.Mapper.Map<ProfileViewModel>(token.Profile);
                        profile = Encryptor.DecryptProfile(profile);

                        UserInfoViewModel ui = new UserInfoViewModel();
                        ui.profile = profile;
                        ui.rates = AutoMapper.Mapper.Map<List<RateViewModel>>(_rateRepo.Get().ToList());
                        _logger.Log("Post api/userdata before OK. Token: " + token, "api", 3);
                        return Ok(ui);
                    }
                }
                _logger.Log("Post api/userdata. Error: Token already used ", "api", 3);
                return new CustomErrorActionResult(Request, "Token allready used", ErrorCodes.TokenAllreadyActivated, HttpStatusCode.BadRequest);
                
            }
            else
            {
                _logger.Log("Post api/userdata. Error: Token not found", "api", 3);
                return new CustomErrorActionResult(Request,"Token not found", ErrorCodes.InvalidAuthorization, HttpStatusCode.Unauthorized);
            }
        }
        // Delete token from main server
        public IHttpActionResult Delete(tmpDeleteToken obj)
        {
            ILogger _logger = new Logger();
            _logger.Log("Delete TokenController. Object tmpDeleteToken initial: " + obj, "api", 3);
            if (obj.Password == PasswordString)
            {
                TokenViewModel tvm = new TokenViewModel();
                tvm.GuId = obj.GuId;
                tvm = Encryptor.EncryptToken(tvm);

                Token token = _tokenRepo.Get(x => x.GuId == tvm.GuId && x.Status > 0).FirstOrDefault();

                if (token != null)
                {
                    token.Status = 0;

                    try
                    {
                        _uow.Save();
                        _logger.Log("Delete TokenController. Before OK: ", "api", 3);
                        return Ok();
                    }
                    catch (Exception ex)
                    {
                        _logger.Log("Delete TokenController. Save error. Exception: " + ex.Message, "api", 3);
                        return new CustomErrorActionResult(Request, "Save Error", ErrorCodes.SaveError, HttpStatusCode.BadRequest);
                    }
                }
                else
                {
                    _logger.Log("Delete TokenController. Error: Token not found ", "api", 3);
                    return new CustomErrorActionResult(Request, "Token not found", ErrorCodes.InvalidAuthorization, HttpStatusCode.BadRequest);
                }
            }
            else
            {
                _logger.Log("Delete TokenController. Error: Wrong password. Password: "******"api", 3);
                return new CustomErrorActionResult(Request, "Wrong Password", ErrorCodes.BadPassword, HttpStatusCode.Unauthorized);
            }
        }