Ejemplo n.º 1
0
        public async Task <BusinessPartner> UpdateBusinessPartnerAsync(string cid, BusinessPartner businessPartner,
                                                                       CancellationToken cancellation)
        {
            var validator = new CustomerValidator(ValidateCardGroupExist, ValidateSalesmanCodeExist);

            validator.RuleFor(x => x.Key).Must(x => x == cid);
            validator.RuleFor(x => x.Name).Must(x => x == null || !string.IsNullOrWhiteSpace(x));
            validator.RuleFor(x => x.GroupSn).Must(x => !x.HasValue || ValidateCardGroupExist(x))
            .WithMessage(x => $"Card Group {x.GroupSn} is invalid");
            validator.RuleFor(x => x.SalesmanCode).Must(x => !x.HasValue || ValidateSalesmanCodeExist(x))
            .WithMessage(x => $"Salesman with code {x.SalesmanCode} is  invalid");
            var isBusinessPannerValid = await validator.ValidateAsync(businessPartner, cancellation);

            if (!isBusinessPannerValid.IsValid)
            {
                throw new InvalidStateException(isBusinessPannerValid.Errors
                                                .Select(x => x.ErrorMessage).Aggregate((s1, s2) => $"{s1},{s2}"));
            }

            using var transaction = _dalService.CreateUnitOfWork();
            //TODO check which field are updateable
            var result = await transaction.BusinessPartners.UpdateAsync(businessPartner);

            await transaction.CompleteAsync(cancellation);

            return(result);
        }
Ejemplo n.º 2
0
        public async Task <BusinessPartner> AddBusinessPartnerAsync(BusinessPartner businessPartner,
                                                                    CancellationToken cancellation)
        {
            var validator = new CustomerValidator(ValidateCardGroupExist, ValidateSalesmanCodeExist);

            validator.RuleFor(x => x.Name).NotEmpty();
            validator.RuleFor(x => x.GroupSn).Must(ValidateCardGroupExist)
            .WithMessage(x => $"Card Group {x.GroupSn} is invalid");
            validator.RuleFor(x => x.SalesmanCode).Must(ValidateSalesmanCodeExist)
            .WithMessage(x => $"Salesman with code {x.SalesmanCode} is  invalid");
            validator.RuleFor(x => x.Key).Null();
            validator.RuleFor(x => x.CreationDateTime).Null();
            var isBusinessPannerValid = await validator.ValidateAsync(businessPartner, cancellation);

            if (!isBusinessPannerValid.IsValid)
            {
                throw new InvalidStateException(isBusinessPannerValid.Errors
                                                .Select(x => x.ErrorMessage).Aggregate((s1, s2) => $"{s1},{s2}"));
            }


            using var transaction            = _dalService.CreateUnitOfWork();
            businessPartner.CreationDateTime = DateTime.Now;
            var result = await transaction.BusinessPartners.AddAsync(businessPartner);

            await transaction.CompleteAsync(cancellation);

            return(result);
        }