Esempio n. 1
0
        public async Task <Guid> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            var entity = _mapper.Map <User>(request);

            entity.PasswordHash = LeGateSecurityHandler.HashText(request.Password);
            entity.CreationDate = DateTime.Now;
            _context.Users.Add(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(entity.Id);
        }
            public async Task <User> Handle(RegisterUserQuery request, CancellationToken cancellationToken)
            {
                var user = request.User;

                PasswordHelper.CreatePasswordHash(request.Password, out byte[] passwordHash, out byte[] passwordSalt);

                user.PasswordHash = passwordHash;
                user.PasswordSalt = passwordSalt;

                await _context.Users.AddAsync(user, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);

                return(user);
            }
Esempio n. 3
0
        public async Task <CreateUserResult> Handle(CreateUserCommand request, CancellationToken cancellationToken)
        {
            var user = _mapper.Map <Domain.Entities.User>(request);

            _userDbContext.Users.Add(user);

            var result = new CreateUserResult();

            if (await _userDbContext.SaveChangesAsync(cancellationToken) > 0)
            {
                result.Id      = user.Id;
                result.Success = true;
            }


            return(result);
        }
Esempio n. 4
0
        public async Task <DeleteUserResult> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.Users.FirstOrDefaultAsync(i => i.Id == request.Id);

            if (user == null)
            {
                throw new NotFoundException(request.Id);
            }

            _context.Users.Remove(user);

            var result = new DeleteUserResult();

            if (await _context.SaveChangesAsync(cancellationToken) > 0)
            {
                result.Id        = request.Id;
                result.IsDeleted = true;
            }

            return(result);
        }
Esempio n. 5
0
        public async Task <UpdateUserResult> Handle(UpdateUserCommand request, CancellationToken cancellationToken)
        {
            var user = await _context.Users.AsNoTracking().FirstOrDefaultAsync(i => i.Id == request.Id);

            if (user == null)
            {
                throw new NotFoundException(request.Id);
            }

            var result = new UpdateUserResult();
            //TODO: use Automapper
            var userModel = _mapper.Map <Domain.Entities.User>(request);

            _context.Users.Update(userModel);

            if (await _context.SaveChangesAsync(cancellationToken) > 0)
            {
                result.Id        = request.Id;
                result.IsUpdated = true;
            }

            return(result);
        }
Esempio n. 6
0
        public async Task <Unit> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            var entity = await _context.Users.FindAsync(request.Id);

            if (entity == null)
            {
                throw new NotFoundException(nameof(User), request.Id);
            }

            var haschildrens = _context.Users.Any(o => o.ParentUserId == entity.Id);

            if (haschildrens)
            {
                throw new DeleteFailureException(nameof(Users), request.Id, "There are existing users associated with this user.");
            }

            var hasMarkupPlans = _context.UserMarkupPlans.Any(o => o.UserId == entity.Id);

            if (hasMarkupPlans)
            {
                throw new DeleteFailureException(nameof(UserMarkupPlan), request.Id, "There are MarkupPlans orders associated with this user.");
            }

            var hasPointsRedeemPlan = _context.UserPointsRedeemPlans.Any(o => o.UserId == entity.Id);

            if (hasPointsRedeemPlan)
            {
                throw new DeleteFailureException(nameof(PointsRedeemPlan), request.Id, "There are PointsRedeemPlan orders associated with this user.");
            }

            _context.Users.Remove(entity);

            await _context.SaveChangesAsync(cancellationToken);

            return(Unit.Value);
        }