public IEnumerable <EndorsementDto> GetEndorsements(int billId)
        {
            var billOfExchange = _billsOfExchangeService.GetById(billId);

            if (billOfExchange.DrawerId == billOfExchange.BeneficiaryId)
            {
                throw new ApplicationException("Bill of exchange has same drawer as beneficiary.");
            }

            var         endorsements       = _repository.GetByBillIds(new[] { billId }).First().ToList();
            var         list               = new LinkedList <Endorsement>();
            Endorsement currentEndorsement = null;

            while (currentEndorsement == null ||
                   endorsements.Any(x => x.PreviousEndorsementId == currentEndorsement.Id))
            {
                currentEndorsement = currentEndorsement == null
                    ? GetFirstEndorsement(endorsements)
                    : endorsements.FirstOrDefault(x => x.PreviousEndorsementId == currentEndorsement.Id);

                if (list.Any(x => x.Id == currentEndorsement.Id))
                {
                    throw new ApplicationException(
                              "Indosament data inconsistency - circle reference between endorsements");
                }

                if (currentEndorsement.NewBeneficiaryId == billOfExchange.BeneficiaryId)
                {
                    throw new ApplicationException(
                              $"Endorsement has the same new beneficiary party as bill of exchange");
                }

                list.AddLast(currentEndorsement);
            }

            if (list.Count != endorsements.Count)
            {
                throw new ApplicationException("List of endorsement is not linked.");
            }

            var newBeneficiaries = list.Select(x => x.NewBeneficiaryId).ToList();

            if (newBeneficiaries.Count() != newBeneficiaries.Distinct().Count())
            {
                throw new ApplicationException("Indosament data inconsistenci - same new beneficiary id");
            }

            return(MapToDto(list));
        }
 public BillOfExchangeDetailDto GetById([FromRoute] int id) =>
 _billsOfExchangeService.GetById(id);