コード例 #1
0
        public async Task <Result <ManufacturerDto> > Handle(UpdateManufacturerCommand request,
                                                             CancellationToken cancellationToken)
        {
            var          cacheKey = $"Manufacturer-{request.ManufacturerId}";
            Manufacturer manufacturerInDb;

            if (!await _cacheService.IsExistsAsync(cacheKey))
            {
                manufacturerInDb = await _manufacturerRepository.GetAsync(m => m.Id == request.ManufacturerId);

                manufacturerInDb.CheckForNull();
            }

            var manufacturerDto = _mapper.Map <ManufacturerDto>(request.Manufacturer) with {
                Id = request.ManufacturerId
            };

            manufacturerInDb = _mapper.Map <Manufacturer>(manufacturerDto);
            LogDto log = new(Guid.NewGuid().ToString(), request.UserName, "edited manufacturer",
                             JsonSerializer.Serialize(manufacturerDto, CommandExtensions.JsonSerializerOptions), DateTime.UtcNow);

            await _manufacturerRepository.UpdateAsync(m => m.Id == manufacturerInDb.Id, manufacturerInDb);

            await _sender.PublishAsync(new UpdatedManufacturer(manufacturerInDb), cancellationToken);

            await _sender.PublishAsync(log, cancellationToken);

            await _cacheService.UpdateAsync(cacheKey, manufacturerInDb, _manufacturerSettings);

            return(Result <ManufacturerDto> .Success(manufacturerDto));
        }
    }
コード例 #2
0
        public async Task <Result <ProductDto> > Handle(UpdateProductCommand request, CancellationToken cancellationToken)
        {
            var     cacheKey = $"Product-{request.ProductId}";
            Product productInDb;

            if (!await _cacheService.IsExistsAsync(cacheKey))
            {
                productInDb = await _productRepository.GetAsync(p => p.Id == request.ProductId);

                productInDb.CheckForNull();
            }

            productInDb = await _productService.GetManufacturersAndCustomer(request.Product,
                                                                            request.Product.ManufacturerIds.ToList());

            productInDb.Id = request.ProductId;
            var result = _mapper.Map <ProductDto>(productInDb) with {
                Id = request.ProductId
            };
            LogDto log = new(Guid.NewGuid().ToString(), request.UserName, "edited product",
                             JsonSerializer.Serialize(result, CommandExtensions.JsonSerializerOptions), DateTime.UtcNow);

            await _cacheService.SetCacheAsync(cacheKey, productInDb, _productSettings);

            await _sender.PublishAsync(log, cancellationToken);

            await _productRepository.UpdateAsync(p => p.Id == productInDb.Id, productInDb);

            return(Result <ProductDto> .Success(result));
        }
    }
コード例 #3
0
        public async Task <Result <object> > Handle(DeleteUserCommand request, CancellationToken cancellationToken)
        {
            var cacheKey = $"User-{request.UserId}";

            if (!await _cacheService.IsExistsAsync(cacheKey))
            {
                var userInDb = await _userRepository.GetAsync(u => u.Id == request.UserId);

                userInDb.CheckForNull();
            }

            var token = await _tokenRepository.GetAsync(t => t.Id == request.UserId);

            if (token is not null)
            {
                await _tokenRepository.DeleteAsync(t => t.Id == token.Id);

                await _sender.PublishAsync(token, cancellationToken);
            }

            await _userRepository.DeleteAsync(u => u.Id == request.UserId);

            await _cacheService.RemoveAsync(cacheKey);

            return(Result <object> .Success());
        }
コード例 #4
0
        public async Task <Result <ManufacturerDto> > Handle(CreateManufacturerCommand request,
                                                             CancellationToken cancellationToken)
        {
            var manufacturerDto  = _mapper.Map <ManufacturerDto>(request.Manufacturer);
            var manufacturerToDb = _mapper.Map <Manufacturer>(manufacturerDto);

            var    cacheKey = $"Manufacturer-{manufacturerToDb.Id}";
            LogDto log      = new(Guid.NewGuid().ToString(), request.UserName, "added manufacturer",
                                  JsonSerializer.Serialize(manufacturerDto, CommandExtensions.JsonSerializerOptions), DateTime.UtcNow);

            await _cacheService.SetCacheAsync(cacheKey, manufacturerToDb, _manufacturerSettings);

            await _manufacturerRepository.CreateAsync(manufacturerToDb);

            await _sender.PublishAsync(new CreatedManufacturer(manufacturerToDb), cancellationToken);

            await _sender.PublishAsync(log, cancellationToken);

            return(Result <ManufacturerDto> .Success(manufacturerDto));
        }
コード例 #5
0
        public async Task <Result <CustomerDto> > Handle(CreateCustomerCommand request,
                                                         CancellationToken cancellationToken)
        {
            var customerToDb = _mapper.Map <Customer>(request.Customer);

            var    cacheKey = $"Customer-{customerToDb.Id}";
            LogDto log      =
                new(Guid.NewGuid().ToString(), request.UserName, "added customer", JsonSerializer.Serialize(
                        customerToDb,
                        CommandExtensions.JsonSerializerOptions), DateTime.UtcNow);

            await _cacheService.SetCacheAsync(cacheKey, customerToDb, _customerSettings);

            await _customerRepository.CreateAsync(customerToDb);

            await _sender.PublishAsync(new CreatedCustomer(customerToDb), cancellationToken);

            await _sender.PublishAsync(log, cancellationToken);

            return(Result <CustomerDto> .Success(request.Customer with {
                Id = customerToDb.Id
            }));
        }
コード例 #6
0
        public async Task <Result <object> > Handle(DeleteManufacturerCommand request, CancellationToken cancellationToken)
        {
            var cacheKey = $"Manufacturer-{request.Id}";

            if (!await _cacheService.IsExistsAsync(cacheKey))
            {
                var manufacturerInDb = await _manufacturerRepository.GetAsync(m => m.Id == request.Id);

                manufacturerInDb.CheckForNull();
            }

            await _sender.PublishAsync(new DeletedManufacturer(request.Id), cancellationToken);

            await _manufacturerRepository.DeleteAsync(m => m.Id == request.Id);

            await _cacheService.RemoveAsync(cacheKey);

            return(Result <object> .Success());
        }
コード例 #7
0
        public async Task <Result <ProductDto> > Handle(CreateProductCommand request, CancellationToken cancellationToken)
        {
            var productToDb =
                await _productService.GetManufacturersAndCustomer(request.Product,
                                                                  request.Product.ManufacturerIds.ToList());

            var cacheKey = $"Product-{productToDb.Id}";
            await _cacheService.SetCacheAsync(cacheKey, productToDb, _productSettings);

            var    result = _mapper.Map <ProductDto>(productToDb);
            LogDto log    = new(Guid.NewGuid().ToString(), request.UserName, "added product",
                                JsonSerializer.Serialize(result, CommandExtensions.JsonSerializerOptions), DateTime.UtcNow);

            await _sender.PublishAsync(log, cancellationToken);

            await _productRepository.CreateAsync(productToDb);

            return(Result <ProductDto> .Success(result));
        }
コード例 #8
0
        public async Task <Result <UserDto> > Handle(UpdateUserCommand request, CancellationToken cancellationToken)
        {
            var  cacheKey = $"User-{request.UserId}";
            User userInDb;

            if (!await _cacheService.IsExistsAsync(cacheKey))
            {
                userInDb = await _userRepository.GetAsync(u => u.Id == request.UserId);

                userInDb.CheckForNull();
            }

            var userDto = _mapper.Map <UserDto>(request.User) with {
                Id = request.UserId
            };

            userInDb = _mapper.Map <User>(userDto);
            var token = await _tokenRepository.GetAsync(t => t.User.Id == request.UserId);

            LogDto log =
                new(Guid.NewGuid().ToString(), request.UserName, "edited user", JsonSerializer.Serialize(userDto,
                                                                                                         CommandExtensions.JsonSerializerOptions), DateTime.UtcNow);

            if (token is not null)
            {
                token.User = userInDb;
                await _tokenRepository.UpdateAsync(t => t.Id == token.Id, token);
            }

            await _userRepository.UpdateAsync(u => u.Id == userInDb.Id, userInDb);

            await _cacheService.UpdateAsync(cacheKey, userInDb, _userSettings);

            await _sender.PublishAsync(log, cancellationToken);

            return(Result <UserDto> .Success(userDto));
        }
    }