public async Task <Product> CreateAsync(Product item, CancellationToken cancellationToken = default)
        {
            if (!Guid.TryParse(_principal.GetUserID(), out var userID))
            {
                throw new BusinessException($"{nameof(userID)} is invalid");
            }

            var product = _dataContext.Products.FirstOrDefault(p => p.Name == item.Name && p.UserID == userID);

            if (product != null)
            {
                throw new BusinessException("Product already exists!");
            }

            item.UserID = userID;
            item.User   = _dataContext.Users.FirstOrDefault(u => u.Id == userID) ?? throw new BusinessException("User is not exists!");
            item.User.Products.Add(item);

            var category = _dataContext.Categories.FirstOrDefault(c => c.ID == item.CategoryID);

            if (category != null)
            {
                item.CategoryID = category.ID;
                item.Category   = category;
            }

            item.ID = _dataContext.Products.Max(c => c.ID) + 1;
            _dataContext.Products.Add(item);

            return(await Task.FromResult(item));
        }
        public async Task <UserDto> Handle(ResetUserPasswordCommand request, CancellationToken cancellationToken)
        {
            if (String.IsNullOrWhiteSpace(request.Token))
            {
                throw new ValidationException($"{nameof(request.Token)} is invalid");
            }

            if (request.Dto == null)
            {
                throw new ValidationException($"{nameof(request.Dto)} is invalid");
            }

            if (String.IsNullOrWhiteSpace(request.Dto.Password))
            {
                throw new ValidationException($"{nameof(request.Dto.Password)} is invalid");
            }

            var userID = _principal.GetUserID();
            var user   = await _usersRepository.ResetPasswordAsync(userID, request.Dto.Password, request.Token);

            var result = new UserDto
            {
                ID             = user.Id,
                UserName       = user.UserName,
                Email          = user.Email,
                EmailConfirmed = user.EmailConfirmed
            };

            return(result);
        }
        public async Task <ProductDto> Handle(GetProductQuery request, CancellationToken cancellationToken)
        {
            if (request.ID <= 0)
            {
                throw new ValidationException($"{nameof(request.ID)} is invalid");
            }

            if (!Guid.TryParse(_principal.GetUserID(), out var userID))
            {
                throw new BusinessException($"{nameof(userID)} is invalid");
            }

            var product = await _repository.GetAsync(request.ID, userID, cancellationToken);

            if (product == null)
            {
                throw new BusinessException("Product is not exists!");
            }

            var result = new ProductDto
            {
                ID         = product.ID,
                Name       = product.Name,
                CategoryID = product.CategoryID
            };

            return(result);
        }
        public async Task <ProductItemDto[]> Handle(GetProductItemsQuery request, CancellationToken cancellationToken)
        {
            if (!Guid.TryParse(_principal.GetUserID(), out var userID))
            {
                throw new BusinessException($"{nameof(userID)} is invalid");
            }

            var productItems = await _repository.GetAsync(userID, cancellationToken);

            var result = productItems
                         .Select(pi => new ProductItemDto
            {
                ID        = pi.ID,
                Name      = pi.Name,
                Price     = pi.Price,
                Quantity  = pi.Quantity,
                Sum       = pi.Sum,
                NDS       = pi.NDS,
                ProductID = pi.ProductID,
                ReceiptID = pi.ReceiptID
            })
                         .ToArray();

            return(result);
        }
        public async Task <ProductItemDto> Handle(GetProductItemQuery request, CancellationToken cancellationToken)
        {
            if (request.ID <= 0)
            {
                throw new ValidationException($"{nameof(request.ID)} is invalid");
            }

            if (!Guid.TryParse(_principal.GetUserID(), out var userID))
            {
                throw new BusinessException($"{nameof(userID)} is invalid");
            }

            var productItem = await _repository.GetAsync(request.ID, userID, cancellationToken);

            if (productItem == null)
            {
                throw new BusinessException("ProductItem is not exists!");
            }

            var result = new ProductItemDto
            {
                ID        = productItem.ID,
                Name      = productItem.Name,
                Price     = productItem.Price,
                Quantity  = productItem.Quantity,
                Sum       = productItem.Sum,
                NDS       = productItem.NDS,
                ProductID = productItem.ProductID,
                ReceiptID = productItem.ReceiptID
            };

            return(result);
        }
Exemple #6
0
        public async Task <ProductDto> Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            if (request.Dto == null)
            {
                throw new ValidationException($"{nameof(request.Dto)} is invalid");
            }

            if (!Guid.TryParse(_principal.GetUserID(), out var userID))
            {
                throw new BusinessException($"{nameof(userID)} is invalid");
            }

            var item = new Product
            {
                Name       = request.Dto.Name,
                CategoryID = request.Dto.CategoryID,
                UserID     = userID
            };

            var product = await _repository.CreateAsync(item, cancellationToken);

            var result = new ProductDto
            {
                ID         = product.ID,
                Name       = product.Name,
                CategoryID = product.CategoryID
            };

            return(result);
        }
Exemple #7
0
        public async Task <Unit> Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            var userID = _principal.GetUserID();

            await _usersRepository.DeleteAsync(userID);

            return(Unit.Value);
        }
        public async Task <User> GetAsync(String id)
        {
            if (!Guid.TryParse(_principal.GetUserID(), out var userID))
            {
                throw new BusinessException($"{nameof(userID)} is invalid");
            }

            var user = _dataContext.Users.FirstOrDefault(u => u.Id == userID);

            return(await Task.FromResult(user));
        }
Exemple #9
0
        public async Task <ReceiptDto> Handle(GetReceiptQuery request, CancellationToken cancellationToken)
        {
            if (request.ID <= 0)
            {
                throw new ValidationException($"{nameof(request.ID)} is invalid");
            }

            if (!Guid.TryParse(_principal.GetUserID(), out var userID))
            {
                throw new BusinessException($"{nameof(userID)} is invalid");
            }

            var receipt = await _repository.GetAsync(request.ID, userID, cancellationToken);

            if (receipt == null)
            {
                throw new BusinessException("ProductItem is not exists!");
            }

            var result = new ReceiptDto
            {
                ID = receipt.ID,
                FiscalDocumentNumber = receipt.FiscalDocumentNumber,
                FiscalDriveNumber    = receipt.FiscalDriveNumber,
                FiscalType           = receipt.FiscalType,
                DateTime             = receipt.DateTime,
                TotalSum             = receipt.TotalSum,
                OperationType        = receipt.OperationType,
                Place        = receipt.Place,
                Organization = new OrganizationDto
                {
                    INN  = receipt.Organization.INN,
                    Name = receipt.Organization.Name
                },
                ProductItems = receipt.ProductItems.Select(pi => new ProductItemDto
                {
                    ID        = pi.ID,
                    Name      = pi.Name,
                    Price     = pi.Price,
                    Quantity  = pi.Quantity,
                    Sum       = pi.Sum,
                    NDS       = pi.NDS,
                    ReceiptID = pi.ReceiptID,
                    ProductID = pi.ProductID
                }).ToArray()
            };

            return(result);
        }
		public async Task<Unit> Handle(DeleteProductCommand request, CancellationToken cancellationToken)
		{
			if (request.ID <= 0)
			{
				throw new ValidationException($"{nameof(request.ID)} is invalid");
			}

			if (!Guid.TryParse(_principal.GetUserID(), out var userID))
			{
				throw new BusinessException($"{nameof(userID)} is invalid");
			}

			await _repository.DeleteAsync(request.ID, userID, cancellationToken);

			return Unit.Value;
		}
Exemple #11
0
        public async Task <ProductItemDto> Handle(UpdateProductItemCommand request, CancellationToken cancellationToken)
        {
            if (request.Dto == null)
            {
                throw new ValidationException($"{nameof(request.Dto)} is invalid");
            }

            if (request.Dto.ID <= 0)
            {
                throw new ValidationException($"{nameof(request.Dto.ID)} is invalid");
            }

            if (!Guid.TryParse(_principal.GetUserID(), out var userID))
            {
                throw new BusinessException($"{nameof(userID)} is invalid");
            }

            var item = new ProductItem
            {
                ID        = request.Dto.ID,
                Name      = request.Dto.Name,
                Price     = request.Dto.Price,
                Quantity  = request.Dto.Quantity,
                Sum       = request.Dto.Sum,
                NDS       = request.Dto.NDS,
                ProductID = request.Dto.ProductID,
                ReceiptID = request.Dto.ReceiptID,
                UserID    = userID
            };

            var productItem = await _repository.UpdateAsync(item, cancellationToken);

            var result = new ProductItemDto
            {
                ID        = productItem.ID,
                Name      = productItem.Name,
                Price     = productItem.Price,
                Quantity  = productItem.Quantity,
                Sum       = productItem.Sum,
                NDS       = productItem.NDS,
                ProductID = productItem.ProductID,
                ReceiptID = productItem.ReceiptID
            };

            return(result);
        }
        public async Task <Unit> Handle(ResetUserPasswordQuery request, CancellationToken cancellationToken)
        {
            var userID = _principal.GetUserID();
            var user   = await _usersRepository.GetAsync(userID);

            if (user == null)
            {
                throw new BusinessException("User is not exists!");
            }

            var to    = new EmailAddress(user.Email, user.UserName);
            var token = await _usersRepository.GeneratePasswordResetTokenAsync(user);

            var emailMessage = _emailMessageFactory.CreatePasswordResettingMessage(to, token);

            await _emailService.SendAsync(emailMessage);

            return(Unit.Value);
        }
        public async Task <UserDto> Handle(GetUserQuery request, CancellationToken cancellationToken)
        {
            var userID = _principal.GetUserID();
            var user   = await _usersRepository.GetAsync(userID);

            if (user == null)
            {
                throw new BusinessException("User is not exists!");
            }

            var result = new UserDto
            {
                ID             = user.Id,
                UserName       = user.UserName,
                Email          = user.Email,
                EmailConfirmed = user.EmailConfirmed
            };

            return(result);
        }
        public async Task <ProductDto[]> Handle(GetProductsQuery request, CancellationToken cancellationToken)
        {
            if (!Guid.TryParse(_principal.GetUserID(), out var userID))
            {
                throw new BusinessException($"{nameof(userID)} is invalid");
            }

            var products = await _repository.GetAsync(userID, cancellationToken);

            var result = products
                         .Select(p => new ProductDto
            {
                ID         = p.ID,
                Name       = p.Name,
                CategoryID = p.CategoryID
            })
                         .ToArray();

            return(result);
        }
Exemple #15
0
        public async Task <Unit> Handle(ChangeUserEmailQuery request, CancellationToken cancellationToken)
        {
            if (String.IsNullOrWhiteSpace(request.Email))
            {
                throw new ValidationException($"{nameof(request.Email)} is invalid");
            }

            var userID = _principal.GetUserID();
            var user   = await _usersRepository.GetAsync(userID);

            if (user == null)
            {
                throw new BusinessException("User is not exists!");
            }

            var to    = new EmailAddress(user.Email, user.UserName);
            var token = await _usersRepository.GenerateChangeEmailTokenAsync(user, request.Email);

            var emailMessage = _emailMessageFactory.CreateEmailChangingMessage(to, token);

            await _emailService.SendAsync(emailMessage);

            return(Unit.Value);
        }
        public async Task <ReceiptDto> Handle(CreateReceiptCommand request, CancellationToken cancellationToken)
        {
            if (request.Dto == null)
            {
                throw new ValidationException($"{nameof(request.Dto)} is invalid");
            }

            if (!Guid.TryParse(_principal.GetUserID(), out var userID))
            {
                throw new BusinessException($"{nameof(userID)} is invalid");
            }

            var existingReceipt = await _repository.FindAsync(request.Dto.FiscalDocumentNumber, request.Dto.FiscalDriveNumber, userID, cancellationToken);

            if (existingReceipt != null)
            {
                throw new BusinessException("Receipt already exists!");
            }

            var item = new Receipt
            {
                FiscalDocumentNumber = request.Dto.FiscalDocumentNumber,
                FiscalDriveNumber    = request.Dto.FiscalDriveNumber,
                FiscalType           = request.Dto.FiscalType,
                DateTime             = request.Dto.DateTime,
                TotalSum             = request.Dto.TotalSum,
                OperationType        = request.Dto.OperationType,
                Place        = request.Dto.Place,
                Organization = new Organization
                {
                    INN  = request.Dto.Organization.INN,
                    Name = request.Dto.Organization.Name
                },
                ProductItems = request.Dto.ProductItems.Select(pi => new ProductItem
                {
                    ID       = pi.ID,
                    Name     = pi.Name,
                    Price    = pi.Price,
                    Quantity = pi.Quantity,
                    Sum      = pi.Sum,
                    NDS      = pi.NDS,
                    UserID   = userID
                }).ToList(),
                UserID = userID
            };

            var receipt = await _repository.CreateAsync(item, cancellationToken);

            var result = new ReceiptDto
            {
                ID = receipt.ID,
                FiscalDocumentNumber = receipt.FiscalDocumentNumber,
                FiscalDriveNumber    = receipt.FiscalDriveNumber,
                FiscalType           = receipt.FiscalType,
                DateTime             = receipt.DateTime,
                TotalSum             = receipt.TotalSum,
                OperationType        = receipt.OperationType,
                Place        = receipt.Place,
                Organization = new OrganizationDto
                {
                    INN  = receipt.Organization.INN,
                    Name = receipt.Organization.Name
                },
                ProductItems = receipt.ProductItems.Select(pi => new ProductItemDto
                {
                    ID        = pi.ID,
                    Name      = pi.Name,
                    Price     = pi.Price,
                    Quantity  = pi.Quantity,
                    Sum       = pi.Sum,
                    NDS       = pi.NDS,
                    ReceiptID = pi.ReceiptID,
                    ProductID = pi.ProductID
                }).ToArray()
            };

            return(result);
        }