public async Task <PaymentResponse> Handle(CreatePaymentCommand request, CancellationToken cancellationToken) { var payment = _mapper.Map <Payment>(request); payment.IsDeleted = false; payment.CreatedBy = _currentUser.UserName; var paymentResponse = _mapper.Map <PaymentResponse>(await _paymentRepository.AddByLoadingReferenceAsync(payment, p => p.Order)); _logger.LogInformation($"Created payment, {JsonSerializer.Serialize(paymentResponse)}."); return(paymentResponse); }
public async Task<bool> Handle(UpdateUserCommand request, CancellationToken cancellationToken) { var user = await _userRepository.GetSingleAsync(new UserSpecification(request.UserId)); if (user == null) throw new BadRequestException($"User with user, {request.UserId} does not exist."); // throw error if the new username is already taken if (await _userRepository.GetSingleAsync(new UserSpecification(request.UserName)) != null) { _logger.LogInformation($"User, {request.UserName} already exists."); throw new BadRequestException($"User, {request.UserName} already exists."); } var passwordSaltAndHash = Password.CreatePasswordHash(request.Password); // update user properties user.UserName = request.UserName; user.PasswordSalt = passwordSaltAndHash.Item1; user.PasswordHash = passwordSaltAndHash.Item2; user.LastModifiedBy = _currentUser.UserName; var userResponse = _mapper.Map<UserResponse> (await _userRepository.UpdateAsync(user)); _logger.LogInformation($"Updated user, {JsonSerializer.Serialize(userResponse)}."); return userResponse != null; }
public async Task <bool> Handle(DeleteUserCommand request, CancellationToken cancellationToken) { var user = await _userRepository.GetSingleAsync(new UserSpecification(request.UserId)); if (user == null) { throw new BadRequestException($"User with userId, {request.UserId} does not exist."); } user.LastModifiedBy = _currentUser.UserName; user.IsDeleted = true; var result = await _userRepository.UpdateAsync(user); _logger.LogInformation($"Deleted user with userId, {request.UserId}."); return(result != null); }
public async Task <bool> Handle(DeleteProductCommand request, CancellationToken cancellationToken) { var product = await _productRepository.GetSingleAsync(new ProductSpecification(request.ProductId)); if (product == null) { throw new BadRequestException($"Product with productId, {request.ProductId} does not exist."); } product.LastModifiedBy = _currentUser.UserName; product.IsDeleted = true; var result = await _productRepository.UpdateAsync(product); _logger.LogInformation($"Deleted product with productId, {request.ProductId}."); return(result != null); }
public async Task <ProductResponse> Handle(CreateProductCommand request, CancellationToken cancellationToken) { if (await _productRepository.CountAsync(new ProductSpecification(request.Code)) > 0) { throw new BadRequestException("Product code already exists."); } var product = _mapper.Map <Product>(request); product.IsDeleted = false; product.CreatedBy = _currentUser.UserName; var productResponse = _mapper.Map <ProductResponse>(await _productRepository.AddByLoadingReferenceAsync(product, p => p.Category)); _logger.LogInformation($"Created product, {JsonSerializer.Serialize(productResponse)}."); return(productResponse); }
public async Task <CategoryResponse> Handle(CreateCategoryCommand request, CancellationToken cancellationToken) { if (await _categoryRepository.GetSingleAsync(new CategorySpecification(request.Name)) != null) { throw new BadRequestException("Category name already exists."); } var category = _mapper.Map <Category>(request); category.IsDeleted = false; category.CreatedBy = _currentUser.UserName; var categoryResponse = _mapper.Map <CategoryResponse>(await _categoryRepository.AddAsync(category)); _logger.LogInformation($"Created category, {JsonSerializer.Serialize(categoryResponse)}."); return(categoryResponse); }
public async Task <ProductResponse> Handle(UpdateProductCommand request, CancellationToken cancellationToken) { var product = await _productRepository.GetSingleAsync(new ProductSpecification(request.ProductId)); if (product == null) { throw new BadRequestException($"Product with productId, {request.ProductId} does not exist."); } product.Code = request.Code; product.Name = request.Name; product.Description = request.Description; product.UnitPrice = request.UnitPrice; product.CategoryId = request.CategoryId; product.LastModifiedBy = _currentUser.UserName; var productResponse = _mapper.Map <ProductResponse> (await _productRepository.UpdateByLoadingReferenceAsync(product, p => p.Category)); _logger.LogInformation($"Updated product, {JsonSerializer.Serialize(productResponse)}."); return(productResponse); }
public async Task <IActionResult> Authenticate([FromBody] LoginUserCommand command) { if (!await _mediator.Send(command)) { return(BadRequest(new { message = "Username or password is incorrect" })); } var tokenHandler = new JwtSecurityTokenHandler(); var key = Encoding.UTF8.GetBytes(_jwtIssuerOptions.SigningKey); var tokenDescriptor = new SecurityTokenDescriptor { Subject = new ClaimsIdentity(new Claim[] { new Claim(JwtRegisteredClaimNames.Sub, command.UserName), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()), new Claim(JwtRegisteredClaimNames.UniqueName, command.UserName), new Claim(ClaimTypes.Name, command.UserName.ToString()), new Claim(ClaimTypes.Role, Role.Admin) }), Expires = DateTime.Now.AddDays(1), NotBefore = DateTime.UtcNow, Issuer = _jwtIssuerOptions.Issuer, Audience = _jwtIssuerOptions.Audience, SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature), }; var token = tokenHandler.CreateToken(tokenDescriptor); var tokenString = tokenHandler.WriteToken(token); _logger.LogInformation($"User {command.UserName} authenticated."); // return basic user info and authentication token return(Ok(new { UserName = command.UserName, Token = tokenString, Expiration = token.ValidTo })); }
public async Task <bool> Handle(CreateUserCommand request, CancellationToken cancellationToken) { if (await _userRepository.GetSingleAsync(new UserSpecification(request.UserName)) != null) { throw new BadRequestException($"User, {request.UserName} already exists."); } var user = new User(); var passwordSaltAndHash = Password.CreatePasswordHash(request.Password); // update user properties user.UserName = request.UserName; user.PasswordSalt = passwordSaltAndHash.Item1; user.PasswordHash = passwordSaltAndHash.Item2; user.IsDeleted = false; // _currentUserService.UserName is null as API action to create user is anonymous. //user.CreatedBy = _currentUserService.UserName; user.CreatedBy = "anonymous"; var userResponse = _mapper.Map <UserResponse>(await _userRepository.AddAsync(user)); _logger.LogInformation($"Created user, {JsonSerializer.Serialize(userResponse)}."); return(userResponse != null); }