Exemple #1
0
 public override string ToString() => sb.Join("\n");
Exemple #2
0
        /// <summary>
        ///  Handle the command with context
        /// </summary>
        /// <param name="command">The command to handle</param>
        /// <param name="context">The correlation context</param>
        /// <returns></returns>
        public override async Task HandleAsync(CreateArticle command, ICorrelationContext context)
        {
            await base.HandleAsync(command, context);

            var product = GetDomainObject(command);

            if (product.Code == null)
            {
                throw new MicroSException("article_code_needed", $"The code article must be provided");
            }
            var exist = await Repository.ExistsAsync(article => article.Code == product.Code);

            if (exist)
            {
                //throw new MicroSException("article_code_duplicate", $"This code article ${product.Code} already exist");
                await BusPublisher.PublishAsync(new ArticleDuplicatedEvent(product.Code), context);
            }
            if (string.IsNullOrEmpty(product.Type))
            {
                throw new MicroSException("article_invalid", $"you must define {nameof(product.Type)}");
            }
            if (product.Type == "MO")
            {
                WeStringBuilder sb = new WeStringBuilder();

                if (product.TxOpe <= 0)
                {
                    sb.Add($"you must define {nameof(product.TxOpe)}");
                }
                if (product.TxPrep <= 0)
                {
                    sb.Add($"you must define {nameof(product.TxPrep)}");
                }
                if (sb.HasItems)
                {
                    throw new MicroSException("article_invalid", sb.Join("\n"));
                }
                product.Nomenclatures = null;
                product.Operations    = null;
                product.Tarifs        = null;
            }
            else if (product.Type == "TO" || product.Type == "PR")
            {
                WeStringBuilder sb = new WeStringBuilder();
                if (product.Prix <= 0)
                {
                    sb.Add($"you must define {nameof(product.Prix)}");
                }
                if (product.Densite <= 0)
                {
                    sb.Add($"you must define {nameof(product.Densite)}");
                }
                if (sb.HasItems)
                {
                    throw new MicroSException("article_invalid", sb.Join("\n"));
                }
            }
            if (product.TpPrep < 0)
            {
                throw new MicroSException("article_invalid", $"you must define {nameof(product.TpPrep)}");
            }
            if (product.TpOpe < 0)
            {
                throw new MicroSException("article_invalid", $"you must define {nameof(product.TpOpe)}");
            }
            if (product.TpBase < 0)
            {
                throw new MicroSException("article_invalid", $"you must define {nameof(product.TpBase)}");
            }

            if (product.Operations != null)
            {
                var query = from op in product.Operations.ToList() group op by op.Ordre into grp where grp.Count() > 1 select grp.Key;
                if (query.Any())
                {
                    WeStringBuilder sb = new WeStringBuilder();
                    sb.Add(query);
                    throw new MicroSException("operation_duplicate", $"Cannot have duplicate Ordre:${sb.Join(",")}");
                }
            }

            await Repository.AddAsync(product);

            await BusPublisher.PublishAsync(CreateEvent <ArticleCreated>(command), context);
        }