/// <summary>
 /// Maps LoanId, ReceiptRowId, LoanRowId, Involvement, Name
 /// </summary>
 /// <param name="dto"></param>
 /// <returns></returns>
 /// <exception cref="NullReferenceException"></exception>
 public static RowParticipantDTO FromBLL(BLLRowParticipantDTO dto)
 {
     if (dto == null)
     {
         throw new NullReferenceException("Can't map, BLLRowParticipantDTO is null");
     }
     return(new RowParticipantDTO()
     {
         Name = dto.Name,
         LoanId = dto.LoanId,
         ReceiptRowId = dto.ReceiptRowId,
         Involvement = dto.Involvement,
         AppUserId = dto.AppUserId,
         LoanRowId = dto.LoanRowId
     });
 }
示例#2
0
        public async Task <BLLReceiptRowDTO> AddRowParticipantAsync(BLLRowParticipantDTO newParticipant, int currentUserId)
        {
            //Involvement is between 0 and 1
            if (newParticipant.Involvement == null ||
                newParticipant.Involvement > 1 ||
                newParticipant.Involvement <= 0)
            {
                return(null);
            }
            if (newParticipant.AppUserId == null)
            {
                return(null);
            }

            //Participant user exists
            if (!await Uow.AppUsers.Exists(newParticipant.AppUserId.Value))
            {
                return(null);
            }

            var receiptRow = await Uow.ReceiptRows.FindRowAndRelatedDataAsync(newParticipant.ReceiptRowId);

            if (receiptRow?.ReceiptId == null)
            {
                return(null);
            }

            var receipt = await Uow.Receipts.FindReceiptAsync(receiptRow.ReceiptId.Value);


            if (receipt?.ReceiptParticipants == null)
            {
                return(null);
            }
            if (receipt.ReceiptManagerId != currentUserId || receipt.IsFinalized)
            {
                return(null);
            }


            if (receiptRow.Participants != null && receiptRow.Participants.Any())
            {
                //Participant can't already be a participant in this row
                if (receiptRow.Participants.Any(dto => dto.AppUserId == newParticipant.AppUserId))
                {
                    return(null);
                }

                var currentInvolvementSum = receiptRow.Participants.Select(dto => dto.Involvement).Sum();
                //Total involvement can't be greater than 1
                if (currentInvolvementSum == null || currentInvolvementSum + newParticipant.Involvement > 1)
                {
                    return(null);
                }
            }

            var receiptParticipant = await Uow.ReceiptParticipants.FindOrAddAsync(receipt.ReceiptId, newParticipant.AppUserId.Value);

            var loanId = await Uow.Loans.FindOrAddAsync(receiptParticipant, receipt.ReceiptManagerId);

            await Uow.LoanRows.AddAsync(loanId, receiptRow.ReceiptRowId.Value, newParticipant.Involvement.Value);

            await Uow.SaveChangesAsync();

            receiptRow = await Uow.ReceiptRows.FindRowAndRelatedDataAsync(newParticipant.ReceiptRowId);

            return(ReceiptRowMapper.FromDAL(receiptRow));
        }