Exemplo n.º 1
0
        public AddUserResponse AddUser(AddUserRequest request)
        {
            var response = new AddUserResponse {
                Errors = new List <string>()
            };

            //TODO: validari

            if (request.Age < 10)
            {
                response.Success = false;
                response.Errors.Add("Age is lower than 10");
                return(response);
            }

            _userRepository.Add(new User
            {
                Name      = request.Name,
                Age       = request.Age,
                Username  = request.Username,
                Password  = Sha.Encrypt(request.Password),
                UserImage = request.UserImage
            });

            response.Success = true;
            return(response);
        }
        public Session GenerateSession(string username, string password)
        {
            var user = _repository.GetByUsername(username);

            if (user == null)
            {
                return(null);
            }

            if (Valid(user.Id, user.Password))
            {
                return(_context.Session.FirstOrDefault(u => u.UserId == user.Id));
            }

            var hash    = Sha.Encrypt(RandomString.CreateString(256));
            var session = new Session
            {
                UserId = user.Id,
                Key    = hash
            };

            _context.Session.Add(session);
            _context.SaveChanges();
            return(session);
        }