Exemple #1
0
            public async Task <UserTokenOut> Handle(LoginCommand request, CancellationToken cancellationToken)
            {
                var appUser = await _unitOfWork.Users.GetByEmailAsync(request.Email, cancellationToken);

                if (appUser == null)
                {
                    _appNotificationHandler.AddNotification(Keys.APPUSER, Messages.INVALID_EMAIL_PASSWORD);
                    return(null);
                }

                var passwordResult = _passwordHasher.VerifyHashedPassword(appUser, appUser.Password, request.Password);

                if (passwordResult == PasswordVerificationResult.Failed)
                {
                    _appNotificationHandler.AddNotification(Keys.APPUSER, Messages.INVALID_EMAIL_PASSWORD);
                    return(null);
                }

                var token = await _tokenGenerator.CreateToken(appUser);

                if (string.IsNullOrWhiteSpace(token))
                {
                    _appNotificationHandler.AddNotification(Keys.APPUSER, Messages.TOKEN_NOT_GENERATED);
                    return(null);
                }

                return(new UserTokenOut
                {
                    Name = appUser.Name,
                    Email = appUser.Email,
                    Token = token
                });

                throw new AppException(HttpStatusCode.InternalServerError);
            }
Exemple #2
0
            public async Task <Unit> Handle(DeleteAppUserCommand request, CancellationToken cancellationToken)
            {
                var appUser = await _unitOfWork.Users.FindAsync(new object[] { request.Id }, cancellationToken);

                if (appUser == null)
                {
                    _appNotificationHandler.AddNotification(Keys.APPUSER, Messages.USER_NOT_FOUND);
                    return(Unit.Value);
                }

                _unitOfWork.Users.Remove(appUser);

                var @event = _mapper.Map <AppUser, AppUserDeletedEvent>(appUser);

                @event.UserId = appUser.Id;

                var success = await _unitOfWork.SaveChangesAndCommitAsync(@event);

                if (success)
                {
                    await _distributedCache.RemoveAsync(CacheKeys.LIST_ALL_USERS);

                    return(Unit.Value);
                }

                throw new AppException(HttpStatusCode.InternalServerError);
            }
Exemple #3
0
            public async Task <Unit> Handle(UpdateAppUserCommand request, CancellationToken cancellationToken)
            {
                var appUser = await _unitOfWork.Users.GetByEmailAsync(request.Email, cancellationToken);

                if (appUser == null)
                {
                    _appNotificationHandler.AddNotification(Keys.APPUSER, Messages.USER_NOT_FOUND);
                    return(Unit.Value);
                }

                appUser.Update(request.Name);

                await _unitOfWork.Users.UpdateAsync(appUser, cancellationToken);

                var @event = _mapper.Map <AppUser, AppUserUpdatedEvent>(appUser);

                @event.UserId = appUser.Email;

                var success = await _unitOfWork.SaveAndCommitEvents(@event);

                if (success)
                {
                    await _distributedCache.RemoveAsync(CacheKeys.LIST_ALL_USERS);

                    return(Unit.Value);
                }

                throw new AppException(HttpStatusCode.InternalServerError);
            }
            public async Task <AppUserOut> Handle(DetailAppUserQuery request, CancellationToken cancellationToken)
            {
                var user = await _unitOfWork.Users.GetByEmailAsync(request.Email, cancellationToken);

                if (user == null)
                {
                    _appNotificationHandler.AddNotification(Keys.APPUSER, Messages.USER_NOT_FOUND);
                    return(null);
                }

                return(_mapper.Map <AppUserOut>(user));
            }
            public async Task <AppUserOut <Guid> > Handle(DetailAppUserQuery request, CancellationToken cancellationToken)
            {
                var user = await _unitOfWork.Users.FindAsync(new object[] { request.Id }, cancellationToken);

                if (user == null)
                {
                    _appNotificationHandler.AddNotification(Keys.APPUSER, Messages.USER_NOT_FOUND);
                    return(null);
                }

                return(_mapper.Map <AppUserOut <Guid> >(user));
            }
        public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
        {
            if (!context.ModelState.IsValid)
            {
                foreach (var error in context.ModelState.Values.SelectMany(x => x.Errors))
                {
                    _appNotificationHandler.AddNotification("ModelStateValidator", error.ErrorMessage);
                }
            }

            if (_appNotificationHandler.HasNotifications)
            {
                context.HttpContext.Response.StatusCode  = (int)HttpStatusCode.BadRequest;
                context.HttpContext.Response.ContentType = Common.APPLICATION_JSON;

                var notifications = JsonConvert.SerializeObject(_appNotificationHandler.Notifications);
                await context.HttpContext.Response.WriteAsync(notifications);

                return;
            }

            await next();
        }
Exemple #7
0
            public async Task <Unit> Handle(CreateAppUserCommand request, CancellationToken cancellationToken)
            {
                if (await _unitOfWork.Users.GetByEmailAsync(request.Email, cancellationToken) != null)
                {
                    _appNotificationHandler.AddNotification(Keys.APPUSER, Messages.EMAIL_EXISTS);
                    return(Unit.Value);
                }

                var appUser = AppUser.Create(request.Name, request.Email);

                if (appUser.Invalid)
                {
                    _appNotificationHandler.AddNotifications(appUser.ValidationResult);
                    return(Unit.Value);
                }

                var passwordHash = _passwordHasher.HashPassword(appUser, request.Password);

                appUser.UpdatePassword(passwordHash);

                await _unitOfWork.Users.AddAsync(appUser, cancellationToken);

                var @event = _mapper.Map <AppUser, AppUserCreatedEvent>(appUser);

                @event.UserId = appUser.Id;

                var success = await _unitOfWork.SaveChangesAndCommitAsync(@event);

                if (success)
                {
                    await _distributedCache.RemoveAsync(CacheKeys.LIST_ALL_USERS);

                    return(Unit.Value);
                }

                throw new AppException(HttpStatusCode.InternalServerError);
            }