public string Handle(CreateBaseProductCommand command)
        {
            var existingProduct = context.Set <BaseProduct>()
                                  .FirstOrDefault(x => x.ProductNumber == command.ProductNumber);

            if (existingProduct != null)
            {
                throw new DuplicateProductException(command.ProductNumber);
            }

            var newProduct = mapper.Map <BaseProduct>(command);

            context.Set <BaseProduct>().Add(newProduct);

            context.SaveChangesAsync();

            return(newProduct.ProductNumber);
        }
예제 #2
0
        public string Handle(CreateTransactionCommand command)
        {
            var existingTransaction = context.Set <Transaction>()
                                      .FirstOrDefault(x => x.TransactionNumber == command.TransactionNumber);

            if (existingTransaction != null)
            {
                throw new DuplicateTransactionNumerException(command.TransactionNumber);
            }

            var transaction = mapper.Map <Transaction>(command);

            var customer = customerUpdater.GetCustomerByCustomerNumber(command.CustomerId);

            if (customer != null)
            {
                transaction.CustomerId = customer.Id;
                customerUpdater.UpdateCustomerEmotion(customer, command);
            }

            if (productUpdater.ValidListOfProductResult(mapper.Map <IEnumerable <UpdatedProductSnapshot> >(command.ProductSnapshotDto)).IsValid)
            {
                transaction.ProductSnapshot = productUpdater.CreateNewListofProductSnapshot(
                    mapper.Map <IEnumerable <UpdatedProductSnapshot> >(command.ProductSnapshotDto),
                    transaction.Id);

                context.Set <Transaction>().Add(transaction);

                context.SaveChangesAsync();

                //TODO: Move this function after implementing background jobs.
                productUpdater.UpdateProductQuantity(mapper.Map <IEnumerable <UpdatedProductSnapshot> >(command.ProductSnapshotDto));
            }

            return(transaction.TransactionNumber);
        }